4668. [bug] Use localtime_r and gmtime_r for thread safety.

[RT #45664]
This commit is contained in:
Mark Andrews
2017-08-03 08:42:27 +10:00
parent b9e4835f4b
commit 2019cf29e2
6 changed files with 85 additions and 6 deletions

View 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>
@@ -27,19 +28,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);
}