Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a051c19

Browse files
committed
Set snprintf.c's maximum number of NL arguments to be 31.
Previously, we used the platform's NL_ARGMAX if any, otherwise 16. The trouble with this is that the platform value is hugely variable, ranging from the POSIX-minimum 9 to as much as 64K on recent FreeBSD. Values of more than a dozen or two have no practical use and slow down the initialization of the argtypes array. Worse, they cause snprintf.c to consume far more stack space than was the design intention, possibly resulting in stack-overflow crashes. Standardize on 31, which is comfortably more than we need (it looks like no existing translatable message has more than about 10 parameters). I chose that, not 32, to make the array sizes powers of 2, for some possible small gain in speed of the memset. The lack of reported crashes suggests that the set of platforms we use snprintf.c on (in released branches) may have no overlap with the set where NL_ARGMAX has unreasonably large values. But that's not entirely clear, so back-patch to all supported branches. Per report from Mateusz Guzik (via Thomas Munro). Discussion: https://postgr.es/m/CAEepm=3VF=PUp2f8gU8fgZB22yPE_KBS0+e1AHAtQ=09schTHg@mail.gmail.com
1 parent 419cc8a commit a051c19

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/port/snprintf.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@
4444
#endif
4545
#include <sys/param.h>
4646

47-
#ifndef NL_ARGMAX
48-
#define NL_ARGMAX 16
49-
#endif
47+
/*
48+
* We used to use the platform's NL_ARGMAX here, but that's a bad idea,
49+
* first because the point of this module is to remove platform dependencies
50+
* not perpetuate them, and second because some platforms use ridiculously
51+
* large values, leading to excessive stack consumption in dopr().
52+
*/
53+
#define PG_NL_ARGMAX 31
5054

5155

5256
/*
@@ -345,8 +349,8 @@ dopr(PrintfTarget *target, const char *format, va_list args)
345349
double fvalue;
346350
char *strvalue;
347351
int i;
348-
PrintfArgType argtypes[NL_ARGMAX + 1];
349-
PrintfArgValue argvalues[NL_ARGMAX + 1];
352+
PrintfArgType argtypes[PG_NL_ARGMAX + 1];
353+
PrintfArgValue argvalues[PG_NL_ARGMAX + 1];
350354

351355
/*
352356
* Parse the format string to determine whether there are %n$ format
@@ -396,7 +400,7 @@ dopr(PrintfTarget *target, const char *format, va_list args)
396400
goto nextch1;
397401
case '$':
398402
have_dollar = true;
399-
if (accum <= 0 || accum > NL_ARGMAX)
403+
if (accum <= 0 || accum > PG_NL_ARGMAX)
400404
goto bad_format;
401405
if (afterstar)
402406
{

0 commit comments

Comments
 (0)