Fix portability and safety issues in pqTraceFormatTimestamp.
authorTom Lane <[email protected]>
Wed, 31 Mar 2021 21:00:30 +0000 (17:00 -0400)
committerTom Lane <[email protected]>
Wed, 31 Mar 2021 21:00:30 +0000 (17:00 -0400)
Remove confusion between time_t and pg_time_t; neither
gettimeofday() nor localtime() deal in the latter.
libpq indeed has no business using <pgtime.h> at all.

Use snprintf not sprintf, to ensure we can't overrun the
supplied buffer.  (Unlikely, but let's be safe.)

Per buildfarm.

src/interfaces/libpq/fe-trace.c

index 0bfae12a45025c1b48911a7a1e693e810a093bbb..5faeee745190575c8542414a68080d8785022738 100644 (file)
@@ -26,7 +26,6 @@
 
 #include "libpq-fe.h"
 #include "libpq-int.h"
-#include "pgtime.h"
 #include "port/pg_bswap.h"
 
 /* Enable tracing */
@@ -81,16 +80,14 @@ static void
 pqTraceFormatTimestamp(char *timestr, size_t ts_len)
 {
    struct timeval tval;
-   pg_time_t   stamp_time;
 
    gettimeofday(&tval, NULL);
-   stamp_time = (pg_time_t) tval.tv_sec;
-
    strftime(timestr, ts_len,
             "%Y-%m-%d %H:%M:%S",
-            localtime(&stamp_time));
+            localtime(&tval.tv_sec));
    /* append microseconds */
-   sprintf(timestr + strlen(timestr), ".%06d", (int) (tval.tv_usec));
+   snprintf(timestr + strlen(timestr), ts_len - strlen(timestr),
+            ".%06u", (unsigned int) (tval.tv_usec));
 }
 
 /*