From patchwork Fri Nov 15 19:52:06 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Simon Marchi (Code Review)" X-Patchwork-Id: 35956 Received: (qmail 35987 invoked by alias); 15 Nov 2019 19:52:15 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 35977 invoked by uid 89); 15 Nov 2019 19:52:15 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.1 spammy=HX-Languages-Length:2716 X-HELO: mx1.osci.io Received: from polly.osci.io (HELO mx1.osci.io) (8.43.85.229) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 15 Nov 2019 19:52:14 +0000 Received: by mx1.osci.io (Postfix, from userid 994) id C811A200BC; Fri, 15 Nov 2019 14:52:12 -0500 (EST) Received: from gnutoolchain-gerrit.osci.io (gnutoolchain-gerrit.osci.io [IPv6:2620:52:3:1:5054:ff:fe06:16ca]) by mx1.osci.io (Postfix) with ESMTP id 10B33200BC; Fri, 15 Nov 2019 14:52:07 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by gnutoolchain-gerrit.osci.io (Postfix) with ESMTP id F344A28171; Fri, 15 Nov 2019 14:52:06 -0500 (EST) X-Gerrit-PatchSet: 6 Date: Fri, 15 Nov 2019 14:52:06 -0500 From: "Sourceware to Gerrit sync (Code Review)" To: Christian Biesinger , gdb-patches@sourceware.org Cc: Kevin Buettner Auto-Submitted: auto-generated X-Gerrit-MessageType: merged Subject: [pushed] Use ctime_r and localtime_r for threadsafety X-Gerrit-Change-Id: I329bbdc39d5b576f51859ba00f1617e024c30cbd X-Gerrit-Change-Number: 475 X-Gerrit-ChangeURL: X-Gerrit-Commit: 53fea9c7e6d4993088016a16be56098fd819cebc In-Reply-To: References: Reply-To: noreply@gnutoolchain-gerrit.osci.io, cbiesinger@google.com, kevinb@redhat.com, gdb-patches@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3-76-gf8b6da0ab5 Message-Id: <20191115195206.F344A28171@gnutoolchain-gerrit.osci.io> Sourceware to Gerrit sync has submitted this change. Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/475 ...................................................................... Use ctime_r and localtime_r for threadsafety To make these calls threadsafe. localtime_r is provided by gnulib if necessary, and for ctime_r we can just use it because it is in a linux- specific file. gdb/ChangeLog: 2019-11-15 Christian Biesinger * maint.c (scoped_command_stats::print_time): Use localtime_r instead of localtime (provided through gnulib if necessary). * nat/linux-osdata.c (time_from_time_t): Use ctime_r instead of ctime. Change-Id: I329bbdc39d5b576f51859ba00f1617e024c30cbd --- M gdb/ChangeLog M gdb/maint.c M gdb/nat/linux-osdata.c 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index f31552b..f727aa4 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,12 @@ 2019-11-15 Christian Biesinger + * maint.c (scoped_command_stats::print_time): Use localtime_r + instead of localtime (provided through gnulib if necessary). + * nat/linux-osdata.c (time_from_time_t): Use ctime_r instead + of ctime. + +2019-11-15 Christian Biesinger + * gdbsupport/common-defs.h: Include time.h before pathmax.h to avoid compile errors. diff --git a/gdb/maint.c b/gdb/maint.c index ec9f4ab..a253584 100644 --- a/gdb/maint.c +++ b/gdb/maint.c @@ -1039,10 +1039,11 @@ auto millis = ticks % 1000; std::time_t as_time = system_clock::to_time_t (now); - struct tm *tm = localtime (&as_time); + struct tm tm; + localtime_r (&as_time, &tm); char out[100]; - strftime (out, sizeof (out), "%F %H:%M:%S", tm); + strftime (out, sizeof (out), "%F %H:%M:%S", &tm); printf_unfiltered ("%s.%03d - %s\n", out, (int) millis, msg); } diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index ca6acd3..d82c062 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -916,7 +916,11 @@ { time_t t = (time_t) seconds; - strncpy (time, ctime (&t), maxlen); + /* Per the ctime_r manpage, this buffer needs to be at least 26 + characters long. */ + char buf[30]; + const char *time_str = ctime_r (&t, buf); + strncpy (time, time_str, maxlen); time[maxlen - 1] = '\0'; } }