Commit 5703f704 authored by Evan Hunt's avatar Evan Hunt

replace unsafe ctime() and gmtime() function calls

This silences LGTM warnings that these functions are not thread-safe.
parent 735be3b8
......@@ -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);
}
}
......
......@@ -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
......
......@@ -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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment