summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2021-12-01 13:42:50 +0100
committerMark Wielaard <mark@klomp.org>2021-12-03 13:29:21 +0100
commitb4c0791d3e2ad29ee7111e8df1200bc08d6e4671 (patch)
tree1e1fb6d947c09ce05710c0e1e51d14ecb928c5ea
parenta4f766fa0f77a450a41bee1f8f8948306dfa3695 (diff)
downloadelfutils-b4c0791d3e2ad29ee7111e8df1200bc08d6e4671.tar.gz
debuginfod: Use gmtime_r instead of gmtime to avoid data race
Since we are multi-threaded using gmtime might cause a data race because gmtime reuses a global struct to write data into. Make sure that each thread uses their own struct tm and use gmtime_r instead. Signed-off-by: Mark Wielaard <mark@klomp.org>
-rw-r--r--debuginfod/ChangeLog5
-rw-r--r--debuginfod/debuginfod.cxx15
2 files changed, 14 insertions, 6 deletions
diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index 822bd637..625dead0 100644
--- a/debuginfod/ChangeLog
+++ b/debuginfod/ChangeLog
@@ -1,3 +1,8 @@
+2021-12-01 Mark Wielaard <mark@klomp.org>
+
+ * debuginfod-client.c (timestamp): Use gmtime_r instead of gmtime.
+ (add_mhd_last_modified): Likewise.
+
2021-11-10 Érico N. Rolim <erico.erc@gmail.com>
* debuginfod.cxx: include "system.h" under 'extern "C"' block.
diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
index 764e7b94..0bbaae9f 100644
--- a/debuginfod/debuginfod.cxx
+++ b/debuginfod/debuginfod.cxx
@@ -852,10 +852,11 @@ timestamp (ostream &o)
char datebuf[80];
char *now2 = NULL;
time_t now_t = time(NULL);
- struct tm *now = gmtime (&now_t);
- if (now)
+ struct tm now;
+ struct tm *nowp = gmtime_r (&now_t, &now);
+ if (nowp)
{
- (void) strftime (datebuf, sizeof (datebuf), "%c", now);
+ (void) strftime (datebuf, sizeof (datebuf), "%c", nowp);
now2 = datebuf;
}
@@ -1070,11 +1071,13 @@ conninfo (struct MHD_Connection * conn)
static void
add_mhd_last_modified (struct MHD_Response *resp, time_t mtime)
{
- struct tm *now = gmtime (&mtime);
- if (now != NULL)
+ struct tm now;
+ struct tm *nowp = gmtime_r (&mtime, &now);
+ if (nowp != NULL)
{
char datebuf[80];
- size_t rc = strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %T GMT", now);
+ size_t rc = strftime (datebuf, sizeof (datebuf), "%a, %d %b %Y %T GMT",
+ nowp);
if (rc > 0 && rc < sizeof (datebuf))
(void) MHD_add_response_header (resp, "Last-Modified", datebuf);
}