]> granicus.if.org Git - postgresql/commitdiff
Fix more format truncation issues
authorPeter Eisentraut <peter_e@gmx.net>
Thu, 15 Mar 2018 15:10:41 +0000 (11:10 -0400)
committerPeter Eisentraut <peter_e@gmx.net>
Thu, 15 Mar 2018 15:41:42 +0000 (11:41 -0400)
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
6275f5d28a1577563f53f2171689d4f890a46881, 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 <michael@paquier.xyz>
14 files changed:
contrib/pgstattuple/pgstattuple.c
src/backend/commands/explain.c
src/backend/libpq/be-secure-openssl.c
src/backend/utils/adt/dbsize.c
src/backend/utils/adt/float.c
src/backend/utils/adt/formatting.c
src/backend/utils/misc/guc.c
src/bin/initdb/initdb.c
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_backup_tar.c
src/bin/pgbench/pgbench.c
src/include/postmaster/bgworker.h
src/interfaces/libpq/fe-secure-openssl.c
src/pl/tcl/pltcl.c

index 7ca1bb24d2f3ee1e19b4175cf29781c03c8f4694..b599b6ca21a42541142a9255c982939ccbe96518 100644 (file)
@@ -89,7 +89,7 @@ static Datum
 build_pgstattuple_type(pgstattuple_type *stat, FunctionCallInfo fcinfo)
 {
 #define NCOLUMNS       9
-#define NCHARS         32
+#define NCHARS         314
 
        HeapTuple       tuple;
        char       *values[NCOLUMNS];
index 900fa74e85e8b90700422b4a32411111b8e9a019..f0dfef5a86df8cbb323f67396f84542a013022ff 100644 (file)
@@ -3337,10 +3337,11 @@ void
 ExplainPropertyFloat(const char *qlabel, double value, int ndigits,
                                         ExplainState *es)
 {
-       char            buf[256];
+       char       *buf;
 
-       snprintf(buf, sizeof(buf), "%.*f", ndigits, value);
+       buf = psprintf("%.*f", ndigits, value);
        ExplainProperty(qlabel, buf, true, es);
+       pfree(buf);
 }
 
 /*
index e1ddfb3c16e3e072cbf96e18e0424a0b7ea1a51b..567cf7d45508e4e13927be9e1655b9b0de299a0b 100644 (file)
@@ -1013,7 +1013,7 @@ static const char *
 SSLerrmessage(unsigned long ecode)
 {
        const char *errreason;
-       static char errbuf[32];
+       static char errbuf[36];
 
        if (ecode == 0)
                return _("no SSL error reported");
index 834a10485f6940ecc3421af9c12015461d2daeb0..07e5e78caa8fc935dd1839d1e182bedbffa962e7 100644 (file)
@@ -86,7 +86,7 @@ calculate_database_size(Oid dbOid)
        DIR                *dirdesc;
        struct dirent *direntry;
        char            dirpath[MAXPGPATH];
-       char            pathname[MAXPGPATH + 12 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
+       char            pathname[MAXPGPATH + 21 + sizeof(TABLESPACE_VERSION_DIRECTORY)];
        AclResult       aclresult;
 
        /*
index aadb92de6634eaefec6a31e10eac56888870f0ce..6522c0816ef01797e8a7fe33da50af29f1abe974 100644 (file)
@@ -44,10 +44,6 @@ static const uint32 nan[2] = {0xffffffff, 0x7fffffff};
 #define NAN (*(const double *) nan)
 #endif
 
-/* not sure what the following should be, but better to make it over-sufficient */
-#define MAXFLOATWIDTH  64
-#define MAXDOUBLEWIDTH 128
-
 /*
  * check to see if a float4/8 val has underflowed or overflowed
  */
@@ -360,18 +356,18 @@ Datum
 float4out(PG_FUNCTION_ARGS)
 {
        float4          num = PG_GETARG_FLOAT4(0);
-       char       *ascii = (char *) palloc(MAXFLOATWIDTH + 1);
+       char       *ascii;
 
        if (isnan(num))
-               PG_RETURN_CSTRING(strcpy(ascii, "NaN"));
+               PG_RETURN_CSTRING(pstrdup("NaN"));
 
        switch (is_infinite(num))
        {
                case 1:
-                       strcpy(ascii, "Infinity");
+                       ascii = pstrdup("Infinity");
                        break;
                case -1:
-                       strcpy(ascii, "-Infinity");
+                       ascii = pstrdup("-Infinity");
                        break;
                default:
                        {
@@ -380,7 +376,7 @@ float4out(PG_FUNCTION_ARGS)
                                if (ndig < 1)
                                        ndig = 1;
 
-                               snprintf(ascii, MAXFLOATWIDTH + 1, "%.*g", ndig, num);
+                               ascii = psprintf("%.*g", ndig, num);
                        }
        }
 
@@ -596,18 +592,18 @@ float8out(PG_FUNCTION_ARGS)
 char *
 float8out_internal(double num)
 {
-       char       *ascii = (char *) palloc(MAXDOUBLEWIDTH + 1);
+       char       *ascii;
 
        if (isnan(num))
-               return strcpy(ascii, "NaN");
+               return pstrdup("NaN");
 
        switch (is_infinite(num))
        {
                case 1:
-                       strcpy(ascii, "Infinity");
+                       ascii = pstrdup("Infinity");
                        break;
                case -1:
-                       strcpy(ascii, "-Infinity");
+                       ascii = pstrdup("-Infinity");
                        break;
                default:
                        {
@@ -616,7 +612,7 @@ float8out_internal(double num)
                                if (ndig < 1)
                                        ndig = 1;
 
-                               snprintf(ascii, MAXDOUBLEWIDTH + 1, "%.*g", ndig, num);
+                               ascii = psprintf("%.*g", ndig, num);
                        }
        }
 
index b8bd4caa3e7a3de7bca76d93dedb9be58886edd5..1a1088711c3a2281062a62fedbfcc2762414dce3 100644 (file)
 #define DCH_MAX_ITEM_SIZ          12   /* max localized day name               */
 #define NUM_MAX_ITEM_SIZ               8       /* roman number (RN has 15 chars)       */
 
-/* ----------
- * More is in float.c
- * ----------
- */
-#define MAXFLOATWIDTH  60
-#define MAXDOUBLEWIDTH 500
-
 
 /* ----------
  * Format parser structs
@@ -3911,9 +3904,7 @@ do_to_timestamp(text *date_txt, text *fmt,
                        tmfc.tzm < 0 || tmfc.tzm >= MINS_PER_HOUR)
                        DateTimeParseError(DTERR_TZDISP_OVERFLOW, date_str, "timestamp");
 
-               tz = palloc(7);
-
-               snprintf(tz, 7, "%c%02d:%02d",
+               tz = psprintf("%c%02d:%02d",
                                 tmfc.tzsign > 0 ? '+' : '-', tmfc.tzh, tmfc.tzm);
 
                tm->tm_zone = tz;
@@ -4135,7 +4126,7 @@ int_to_roman(int number)
                                num = 0;
        char       *p = NULL,
                           *result,
-                               numstr[5];
+                               numstr[12];
 
        result = (char *) palloc(16);
        *result = '\0';
@@ -5441,8 +5432,7 @@ int4_to_char(PG_FUNCTION_ARGS)
                /* we can do it easily because float8 won't lose any precision */
                float8          val = (float8) value;
 
-               orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
-               snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, val);
+               orgnum = (char *) psprintf("%+.*e", Num.post, val);
 
                /*
                 * Swap a leading positive sign for a space.
@@ -5641,7 +5631,6 @@ float4_to_char(PG_FUNCTION_ARGS)
                numstr = orgnum = int_to_roman((int) rint(value));
        else if (IS_EEEE(&Num))
        {
-               numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
                if (isnan(value) || is_infinite(value))
                {
                        /*
@@ -5655,7 +5644,7 @@ float4_to_char(PG_FUNCTION_ARGS)
                }
                else
                {
-                       snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
+                       numstr = orgnum = psprintf("%+.*e", Num.post, value);
 
                        /*
                         * Swap a leading positive sign for a space.
@@ -5679,8 +5668,7 @@ float4_to_char(PG_FUNCTION_ARGS)
                        Num.pre += Num.multi;
                }
 
-               orgnum = (char *) palloc(MAXFLOATWIDTH + 1);
-               snprintf(orgnum, MAXFLOATWIDTH + 1, "%.0f", fabs(val));
+               orgnum = (char *) psprintf("%.0f", fabs(val));
                numstr_pre_len = strlen(orgnum);
 
                /* adjust post digits to fit max float digits */
@@ -5688,7 +5676,7 @@ float4_to_char(PG_FUNCTION_ARGS)
                        Num.post = 0;
                else if (numstr_pre_len + Num.post > FLT_DIG)
                        Num.post = FLT_DIG - numstr_pre_len;
-               snprintf(orgnum, MAXFLOATWIDTH + 1, "%.*f", Num.post, val);
+               orgnum = psprintf("%.*f", Num.post, val);
 
                if (*orgnum == '-')
                {                                               /* < 0 */
@@ -5747,7 +5735,6 @@ float8_to_char(PG_FUNCTION_ARGS)
                numstr = orgnum = int_to_roman((int) rint(value));
        else if (IS_EEEE(&Num))
        {
-               numstr = orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
                if (isnan(value) || is_infinite(value))
                {
                        /*
@@ -5761,7 +5748,7 @@ float8_to_char(PG_FUNCTION_ARGS)
                }
                else
                {
-                       snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%+.*e", Num.post, value);
+                       numstr = orgnum = (char *) psprintf("%+.*e", Num.post, value);
 
                        /*
                         * Swap a leading positive sign for a space.
@@ -5784,15 +5771,15 @@ float8_to_char(PG_FUNCTION_ARGS)
                        val = value * multi;
                        Num.pre += Num.multi;
                }
-               orgnum = (char *) palloc(MAXDOUBLEWIDTH + 1);
-               numstr_pre_len = snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.0f", fabs(val));
+               orgnum = psprintf("%.0f", fabs(val));
+               numstr_pre_len = strlen(orgnum);
 
                /* adjust post digits to fit max double digits */
                if (numstr_pre_len >= DBL_DIG)
                        Num.post = 0;
                else if (numstr_pre_len + Num.post > DBL_DIG)
                        Num.post = DBL_DIG - numstr_pre_len;
-               snprintf(orgnum, MAXDOUBLEWIDTH + 1, "%.*f", Num.post, val);
+               orgnum = psprintf("%.*f", Num.post, val);
 
                if (*orgnum == '-')
                {                                               /* < 0 */
index fc3e10c7509540d565b5fa58a47d935955993fa6..4116c2d66ac4309dc11c3e1eefd8820ec7f53308 100644 (file)
@@ -10528,7 +10528,7 @@ check_cluster_name(char **newval, void **extra, GucSource source)
 static const char *
 show_unix_socket_permissions(void)
 {
-       static char buf[8];
+       static char buf[12];
 
        snprintf(buf, sizeof(buf), "%04o", Unix_socket_permissions);
        return buf;
@@ -10537,7 +10537,7 @@ show_unix_socket_permissions(void)
 static const char *
 show_log_file_mode(void)
 {
-       static char buf[8];
+       static char buf[12];
 
        snprintf(buf, sizeof(buf), "%04o", Log_file_mode);
        return buf;
index 65eba7d42fdaffa8a4084ffccaf5015baa91361a..feee359169933aa9728183b085f9a423aa2e5bf3 100644 (file)
@@ -1009,12 +1009,12 @@ static char *
 pretty_wal_size(int segment_count)
 {
        int                     sz = wal_segment_size_mb * segment_count;
-       char       *result = pg_malloc(11);
+       char       *result = pg_malloc(14);
 
        if ((sz % 1024) == 0)
-               snprintf(result, 11, "%dGB", sz / 1024);
+               snprintf(result, 14, "%dGB", sz / 1024);
        else
-               snprintf(result, 11, "%dMB", sz);
+               snprintf(result, 14, "%dMB", sz);
 
        return result;
 }
index fc233a608f395c3acedc2de0be25677465c1b07f..83c976eaf71a04dd6b4f6df1ffa2d349058f8f64 100644 (file)
@@ -1532,7 +1532,7 @@ SetOutput(ArchiveHandle *AH, const char *filename, int compression)
 #ifdef HAVE_LIBZ
        if (compression != 0)
        {
-               char            fmode[10];
+               char            fmode[14];
 
                /* Don't use PG_BINARY_x since this is zlib */
                sprintf(fmode, "wb%d", compression);
index ef9f7145b13640cf78793bcbaf00ce0ee40cc964..007be1298fbd2afb7c5a569c83fb46362c05e642 100644 (file)
@@ -335,7 +335,7 @@ tarOpen(ArchiveHandle *AH, const char *filename, char mode)
        TAR_MEMBER *tm;
 
 #ifdef HAVE_LIBZ
-       char            fmode[10];
+       char            fmode[14];
 #endif
 
        if (mode == 'r')
index 5c07dd9ca5ea629d778ed8cc90b4cf483fd8682e..29d69de4d13cfd43116747fee9907d3d3eb4ccec 100644 (file)
@@ -3591,7 +3591,7 @@ parseQuery(Command *cmd)
        p = sql;
        while ((p = strchr(p, ':')) != NULL)
        {
-               char            var[12];
+               char            var[13];
                char       *name;
                int                     eaten;
 
@@ -5432,7 +5432,7 @@ threadRun(void *arg)
                                                        sqlat,
                                                        lag,
                                                        stdev;
-                               char            tbuf[64];
+                               char            tbuf[315];
 
                                /*
                                 * Add up the statistics of all threads.
index 0c04529f479bedd8cc9a0a5801e615d1e0119483..a8753df8d161cc5938d5a51e2e6ce077f75eb293 100644 (file)
@@ -82,7 +82,7 @@ typedef enum
 
 #define BGW_DEFAULT_RESTART_INTERVAL   60
 #define BGW_NEVER_RESTART                              -1
-#define BGW_MAXLEN                                             64
+#define BGW_MAXLEN                                             96
 #define BGW_EXTRALEN                                   128
 
 typedef struct BackgroundWorker
index cade4e157cbd04e3e27fef56f73aa6feccc0712d..127122563c2cd50864c07501a0f558385b4bc438 100644 (file)
@@ -1436,7 +1436,7 @@ PQsslAttribute(PGconn *conn, const char *attribute_name)
 
        if (strcmp(attribute_name, "key_bits") == 0)
        {
-               static char sslbits_str[10];
+               static char sslbits_str[12];
                int                     sslbits;
 
                SSL_get_cipher_bits(conn->ssl, &sslbits);
index 11411f35ab3ba63eef1264d58030bd85d25f93dd..865071bc3bdc89c10c3f98233b59a6baa1feeb64 100644 (file)
@@ -1456,7 +1456,7 @@ compile_pltcl_function(Oid fn_oid, Oid tgreloid,
                Datum           prosrcdatum;
                bool            isnull;
                char       *proc_source;
-               char            buf[32];
+               char            buf[48];
                Tcl_Interp *interp;
                int                     i;
                int                     tcl_rc;