Commit 82c117cb authored by Andres Freund's avatar Andres Freund

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
parent 8a241792
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "postgres.h" #include "postgres.h"
#include "common/string.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "utils/memdebug.h" #include "utils/memdebug.h"
#include "utils/memutils.h" #include "utils/memutils.h"
...@@ -1086,10 +1087,14 @@ pstrdup(const char *in) ...@@ -1086,10 +1087,14 @@ pstrdup(const char *in)
char * char *
pnstrdup(const char *in, Size len) 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); memcpy(out, in, len);
out[len] = '\0'; out[len] = '\0';
return out; return out;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment