diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2019-12-04 19:36:06 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2019-12-04 19:36:06 -0300 |
commit | 0b9466fce2cf4f8c32b3a9170ca272829aa11e66 (patch) | |
tree | 386bbebd3e2de5a4b4443f6fdfc5c130c30f3b1b /src/common/fe_memutils.c | |
parent | b1abfec825472434ea445b9700eaa80cde9da86a (diff) | |
download | postgresql-0b9466fce2cf4f8c32b3a9170ca272829aa11e66.tar.gz postgresql-0b9466fce2cf4f8c32b3a9170ca272829aa11e66.zip |
Offer pnstrdup to frontend code
We already had it on the backend. Frontend can also use it now.
Discussion: https://postgr.es/m/20191204144021.GA17976@alvherre.pgsql
Diffstat (limited to 'src/common/fe_memutils.c')
-rw-r--r-- | src/common/fe_memutils.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/common/fe_memutils.c b/src/common/fe_memutils.c index ce99b4f4da1..2bc6606b80c 100644 --- a/src/common/fe_memutils.c +++ b/src/common/fe_memutils.c @@ -142,6 +142,33 @@ pstrdup(const char *in) return pg_strdup(in); } +char * +pnstrdup(const char *in, Size size) +{ + char *tmp; + int len; + + if (!in) + { + fprintf(stderr, + _("cannot duplicate null pointer (internal error)\n")); + exit(EXIT_FAILURE); + } + + len = strnlen(in, size); + tmp = malloc(len + 1); + if (tmp == NULL) + { + fprintf(stderr, _("out of memory\n")); + exit(EXIT_FAILURE); + } + + memcpy(tmp, in, len); + tmp[len] = '\0'; + + return tmp; +} + void * repalloc(void *pointer, Size size) { |