diff --git a/mongoose.c b/mongoose.c
index d68a1508..7fe04d48 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -1070,6 +1070,7 @@ static void printdirentry(struct mg_connection *c, const char *name,
char size[64], mod[64], path[MG_PATH_MAX];
int is_dir = S_ISDIR(stp->st_mode), n = 0;
const char *slash = is_dir ? "/" : "";
+ struct tm t;
if (is_dir) {
snprintf(size, sizeof(size), "%s", "[DIR]");
@@ -1084,7 +1085,7 @@ static void printdirentry(struct mg_connection *c, const char *name,
snprintf(size, sizeof(size), "%.1fG", (double) stp->st_size / 1073741824);
}
}
- strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
+ strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&stp->st_mtime, &t));
n = mg_url_encode(name, strlen(name), path, sizeof(path));
mg_printf(c,
"
| %s%s | "
@@ -1543,7 +1544,7 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) {
if (level <= max) {
char timebuf[21], buf[50] = "";
time_t t = time(NULL);
- struct tm *tm = gmtime(&t);
+ struct tm tmp, *tm = gmtime_r(&t, &tmp);
int n, tag;
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm);
tag = level == LL_ERROR ? 'E' : level == LL_INFO ? 'I' : ' ';
diff --git a/mongoose.h b/mongoose.h
index aa3af003..e85bf489 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -335,6 +335,17 @@ typedef int socklen_t;
#define MG_INT64_FMT "%I64d"
+// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
+static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
+ (void) tm;
+ return gmtime(t);
+}
+
+static __inline struct tm *localtime_r(time_t *t, struct tm *tm) {
+ (void) tm;
+ return localtime(t);
+}
+
#endif
diff --git a/src/arch_win32.h b/src/arch_win32.h
index 45d50fcc..70b10fea 100644
--- a/src/arch_win32.h
+++ b/src/arch_win32.h
@@ -67,4 +67,15 @@ typedef int socklen_t;
#define MG_INT64_FMT "%I64d"
+// https://lgtm.com/rules/2154840805/ -gmtime, localtime, ctime and asctime
+static __inline struct tm *gmtime_r(time_t *t, struct tm *tm) {
+ (void) tm;
+ return gmtime(t);
+}
+
+static __inline struct tm *localtime_r(time_t *t, struct tm *tm) {
+ (void) tm;
+ return localtime(t);
+}
+
#endif
diff --git a/src/http.c b/src/http.c
index 3e71d49d..df5ff515 100644
--- a/src/http.c
+++ b/src/http.c
@@ -648,6 +648,7 @@ static void printdirentry(struct mg_connection *c, const char *name,
char size[64], mod[64], path[MG_PATH_MAX];
int is_dir = S_ISDIR(stp->st_mode), n = 0;
const char *slash = is_dir ? "/" : "";
+ struct tm t;
if (is_dir) {
snprintf(size, sizeof(size), "%s", "[DIR]");
@@ -662,7 +663,7 @@ static void printdirentry(struct mg_connection *c, const char *name,
snprintf(size, sizeof(size), "%.1fG", (double) stp->st_size / 1073741824);
}
}
- strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
+ strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&stp->st_mtime, &t));
n = mg_url_encode(name, strlen(name), path, sizeof(path));
mg_printf(c,
"
| %s%s | "
diff --git a/src/log.c b/src/log.c
index 6ff3e08a..89e04f9a 100644
--- a/src/log.c
+++ b/src/log.c
@@ -35,7 +35,7 @@ bool mg_log_prefix(int level, const char *file, int line, const char *fname) {
if (level <= max) {
char timebuf[21], buf[50] = "";
time_t t = time(NULL);
- struct tm *tm = gmtime(&t);
+ struct tm tmp, *tm = gmtime_r(&t, &tmp);
int n, tag;
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm);
tag = level == LL_ERROR ? 'E' : level == LL_INFO ? 'I' : ' ';