Skip to content

Commit 3a4b891

Browse files
committed
Fix more format truncation issues
Fix the warnings created by the compiler warning options -Wformat-overflow=2 -Wformat-truncation=2, supported since GCC 7. This is a more aggressive variant of the fixes in 6275f5d, which GCC 7 warned about by default. The issues are all harmless, but some dubious coding patterns are cleaned up. One issue that is of external interest is that BGW_MAXLEN is increased from 64 to 96. Apparently, the old value would cause the bgw_name of logical replication workers to be truncated in some circumstances. But this doesn't actually add those warning options. It appears that the warnings depend a bit on compilation and optimization options, so it would be annoying to have to keep up with that. This is more of a once-in-a-while cleanup. Reviewed-by: Michael Paquier <[email protected]>
1 parent 648a6c7 commit 3a4b891

File tree

14 files changed

+38
-54
lines changed

14 files changed

+38
-54
lines changed

contrib/pgstattuple/pgstattuple.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static Datum
8989
build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
9090
{
9191
#define NCOLUMNS 9
92-
#define NCHARS 32
92+
#define NCHARS 314
9393

9494
HeapTuple tuple;
9595
char *values[NCOLUMNS];

src/backend/commands/explain.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -3337,10 +3337,11 @@ void
33373337
ExplainPropertyFloat(const char *qlabel, double value, int ndigits,
33383338
ExplainState *es)
33393339
{
3340-
char buf[256];
3340+
char *buf;
33413341

3342-
snprintf(buf, sizeof(buf), "%.*f", ndigits, value);
3342+
buf = psprintf("%.*f", ndigits, value);
33433343
ExplainProperty(qlabel, buf, true, es);
3344+
pfree(buf);
33443345
}
33453346

33463347
/*

src/backend/libpq/be-secure-openssl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ static const char *
10131013
SSLerrmessage(unsigned long ecode)
10141014
{
10151015
const char *errreason;
1016-
static char errbuf[32];
1016+
static char errbuf[36];
10171017

10181018
if (ecode == 0)
10191019
return _("no SSL error reported");

src/backend/utils/adt/dbsize.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ calculate_database_size(Oid dbOid)
8686
DIR *dirdesc;
8787
struct dirent *direntry;
8888
char dirpath[MAXPGPATH];
89-
char pathname[MAXPGPATH + 12 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
89+
char pathname[MAXPGPATH + 21 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
9090
AclResult aclresult;
9191

9292
/*

src/backend/utils/adt/float.c

+10-14
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ static const uint32 nan[2] = {0xffffffff, 0x7fffffff};
4444
#define NAN (*(const double *) nan)
4545
#endif
4646

47-
/* not sure what the following should be, but better to make it over-sufficient */
48-
#define MAXFLOATWIDTH 64
49-
#define MAXDOUBLEWIDTH 128
50-
5147
/*
5248
* check to see if a float4/8 val has underflowed or overflowed
5349
*/
@@ -360,18 +356,18 @@ Datum
360356
float4out(PG_FUNCTION_ARGS)
361357
{
362358
float4 num = PG_GETARG_FLOAT4(0);
363-
char *ascii = (char *) palloc(MAXFLOATWIDTH + 1);
359+
char *ascii;
364360

365361
if (isnan(num))
366-
PG_RETURN_CSTRING(strcpy(ascii, "NaN"));
362+
PG_RETURN_CSTRING(pstrdup("NaN"));
367363

368364
switch (is_infinite(num))
369365
{
370366
case 1:
371-
strcpy(ascii, "Infinity");
367+
ascii = pstrdup("Infinity");
372368
break;
373369
case -1:
374-
strcpy(ascii, "-Infinity");
370+
ascii = pstrdup("-Infinity");
375371
break;
376372
default:
377373
{
@@ -380,7 +376,7 @@ float4out(PG_FUNCTION_ARGS)
380376
if (ndig < 1)
381377
ndig = 1;
382378

383-
snprintf(ascii, MAXFLOATWIDTH + 1, "%.*g", ndig, num);
379+
ascii = psprintf("%.*g", ndig, num);
384380
}
385381
}
386382

@@ -596,18 +592,18 @@ float8out(PG_FUNCTION_ARGS)
596592
char *
597593
float8out_internal(double num)
598594
{
599-
char *ascii = (char *) palloc(MAXDOUBLEWIDTH + 1);
595+
char *ascii;
600596

601597
if (isnan(num))
602-
return strcpy(ascii, "NaN");
598+
return pstrdup("NaN");
603599

604600
switch (is_infinite(num))
605601
{
606602
case 1:
607-
strcpy(ascii, "Infinity");
603+
ascii = pstrdup("Infinity");
608604
break;
609605
case -1:
610-
strcpy(ascii, "-Infinity");
606+
ascii = pstrdup("-Infinity");
611607
break;
612608
default:
613609
{
@@ -616,7 +612,7 @@ float8out_internal(double num)
616612
if (ndig < 1)
617613
ndig = 1;
618614

619-
snprintf(ascii, MAXDOUBLEWIDTH + 1, "%.*g", ndig, num);
615+
ascii = psprintf("%.*g", ndig, num);
620616
}
621617
}
622618

src/backend/utils/adt/formatting.c

+10-23
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,6 @@
117117
#define DCH_MAX_ITEM_SIZ 12 /* max localized day name */
118118
#define NUM_MAX_ITEM_SIZ 8 /* roman number (RN has 15 chars) */
119119

120-
/* ----------
121-
* More is in float.c
122-
* ----------
123-
*/
124-
#define MAXFLOATWIDTH 60
125-
#define MAXDOUBLEWIDTH 500
126-
127120

128121
/* ----------
129122
* Format parser structs
@@ -3911,9 +3904,7 @@ do_to_timestamp(text *date_txt, text *fmt,
39113904
tmfc.tzm < 0 || tmfc.tzm >= MINS_PER_HOUR)
39123905
DateTimeParseError(DTERR_TZDISP_OVERFLOW, date_str, "timestamp");
39133906

3914-
tz = palloc(7);
3915-
3916-
snprintf(tz, 7, "%c%02d:%02d",
3907+
tz = psprintf("%c%02d:%02d",
39173908
tmfc.tzsign > 0 ? '+' : '-', tmfc.tzh, tmfc.tzm);
39183909

39193910
tm->tm_zone = tz;
@@ -4135,7 +4126,7 @@ int_to_roman(int number)
41354126
num = 0;
41364127
char *p = NULL,
41374128
*result,
4138-
numstr[5];
4129+
numstr[12];
41394130

41404131
result = (char *) palloc(16);
41414132
*result = '\0';
@@ -5441,8 +5432,7 @@ int4_to_char(PG_FUNCTION_ARGS)
54415432
/* we can do it easily because float8 won't lose any precision */
54425433
float8 val = (float8) value;
54435434

5444-
orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5445-
snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, val);
5435+
orgnum = (char *) psprintf("%+.*e", Num.post, val);
54465436

54475437
/*
54485438
* Swap a leading positive sign for a space.
@@ -5641,7 +5631,6 @@ float4_to_char(PG_FUNCTION_ARGS)
56415631
numstr = orgnum = int_to_roman((int) rint(value));
56425632
else if (IS_EEEE(&Num))
56435633
{
5644-
numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
56455634
if (isnan(value) || is_infinite(value))
56465635
{
56475636
/*
@@ -5655,7 +5644,7 @@ float4_to_char(PG_FUNCTION_ARGS)
56555644
}
56565645
else
56575646
{
5658-
snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
5647+
numstr = orgnum = psprintf("%+.*e", Num.post, value);
56595648

56605649
/*
56615650
* Swap a leading positive sign for a space.
@@ -5679,16 +5668,15 @@ float4_to_char(PG_FUNCTION_ARGS)
56795668
Num.pre += Num.multi;
56805669
}
56815670

5682-
orgnum = (char *) palloc(MAXFLOATWIDTH + 1);
5683-
snprintf(orgnum, MAXFLOATWIDTH + 1, "%.0f", fabs(val));
5671+
orgnum = (char *) psprintf("%.0f", fabs(val));
56845672
numstr_pre_len = strlen(orgnum);
56855673

56865674
/* adjust post digits to fit max float digits */
56875675
if (numstr_pre_len >= FLT_DIG)
56885676
Num.post = 0;
56895677
else if (numstr_pre_len + Num.post > FLT_DIG)
56905678
Num.post = FLT_DIG - numstr_pre_len;
5691-
snprintf(orgnum, MAXFLOATWIDTH + 1, "%.*f", Num.post, val);
5679+
orgnum = psprintf("%.*f", Num.post, val);
56925680

56935681
if (*orgnum == '-')
56945682
{ /* < 0 */
@@ -5747,7 +5735,6 @@ float8_to_char(PG_FUNCTION_ARGS)
57475735
numstr = orgnum = int_to_roman((int) rint(value));
57485736
else if (IS_EEEE(&Num))
57495737
{
5750-
numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
57515738
if (isnan(value) || is_infinite(value))
57525739
{
57535740
/*
@@ -5761,7 +5748,7 @@ float8_to_char(PG_FUNCTION_ARGS)
57615748
}
57625749
else
57635750
{
5764-
snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
5751+
numstr = orgnum = (char *) psprintf("%+.*e", Num.post, value);
57655752

57665753
/*
57675754
* Swap a leading positive sign for a space.
@@ -5784,15 +5771,15 @@ float8_to_char(PG_FUNCTION_ARGS)
57845771
val = value * multi;
57855772
Num.pre += Num.multi;
57865773
}
5787-
orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
5788-
numstr_pre_len = snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.0f", fabs(val));
5774+
orgnum = psprintf("%.0f", fabs(val));
5775+
numstr_pre_len = strlen(orgnum);
57895776

57905777
/* adjust post digits to fit max double digits */
57915778
if (numstr_pre_len >= DBL_DIG)
57925779
Num.post = 0;
57935780
else if (numstr_pre_len + Num.post > DBL_DIG)
57945781
Num.post = DBL_DIG - numstr_pre_len;
5795-
snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.*f", Num.post, val);
5782+
orgnum = psprintf("%.*f", Num.post, val);
57965783

57975784
if (*orgnum == '-')
57985785
{ /* < 0 */

src/backend/utils/misc/guc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -10528,7 +10528,7 @@ check_cluster_name(char **newval, void **extra, GucSource source)
1052810528
static const char *
1052910529
show_unix_socket_permissions(void)
1053010530
{
10531-
static char buf[8];
10531+
static char buf[12];
1053210532

1053310533
snprintf(buf, sizeof(buf), "%04o", Unix_socket_permissions);
1053410534
return buf;
@@ -10537,7 +10537,7 @@ show_unix_socket_permissions(void)
1053710537
static const char *
1053810538
show_log_file_mode(void)
1053910539
{
10540-
static char buf[8];
10540+
static char buf[12];
1054110541

1054210542
snprintf(buf, sizeof(buf), "%04o", Log_file_mode);
1054310543
return buf;

src/bin/initdb/initdb.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1009,12 +1009,12 @@ static char *
10091009
pretty_wal_size(int segment_count)
10101010
{
10111011
int sz = wal_segment_size_mb * segment_count;
1012-
char *result = pg_malloc(11);
1012+
char *result = pg_malloc(14);
10131013

10141014
if ((sz % 1024) == 0)
1015-
snprintf(result, 11, "%dGB", sz / 1024);
1015+
snprintf(result, 14, "%dGB", sz / 1024);
10161016
else
1017-
snprintf(result, 11, "%dMB", sz);
1017+
snprintf(result, 14, "%dMB", sz);
10181018

10191019
return result;
10201020
}

src/bin/pg_dump/pg_backup_archiver.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,7 @@ SetOutput(ArchiveHandle *AH, const char *filename, int compression)
15321532
#ifdef HAVE_LIBZ
15331533
if (compression != 0)
15341534
{
1535-
char fmode[10];
1535+
char fmode[14];
15361536

15371537
/* Don't use PG_BINARY_x since this is zlib */
15381538
sprintf(fmode, "wb%d", compression);

src/bin/pg_dump/pg_backup_tar.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ tarOpen(ArchiveHandle *AH, const char *filename, char mode)
335335
TAR_MEMBER *tm;
336336

337337
#ifdef HAVE_LIBZ
338-
char fmode[10];
338+
char fmode[14];
339339
#endif
340340

341341
if (mode == 'r')

src/bin/pgbench/pgbench.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3591,7 +3591,7 @@ parseQuery(Command *cmd)
35913591
p = sql;
35923592
while ((p = strchr(p, ':')) != NULL)
35933593
{
3594-
char var[12];
3594+
char var[13];
35953595
char *name;
35963596
int eaten;
35973597

@@ -5432,7 +5432,7 @@ threadRun(void *arg)
54325432
sqlat,
54335433
lag,
54345434
stdev;
5435-
char tbuf[64];
5435+
char tbuf[315];
54365436

54375437
/*
54385438
* Add up the statistics of all threads.

src/include/postmaster/bgworker.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ typedef enum
8282

8383
#define BGW_DEFAULT_RESTART_INTERVAL 60
8484
#define BGW_NEVER_RESTART -1
85-
#define BGW_MAXLEN 64
85+
#define BGW_MAXLEN 96
8686
#define BGW_EXTRALEN 128
8787

8888
typedef struct BackgroundWorker

src/interfaces/libpq/fe-secure-openssl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ PQsslAttribute(PGconn *conn, const char *attribute_name)
14361436

14371437
if (strcmp(attribute_name, "key_bits") == 0)
14381438
{
1439-
static char sslbits_str[10];
1439+
static char sslbits_str[12];
14401440
int sslbits;
14411441

14421442
SSL_get_cipher_bits(conn->ssl, &sslbits);

src/pl/tcl/pltcl.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
14561456
Datum prosrcdatum;
14571457
bool isnull;
14581458
char *proc_source;
1459-
char buf[32];
1459+
char buf[48];
14601460
Tcl_Interp *interp;
14611461
int i;
14621462
int tcl_rc;

0 commit comments

Comments
 (0)