diff --git a/CHANGES b/CHANGES index de9aa69cb6..96abc01cf5 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +4668. [bug] Use localtime_r and gmtime_r for thread safety. + [RT #45664] + 4667. [cleanup] Refactor RDATA unit tests. [RT #45610] 4665. [protocol] Added support for ED25519 and ED448 DNSSEC signing diff --git a/bin/dig/dig.c b/bin/dig/dig.c index 3bc57ad442..56ddee0d05 100644 --- a/bin/dig/dig.c +++ b/bin/dig/dig.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -263,7 +264,12 @@ received(int bytes, isc_sockaddr_t *from, dig_query_t *query) { printf(";; Query time: %ld msec\n", (long int)diff/1000); printf(";; SERVER: %s(%s)\n", fromtext, query->servname); time(&tnow); +#if defined(ISC_PLATFORM_USETHREADS) && !defined(WIN32) + (void)localtime_r(&tnow, &tmnow); +#else tmnow = *localtime(&tnow); +#endif + if (strftime(time_str, sizeof(time_str), "%a %b %d %H:%M:%S %Z %Y", &tmnow) > 0U) printf(";; WHEN: %s\n", time_str); diff --git a/bin/tests/net/driver.c b/bin/tests/net/driver.c index f57dc7976a..9a906000d8 100644 --- a/bin/tests/net/driver.c +++ b/bin/tests/net/driver.c @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -36,19 +37,22 @@ const char *gettime(void); const char *test_result_totext(test_result_t); -/* - * Not thread safe. - */ const char * gettime(void) { static char now[512]; time_t t; +#if defined(ISC_PLATFORM_USETHREADS) && !defined(WIN32) + struct tm tm; +#endif (void)time(&t); - strftime(now, sizeof(now) - 1, - "%A %d %B %H:%M:%S %Y", - localtime(&t)); +#if defined(ISC_PLATFORM_USETHREADS) && !defined(WIN32) + strftime(now, sizeof(now) - 1, "%A %d %B %H:%M:%S %Y", + localtime_r(&t, &tm)); +#else + strftime(now, sizeof(now) - 1, "%A %d %B %H:%M:%S %Y", localtime(&t)); +#endif return (now); } diff --git a/lib/dns/update.c b/lib/dns/update.c index 10910ca507..9e44f0052b 100644 --- a/lib/dns/update.c +++ b/lib/dns/update.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -2027,6 +2028,23 @@ dns_update_signaturesinc(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db, return (result); } +<<<<<<< HEAD +======= +static isc_stdtime_t +epoch_to_yyyymmdd(time_t when) { + struct tm *tm; + +#if defined(ISC_PLATFORM_USETHREADS) && !defined(WIN32) + struct tm tm0; + tm = localtime_r(&when, &tm0); +#else + tm = localtime(&when); +#endif + return (((tm->tm_year + 1900) * 10000) + + ((tm->tm_mon + 1) * 100) + tm->tm_mday); +} + +>>>>>>> 4162d3b36d... 4668. [bug] Use localtime_r and gmtime_r for thread safety. isc_uint32_t dns_update_soaserial(isc_uint32_t serial, dns_updatemethod_t method) { isc_stdtime_t now; diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c index 400bbf8f3b..d10051a124 100644 --- a/lib/isc/unix/time.c +++ b/lib/isc/unix/time.c @@ -30,6 +30,7 @@ #include /* Required for struct timeval on some platforms. */ #include +#include #include #include #include @@ -384,11 +385,18 @@ void isc_time_formattimestamp(const isc_time_t *t, char *buf, unsigned int len) { time_t now; unsigned int flen; +#ifdef ISC_PLATFORM_USETHREADS + struct tm tm; +#endif REQUIRE(len > 0); now = (time_t) t->seconds; +#ifdef ISC_PLATFORM_USETHREADS + flen = strftime(buf, len, "%d-%b-%Y %X", localtime_r(&now, &tm)); +#else flen = strftime(buf, len, "%d-%b-%Y %X", localtime(&now)); +#endif INSIST(flen < len); if (flen != 0) snprintf(buf + flen, len - flen, @@ -401,6 +409,9 @@ void isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) { time_t now; unsigned int flen; +#ifdef ISC_PLATFORM_USETHREADS + struct tm tm; +#endif REQUIRE(len > 0); @@ -408,7 +419,12 @@ isc_time_formathttptimestamp(const isc_time_t *t, char *buf, unsigned int len) { * 5 spaces, 1 comma, 3 GMT, 2 %d, 4 %Y, 8 %H:%M:%S, 3+ %a, 3+ %b (29+) */ now = (time_t)t->seconds; +#ifdef ISC_PLATFORM_USETHREADS + flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", + gmtime_r(&now, &tm)); +#else flen = strftime(buf, len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(&now)); +#endif INSIST(flen < len); } @@ -434,10 +450,17 @@ void isc_time_formatISO8601(const isc_time_t *t, char *buf, unsigned int len) { time_t now; unsigned int flen; +#ifdef ISC_PLATFORM_USETHREADS + struct tm tm; +#endif REQUIRE(len > 0); now = (time_t)t->seconds; +#ifdef ISC_PLATFORM_USETHREADS + flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime_r(&now, &tm)); +#else flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%SZ", gmtime(&now)); +#endif INSIST(flen < len); } diff --git a/lib/tests/t_api.c b/lib/tests/t_api.c index bfc68ee080..f01fbffb4a 100644 --- a/lib/tests/t_api.c +++ b/lib/tests/t_api.c @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -605,9 +606,16 @@ t_getdate(char *buf, size_t buflen) { size_t n; time_t t; struct tm *p; +#if defined(ISC_PLATFORM_USETHREADS) && !defined(WIN32) + struct tm tm; +#endif t = time(NULL); +#if defined(ISC_PLATFORM_USETHREADS) && !defined(WIN32) + p = localtime_r(&t, &tm); +#else p = localtime(&t); +#endif n = strftime(buf, buflen - 1, "%A %d %B %H:%M:%S %Y\n", p); return(n != 0U ? buf : NULL); }