From 1d0c3f36fc0a718ce335d510753ea9ee11e26b38 Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Tue, 25 Feb 2020 15:19:20 +0100 Subject: [PATCH] Make SafeSnprintf work on PG11 (cherry picked from commit 62bf571cedcfdf4451614b8520448d105df07045) --- .../distributed/utils/citus_safe_lib.c | 19 +++++++---- src/backend/distributed/utils/pg11_snprintf.c | 34 +++++++++++++------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/backend/distributed/utils/citus_safe_lib.c b/src/backend/distributed/utils/citus_safe_lib.c index 2e9875051..56ea7cdc7 100644 --- a/src/backend/distributed/utils/citus_safe_lib.c +++ b/src/backend/distributed/utils/citus_safe_lib.c @@ -12,11 +12,6 @@ *------------------------------------------------------------------------- */ -/* In PG 11 pg_vsnprintf is not exported unless you set this define */ -#if PG_VERSION_NUM < 120000 -#define USE_REPL_SNPRINTF 1 -#endif - #include "postgres.h" #include "safe_lib.h" @@ -26,6 +21,18 @@ #include "distributed/citus_safe_lib.h" #include "lib/stringinfo.h" +/* + * In PG 11 pg_vsnprintf is not exported and compiled in most cases, in that + * case use the copied one from pg11_snprintf.c + * NOTE: Whenever removing this section also remove pg11_snprintf.c + */ +#if PG_VERSION_NUM < 120000 +extern int pg11_vsnprintf(char *str, size_t count, const char *fmt, va_list args); +#define citus_vsnprintf pg11_vsnprintf +#else +#define citus_vsnprintf pg_vsnprintf +#endif + /* * ereport_constraint_handler is a constraint handler that calls ereport. A @@ -296,7 +303,7 @@ SafeSnprintf(char *restrict buffer, rsize_t bufsz, const char *restrict format, va_list args; va_start(args, format); - size_t result = pg_vsnprintf(buffer, bufsz, format, args); + size_t result = citus_vsnprintf(buffer, bufsz, format, args); va_end(args); return result; } diff --git a/src/backend/distributed/utils/pg11_snprintf.c b/src/backend/distributed/utils/pg11_snprintf.c index 54b32de60..0b2ca14ec 100644 --- a/src/backend/distributed/utils/pg11_snprintf.c +++ b/src/backend/distributed/utils/pg11_snprintf.c @@ -44,6 +44,9 @@ #endif #include +/* Include this file only for PG11 and only when USE_REPL_SNPRINTF is not set */ +#if PG_VERSION_NUM < 120000 +#ifndef USE_REPL_SNPRINTF /* * We used to use the platform's NL_ARGMAX here, but that's a bad idea, * first because the point of this module is to remove platform dependencies @@ -108,6 +111,13 @@ #undef fprintf #undef printf +extern int pg11_vsnprintf(char *str, size_t count, const char *fmt, va_list args); +extern int pg11_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4); +extern int pg11_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2, 3); +extern int pg11_vfprintf(FILE *stream, const char *fmt, va_list args); +extern int pg11_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3); +extern int pg11_printf(const char *fmt,...) pg_attribute_printf(1, 2); + /* * Info about where the formatted output is going. * @@ -163,7 +173,7 @@ static void dopr(PrintfTarget *target, const char *format, va_list args); int -pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args) +pg11_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { PrintfTarget target; char onebyte[1]; @@ -191,19 +201,19 @@ pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args) } int -pg_snprintf(char *str, size_t count, const char *fmt,...) +pg11_snprintf(char *str, size_t count, const char *fmt,...) { int len; va_list args; va_start(args, fmt); - len = pg_vsnprintf(str, count, fmt, args); + len = pg11_vsnprintf(str, count, fmt, args); va_end(args); return len; } static int -pg_vsprintf(char *str, const char *fmt, va_list args) +pg11_vsprintf(char *str, const char *fmt, va_list args) { PrintfTarget target; @@ -219,19 +229,19 @@ pg_vsprintf(char *str, const char *fmt, va_list args) } int -pg_sprintf(char *str, const char *fmt,...) +pg11_sprintf(char *str, const char *fmt,...) { int len; va_list args; va_start(args, fmt); - len = pg_vsprintf(str, fmt, args); + len = pg11_vsprintf(str, fmt, args); va_end(args); return len; } int -pg_vfprintf(FILE *stream, const char *fmt, va_list args) +pg11_vfprintf(FILE *stream, const char *fmt, va_list args) { PrintfTarget target; char buffer[1024]; /* size is arbitrary */ @@ -253,25 +263,25 @@ pg_vfprintf(FILE *stream, const char *fmt, va_list args) } int -pg_fprintf(FILE *stream, const char *fmt,...) +pg11_fprintf(FILE *stream, const char *fmt,...) { int len; va_list args; va_start(args, fmt); - len = pg_vfprintf(stream, fmt, args); + len = pg11_vfprintf(stream, fmt, args); va_end(args); return len; } int -pg_printf(const char *fmt,...) +pg11_printf(const char *fmt,...) { int len; va_list args; va_start(args, fmt); - len = pg_vfprintf(stdout, fmt, args); + len = pg11_vfprintf(stdout, fmt, args); va_end(args); return len; } @@ -1167,3 +1177,5 @@ trailing_pad(int *padlen, PrintfTarget *target) ++(*padlen); } } +#endif /* USE_REPL_SNPRINTF */ +#endif /* PG_VERSION_NUM < 120000 */