public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Christian Biesinger <cbiesinger@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Use ctime_r and localtime_r for threadsafety
Date: Fri, 15 Nov 2019 19:51:00 -0000	[thread overview]
Message-ID: <20191115195134.35307.qmail@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=53fea9c7e6d4993088016a16be56098fd819cebc

commit 53fea9c7e6d4993088016a16be56098fd819cebc
Author: Christian Biesinger <cbiesinger@google.com>
Date:   Thu Oct 31 16:02:41 2019 -0500

    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  <cbiesinger@google.com>
    
    	* 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

Diff:
---
 gdb/ChangeLog          | 7 +++++++
 gdb/maint.c            | 5 +++--
 gdb/nat/linux-osdata.c | 6 +++++-
 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  <cbiesinger@google.com>
 
+	* 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  <cbiesinger@google.com>
+
 	* 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 @@ scoped_command_stats::print_time (const char *msg)
   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_from_time_t (char *time, int maxlen, TIME_T seconds)
     {
       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';
     }
 }


                 reply	other threads:[~2019-11-15 19:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20191115195134.35307.qmail@sourceware.org \
    --to=cbiesinger@sourceware.org \
    --cc=gdb-cvs@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).