From d3c677e78e98577be101003722fcbcbdf772f3c4 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Thu, 21 Sep 2017 11:46:05 -0700 Subject: [PATCH 2/2] Fix pnstrdup() to not memcpy() the maximum allowed length. The previous behaviour was dangerous if the length passed wasn't the size of the underlying buffer, but the maximum size of the underlying buffer. Author: Andres Freund Discussion: https://postgr.es/m/20161003215524.mwz5p45pcverrkyk@alap3.anarazel.de --- src/backend/utils/mmgr/mcxt.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index cd696f16bc..64e0408d5a 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -21,6 +21,7 @@ #include "postgres.h" +#include "common/string.h" #include "miscadmin.h" #include "utils/memdebug.h" #include "utils/memutils.h" @@ -1086,10 +1087,14 @@ pstrdup(const char *in) char * pnstrdup(const char *in, Size len) { - char *out = palloc(len + 1); + char *out; + len = pg_strnlen(in, len); + + out = palloc(len + 1); memcpy(out, in, len); out[len] = '\0'; + return out; } -- 2.14.1.536.g6867272d5b.dirty