From: Evan Hunt Date: Tue, 10 Mar 2020 01:52:33 +0000 (-0700) Subject: replace unsafe ctime() and gmtime() function calls X-Git-Tag: v9.11.18~21^2~2 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=c16c095b32d57a484abd2a0918ef36f65c64cbbc;p=thirdparty%2Fbind9.git replace unsafe ctime() and gmtime() function calls This silences LGTM warnings that these functions are not thread-safe. (cherry picked from commit 5703f704273955f6c54627b6e05d8eb009b81337) --- diff --git a/bin/dnssec/dnssec-settime.c b/bin/dnssec/dnssec-settime.c index 7afcaee0965..62ef8422c74 100644 --- a/bin/dnssec/dnssec-settime.c +++ b/bin/dnssec/dnssec-settime.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -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); } } diff --git a/bin/dnssec/dnssec-signzone.c b/bin/dnssec/dnssec-signzone.c index 319a8050bd4..8d93be83fa3 100644 --- a/bin/dnssec/dnssec-signzone.c +++ b/bin/dnssec/dnssec-signzone.c @@ -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(¤ttime); /* Thread specific. */ +#else + struct tm t, *tm = localtime_r(¤ttime, &t); +#endif + unsigned int flen; + char timebuf[80]; if (outputformat != dns_masterformat_text) return; - currenttime = time(NULL); - fprintf(fp, "; File written on %s", ctime(¤ttime)); + 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 diff --git a/lib/dns/gen.c b/lib/dns/gen.c index 5fbdc3bfd3f..589cd205265 100644 --- a/lib/dns/gen.c +++ b/lib/dns/gen.c @@ -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);