Make SafeSnprintf work on PG11

pull/3465/head
Jelte Fennema 2020-02-25 15:19:20 +01:00
parent 7d24cebc80
commit 62bf571ced
2 changed files with 36 additions and 17 deletions

View File

@ -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 "postgres.h"
#include "safe_lib.h" #include "safe_lib.h"
@ -26,6 +21,18 @@
#include "distributed/citus_safe_lib.h" #include "distributed/citus_safe_lib.h"
#include "lib/stringinfo.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 * 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_list args;
va_start(args, format); 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); va_end(args);
return result; return result;
} }

View File

@ -44,6 +44,9 @@
#endif #endif
#include <sys/param.h> #include <sys/param.h>
/* 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, * 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 * first because the point of this module is to remove platform dependencies
@ -108,6 +111,13 @@
#undef fprintf #undef fprintf
#undef printf #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. * Info about where the formatted output is going.
* *
@ -163,7 +173,7 @@ static void dopr(PrintfTarget *target, const char *format, va_list args);
int 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; PrintfTarget target;
char onebyte[1]; char onebyte[1];
@ -191,19 +201,19 @@ pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args)
} }
int int
pg_snprintf(char *str, size_t count, const char *fmt,...) pg11_snprintf(char *str, size_t count, const char *fmt,...)
{ {
int len; int len;
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
len = pg_vsnprintf(str, count, fmt, args); len = pg11_vsnprintf(str, count, fmt, args);
va_end(args); va_end(args);
return len; return len;
} }
static int static int
pg_vsprintf(char *str, const char *fmt, va_list args) pg11_vsprintf(char *str, const char *fmt, va_list args)
{ {
PrintfTarget target; PrintfTarget target;
@ -219,19 +229,19 @@ pg_vsprintf(char *str, const char *fmt, va_list args)
} }
int int
pg_sprintf(char *str, const char *fmt,...) pg11_sprintf(char *str, const char *fmt,...)
{ {
int len; int len;
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
len = pg_vsprintf(str, fmt, args); len = pg11_vsprintf(str, fmt, args);
va_end(args); va_end(args);
return len; return len;
} }
int int
pg_vfprintf(FILE *stream, const char *fmt, va_list args) pg11_vfprintf(FILE *stream, const char *fmt, va_list args)
{ {
PrintfTarget target; PrintfTarget target;
char buffer[1024]; /* size is arbitrary */ char buffer[1024]; /* size is arbitrary */
@ -253,25 +263,25 @@ pg_vfprintf(FILE *stream, const char *fmt, va_list args)
} }
int int
pg_fprintf(FILE *stream, const char *fmt,...) pg11_fprintf(FILE *stream, const char *fmt,...)
{ {
int len; int len;
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
len = pg_vfprintf(stream, fmt, args); len = pg11_vfprintf(stream, fmt, args);
va_end(args); va_end(args);
return len; return len;
} }
int int
pg_printf(const char *fmt,...) pg11_printf(const char *fmt,...)
{ {
int len; int len;
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
len = pg_vfprintf(stdout, fmt, args); len = pg11_vfprintf(stdout, fmt, args);
va_end(args); va_end(args);
return len; return len;
} }
@ -1167,3 +1177,5 @@ trailing_pad(int *padlen, PrintfTarget *target)
++(*padlen); ++(*padlen);
} }
} }
#endif /* USE_REPL_SNPRINTF */
#endif /* PG_VERSION_NUM < 120000 */