]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
replace unsafe ctime() and gmtime() function calls
authorEvan Hunt <each@isc.org>
Tue, 10 Mar 2020 01:52:33 +0000 (18:52 -0700)
committerEvan Hunt <each@isc.org>
Tue, 17 Mar 2020 22:39:08 +0000 (15:39 -0700)
This silences LGTM warnings that these functions are not thread-safe.

(cherry picked from commit 5703f704273955f6c54627b6e05d8eb009b81337)

bin/dnssec/dnssec-settime.c
bin/dnssec/dnssec-signzone.c
lib/dns/gen.c

index 7afcaee096581b3eed89e72f38c16e2c47ef5274..62ef8422c74b7c24f3cf112e10e0b25432883068 100644 (file)
@@ -28,6 +28,7 @@
 #include <isc/mem.h>
 #include <isc/print.h>
 #include <isc/string.h>
+#include <isc/time.h>
 #include <isc/util.h>
 
 #include <dns/keyvalues.h>
@@ -109,7 +110,6 @@ printtime(dst_key_t *key, int type, const char *tag, bool epoch,
          FILE *stream)
 {
        isc_result_t result;
-       const char *output = NULL;
        isc_stdtime_t when;
 
        if (tag != NULL)
@@ -121,9 +121,19 @@ printtime(dst_key_t *key, int type, const char *tag, bool epoch,
        } else if (epoch) {
                fprintf(stream, "%d\n", (int) when);
        } else {
-               time_t timet = when;
-               output = ctime(&timet);
-               fprintf(stream, "%s", output);
+               time_t now = (time_t)when;
+#ifdef _MSC_VER
+               struct tm *tm = localtime(&now); /* Thread specific. */
+#else
+               struct tm t, *tm = localtime_r(&now, &t);
+#endif
+               unsigned int flen;
+               char timebuf[80];
+
+               flen = strftime(timebuf, sizeof(timebuf),
+                               "%a %b %e %H:%M:%S %Y", tm);
+               INSIST(flen > 0U && flen < sizeof(timebuf));
+               fprintf(stream, "%s\n", timebuf);
        }
 }
 
index 319a8050bd41c7078fd2d7d3ff3d8d934e2cf166..8d93be83fa30191ed77cea2c1ba1a3b2f591d403 100644 (file)
@@ -2954,13 +2954,21 @@ writeset(const char *prefix, dns_rdatatype_t type) {
 
 static void
 print_time(FILE *fp) {
-       time_t currenttime;
+       time_t currenttime = time(NULL);
+#ifdef _MSC_VER
+       struct tm *tm = localtime(&currenttime); /* Thread specific. */
+#else
+       struct tm t, *tm = localtime_r(&currenttime, &t);
+#endif
+       unsigned int flen;
+       char timebuf[80];
 
        if (outputformat != dns_masterformat_text)
                return;
 
-       currenttime = time(NULL);
-       fprintf(fp, "; File written on %s", ctime(&currenttime));
+       flen = strftime(timebuf, sizeof(timebuf), "%a %b %e %H:%M:%S %Y", tm);
+       INSIST(flen > 0U && flen < sizeof(timebuf));
+       fprintf(fp, "; File written on %s\n", timebuf);
 }
 
 static void
index 5fbdc3bfd3f96e7f69d4f6fd8f5cf8d168dc65b6..589cd205265c92c9291567dc93ac36afaa7c64ce 100644 (file)
@@ -689,7 +689,12 @@ main(int argc, char **argv) {
        }
 
        if (now != -1) {
-               struct tm *tm = gmtime(&now);
+#ifdef _MSC_VER
+               struct tm *tm = gmtime(&now); /* Thread specific. */
+#else
+               struct tm t, *tm = gmtime_r(&now, &t);
+#endif
+
                if (tm != NULL && tm->tm_year > 104) {
                        n = snprintf(year, sizeof(year), "-%d",
                                     tm->tm_year + 1900);