]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
4668. [bug] Use localtime_r and gmtime_r for thread safety.
authorMark Andrews <marka@isc.org>
Wed, 2 Aug 2017 22:42:27 +0000 (08:42 +1000)
committerMark Andrews <marka@isc.org>
Wed, 2 Aug 2017 22:42:27 +0000 (08:42 +1000)
                        [RT #45664]

CHANGES
bin/dig/dig.c
bin/tests/net/driver.c
lib/dns/update.c
lib/isc/unix/time.c
lib/tests/t_api.c

diff --git a/CHANGES b/CHANGES
index d6dee6a332d8d3d80338bf19b809ba65f7ab028a..d3de6fb3393db3644f3fed9652558ca028f2c519 100644 (file)
--- 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]
 
 4666.  [bug]           dnssec-keymgr: Domain names beginning with digits (0-9)
index e38d03618000ef84a4db7e484525bed0f3ea2e6b..92e0438cc370818989948eb5345545430104addf 100644 (file)
@@ -16,6 +16,7 @@
 #include <isc/app.h>
 #include <isc/netaddr.h>
 #include <isc/parseint.h>
+#include <isc/platform.h>
 #include <isc/print.h>
 #include <isc/string.h>
 #include <isc/task.h>
@@ -261,7 +262,12 @@ received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
                        printf(";; Query time: %ld msec\n", (long) 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);
index d6fe3f77a9d0eb9bb283851257f997412f4e6653..db9a54b167ca25b9cd19dcd80c579d1d61056343 100644 (file)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <time.h>
 
+#include <isc/platform.h>
 #include <isc/print.h>
 #include <isc/string.h>
 #include <isc/util.h>
 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);
 }
index bcbbd26b089d5e9fd29296c4d9d67c872e66a6cd..34243c99f094e151c7afe3c6e0d1c55e754486a4 100644 (file)
@@ -14,6 +14,7 @@
 #include <isc/magic.h>
 #include <isc/mem.h>
 #include <isc/netaddr.h>
+#include <isc/platform.h>
 #include <isc/print.h>
 #include <isc/serial.h>
 #include <isc/stats.h>
@@ -2023,7 +2024,13 @@ dns_update_signaturesinc(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db,
 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);
 }
index 725d49472d833a01fe556c72aebf379852581295..49aa967e9708f736a818f4e9de8b2a53f7955b80 100644 (file)
@@ -21,6 +21,7 @@
 #include <sys/time.h>  /* Required for struct timeval on some platforms. */
 
 #include <isc/log.h>
+#include <isc/platform.h>
 #include <isc/print.h>
 #include <isc/strerror.h>
 #include <isc/string.h>
@@ -376,11 +377,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,
@@ -395,6 +403,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);
 
@@ -402,7 +413,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);
 }
 
@@ -428,11 +444,18 @@ void
 isc_time_formatISO8601L(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:%S", localtime_r(&now, &tm));
+#else
        flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime(&now));
+#endif
        INSIST(flen < len);
 }
 
@@ -440,11 +463,18 @@ void
 isc_time_formatISO8601Lms(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:%S", localtime_r(&now, &tm));
+#else
        flen = strftime(buf, len, "%Y-%m-%dT%H:%M:%S", localtime(&now));
+#endif
        INSIST(flen < len);
        if (flen > 0U && len - flen >= 6) {
                snprintf(buf + flen, len - flen, ".%03u",
@@ -456,11 +486,18 @@ 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);
 }
 
@@ -468,11 +505,18 @@ void
 isc_time_formatISO8601ms(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);
        if (flen > 0U && len - flen >= 5) {
                flen -= 1; /* rewind one character (Z) */
@@ -486,11 +530,18 @@ isc_time_formatshorttimestamp(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%d%H%M%S", gmtime_r(&now, &tm));
+#else
        flen = strftime(buf, len, "%Y%m%d%H%M%S", gmtime(&now));
+#endif
        INSIST(flen < len);
        if (flen > 0U && len - flen >= 5) {
                flen -= 1; /* rewind one character (Z) */
index 5b7b03e25026bee28575c74e28fdd6cf6d789cbe..8280c299fab39ca744b18464ae08951a59e99df4 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <isc/boolean.h>
 #include <isc/commandline.h>
+#include <isc/platform.h>
 #include <isc/print.h>
 #include <isc/string.h>
 #include <isc/mem.h>
@@ -596,9 +597,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);
 }