]> 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 20:28:15 +0000 (13:28 -0700)
This silences LGTM warnings that these functions are not thread-safe.

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

index f790777097b31bb493b6800eae904528fa52a8aa..041d1ca3025ee3eb523a4839ca7214a30d173fad 100644 (file)
@@ -25,6 +25,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>
@@ -114,7 +115,6 @@ usage(void) {
 static void
 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) {
@@ -127,9 +127,19 @@ printtime(dst_key_t *key, int type, const char *tag, bool epoch, FILE *stream) {
        } 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 50efba63a1f53c43cd1a46d5e73baa3dff311395..325af98c1e7965139100ecd8015f8b2af6b77a12 100644 (file)
@@ -3105,14 +3105,22 @@ 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 77cd75746b8624fcc6b2aaad4e0335f3cd7776a2..2e31c61177574a4db9238cfd640b082ac6777292 100644 (file)
@@ -700,7 +700,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);