public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] debuginfod: Use gmtime_r instead of gmtime to avoid data race
Date: Wed,  1 Dec 2021 13:50:26 +0100	[thread overview]
Message-ID: <20211201125026.13186-1-mark@klomp.org> (raw)

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>
---
 debuginfod/ChangeLog      |  5 +++++
 debuginfod/debuginfod.cxx | 15 +++++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/debuginfod/ChangeLog b/debuginfod/ChangeLog
index ae584b9b..83aaf4b7 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-12-01  Mark Wielaard  <mark@klomp.org>
 
 	* debuginfod-client.c (debuginfod_query_server): Free tmp_url on
diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
index 3b4591dd..112c6701 100644
--- a/debuginfod/debuginfod.cxx
+++ b/debuginfod/debuginfod.cxx
@@ -855,10 +855,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;
     }
 
@@ -1076,11 +1077,13 @@ conninfo (struct MHD_Connection * conn)
 static bool
 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))
         if (MHD_add_response_header (resp, "Last-Modified", datebuf) == MHD_NO)
 	  return false;
-- 
2.18.4


             reply	other threads:[~2021-12-01 12:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-01 12:50 Mark Wielaard [this message]
     [not found] ` <20211201132619.GE17988@redhat.com>
2021-12-03 12:32   ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211201125026.13186-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).