[httpd/logger] Use gmtime_r/localtime_r instead of gmtime/localtime

gmtime and localtime are not thread safe

credit lgtm.com
This commit is contained in:
ejurgensen 2020-08-10 22:19:34 +02:00
parent fada4c0ee7
commit 5e6b5fbe8a
2 changed files with 4 additions and 2 deletions

View File

@ -325,11 +325,12 @@ httpd_request_not_modified_since(struct evhttp_request *req, time_t mtime)
struct evkeyvalq *output_headers;
char last_modified[1000];
const char *modified_since;
struct tm timebuf;
input_headers = evhttp_request_get_input_headers(req);
modified_since = evhttp_find_header(input_headers, "If-Modified-Since");
strftime(last_modified, sizeof(last_modified), "%a, %d %b %Y %H:%M:%S %Z", gmtime(&mtime));
strftime(last_modified, sizeof(last_modified), "%a, %d %b %Y %H:%M:%S %Z", gmtime_r(&mtime, &timebuf));
// Return not modified, if given timestamp matches "If-Modified-Since" request header
if (modified_since && (strcasecmp(last_modified, modified_since) == 0))

View File

@ -117,6 +117,7 @@ vlogger_writer(int severity, int domain, const char *fmt, va_list args)
char content[2048];
char stamp[32];
time_t t;
struct tm timebuf;
int ret;
va_copy(ap, args);
@ -136,7 +137,7 @@ vlogger_writer(int severity, int domain, const char *fmt, va_list args)
if (logfile)
{
t = time(NULL);
ret = strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&t));
ret = strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime_r(&t, &timebuf));
if (ret == 0)
stamp[0] = '\0';