diff --git a/bin/dig/dig.c b/bin/dig/dig.c index 706299e75a..996cbb9495 100644 --- a/bin/dig/dig.c +++ b/bin/dig/dig.c @@ -277,11 +277,7 @@ received(unsigned 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 #ifdef WIN32 /* diff --git a/bin/dnssec/dnssec-settime.c b/bin/dnssec/dnssec-settime.c index 62ef8422c7..e128ee7b4b 100644 --- a/bin/dnssec/dnssec-settime.c +++ b/bin/dnssec/dnssec-settime.c @@ -121,15 +121,16 @@ printtime(dst_key_t *key, int type, const char *tag, bool epoch, } else if (epoch) { fprintf(stream, "%d\n", (int) when); } else { - time_t now = (time_t)when; -#ifdef _MSC_VER - struct tm *tm = localtime(&now); /* Thread specific. */ -#else + time_t now = when; struct tm t, *tm = localtime_r(&now, &t); -#endif unsigned int flen; char timebuf[80]; + if (tm == NULL) { + fprintf(stream, "INVALID\n"); + return; + } + flen = strftime(timebuf, sizeof(timebuf), "%a %b %e %H:%M:%S %Y", tm); INSIST(flen > 0U && flen < sizeof(timebuf)); diff --git a/bin/dnssec/dnssec-signzone.c b/bin/dnssec/dnssec-signzone.c index 8d93be83fa..cd235b2e8f 100644 --- a/bin/dnssec/dnssec-signzone.c +++ b/bin/dnssec/dnssec-signzone.c @@ -2955,16 +2955,13 @@ writeset(const char *prefix, dns_rdatatype_t type) { static void print_time(FILE *fp) { 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) + if (tm == NULL || outputformat != dns_masterformat_text) { return; + } flen = strftime(timebuf, sizeof(timebuf), "%a %b %e %H:%M:%S %Y", tm); INSIST(flen > 0U && flen < sizeof(timebuf)); diff --git a/lib/dns/gen-win32.h b/lib/dns/gen-win32.h index 4791032650..18a98950c3 100644 --- a/lib/dns/gen-win32.h +++ b/lib/dns/gen-win32.h @@ -62,6 +62,7 @@ #include #include #include +#include #include #include @@ -268,6 +269,16 @@ end_directory(isc_dir_t *dir) { FindClose(dir->handle); } +inline struct tm * +gmtime_r(const time_t *clock, struct tm *result) { + errno_t ret = gmtime_s(result, clock); + if (ret != 0) { + errno = ret; + return (NULL); + } + return (result); +} + ISC_LANG_ENDDECLS #endif /* DNS_GEN_WIN32_H */ diff --git a/lib/dns/gen.c b/lib/dns/gen.c index 589cd20526..fe6fef0de2 100644 --- a/lib/dns/gen.c +++ b/lib/dns/gen.c @@ -689,11 +689,7 @@ main(int argc, char **argv) { } if (now != -1) { -#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", diff --git a/lib/dns/update.c b/lib/dns/update.c index f735642991..085cc5509c 100644 --- a/lib/dns/update.c +++ b/lib/dns/update.c @@ -2082,16 +2082,12 @@ 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); + struct tm t, *tm = localtime_r(&when, &t); + if (tm == NULL) { + return (0); + } + return (((tm->tm_year + 1900) * 10000) + ((tm->tm_mon + 1) * 100) + + tm->tm_mday); } uint32_t diff --git a/lib/isc/win32/include/isc/time.h b/lib/isc/win32/include/isc/time.h index 486f527c60..8494736fea 100644 --- a/lib/isc/win32/include/isc/time.h +++ b/lib/isc/win32/include/isc/time.h @@ -13,6 +13,7 @@ #ifndef ISC_TIME_H #define ISC_TIME_H 1 +#include #include #include #include @@ -20,6 +21,30 @@ #include #include +/*** + *** POSIX Shims + ***/ + +inline struct tm * +gmtime_r(const time_t *clock, struct tm *result) { + errno_t ret = gmtime_s(result, clock); + if (ret != 0) { + errno = ret; + return (NULL); + } + return (result); +} + +inline struct tm * +localtime_r(const time_t *clock, struct tm *result) { + errno_t ret = localtime_s(result, clock); + if (ret != 0) { + errno = ret; + return (NULL); + } + return (result); +} + /*** *** Intervals ***/