From patchwork Wed Dec 1 12:50:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 48353 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 905A73858033 for ; Wed, 1 Dec 2021 12:50:54 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id AB2D93858C60 for ; Wed, 1 Dec 2021 12:50:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AB2D93858C60 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from tarox.wildebeest.org (83-87-18-245.cable.dynamic.v4.ziggo.nl [83.87.18.245]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 2DB18300070C; Wed, 1 Dec 2021 13:50:46 +0100 (CET) Received: by tarox.wildebeest.org (Postfix, from userid 1000) id 6C2C0425A456; Wed, 1 Dec 2021 13:50:46 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] debuginfod: Use gmtime_r instead of gmtime to avoid data race Date: Wed, 1 Dec 2021 13:50:26 +0100 Message-Id: <20211201125026.13186-1-mark@klomp.org> X-Mailer: git-send-email 2.18.4 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Cc: Mark Wielaard Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" 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 --- 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 + + * debuginfod-client.c (timestamp): Use gmtime_r instead of gmtime. + (add_mhd_last_modified): Likewise. + 2021-12-01 Mark Wielaard * 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;