diff mbox

[review,v2] Use ctime_r and localtime_r if available

Message ID 20191103025430.BD90720AF6@gnutoolchain-gerrit.osci.io
State New
Headers show

Commit Message

Simon Marchi (Code Review) Nov. 3, 2019, 2:54 a.m. UTC
Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/475
......................................................................

Use ctime_r and localtime_r if available

To make these calls threadsafe.

gdb/ChangeLog:

2019-10-31  Christian Biesinger  <cbiesinger@google.com>

	* config.in: Regenerate.
	* configure: Regenerate.
	* gdbsupport/common.m4: Check for ctime_r.
	* configure.ac: Check for localtime_r.
	* maint.c (scoped_command_stats::print_time): Use localtime_r
	when available instead of localtime.
	* nat/linux-osdata.c (time_from_time_t): Use ctime_r if available
	instead of ctime.

gdb/gdbserver/ChangeLog:

2019-10-31  Christian Biesinger  <cbiesinger@google.com>

	* config.in: Regenerate.
	* configure: Regenerate.

Change-Id: I329bbdc39d5b576f51859ba00f1617e024c30cbd
---
M gdb/config.in
M gdb/configure
M gdb/configure.ac
M gdb/gdbserver/config.in
M gdb/gdbserver/configure
M gdb/gdbsupport/common.m4
M gdb/maint.c
M gdb/nat/linux-osdata.c
8 files changed, 26 insertions(+), 6 deletions(-)

Comments

Andreas Schwab Nov. 3, 2019, 7:20 a.m. UTC | #1
On Nov 02 2019, Christian Biesinger (Code Review) wrote:

> diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
> index 67f9f3a..e0bad81 100644
> --- a/gdb/nat/linux-osdata.c
> +++ b/gdb/nat/linux-osdata.c
> @@ -912,7 +912,13 @@
>      {
>        time_t t = (time_t) seconds;
>  
> -      strncpy (time, ctime (&t), maxlen);
> +      char buf[30];
> +#ifdef HAVE_CTIME_R
> +      const char *time_str = ctime_r (&t, buf);
> +#else
> +      const char *time_str = ctime (&t);
> +#endif

buf is unused if !HAVE_CTIME_R.

Note that both ctime and ctime_r are obsolescent and should be replaced
by strftime.  gdb currently doesn't setlocale LC_TIME, but if it does it
would make the use of these functions undefined.

Andreas.
Doug Evans via gdb-patches Nov. 3, 2019, 8:08 p.m. UTC | #2
On Sun, Nov 3, 2019 at 2:20 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>
> On Nov 02 2019, Christian Biesinger (Code Review) wrote:
>
> > diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
> > index 67f9f3a..e0bad81 100644
> > --- a/gdb/nat/linux-osdata.c
> > +++ b/gdb/nat/linux-osdata.c
> > @@ -912,7 +912,13 @@
> >      {
> >        time_t t = (time_t) seconds;
> >
> > -      strncpy (time, ctime (&t), maxlen);
> > +      char buf[30];
> > +#ifdef HAVE_CTIME_R
> > +      const char *time_str = ctime_r (&t, buf);
> > +#else
> > +      const char *time_str = ctime (&t);
> > +#endif
>
> buf is unused if !HAVE_CTIME_R.

Thanks, fixed.

> Note that both ctime and ctime_r are obsolescent and should be replaced
> by strftime.  gdb currently doesn't setlocale LC_TIME, but if it does it
> would make the use of these functions undefined.

I read through the manpage for ctime/ctime_r and I don't see any note
that the behavior is undefined when setlocale is called, or that it is
obsolete. For completeness, I also looked at the Solaris manpage and
it does not say that either. Instead, it will use a locale-independent
format, which seems desirable here since it is serialized into XML.

Christian
diff mbox

Patch

diff --git a/gdb/config.in b/gdb/config.in
index 5a21fca..9b29196 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -78,6 +78,9 @@ 
 /* Define to 1 if you have the `btowc' function. */
 #undef HAVE_BTOWC
 
+/* Define to 1 if you have the `ctime_r' function. */
+#undef HAVE_CTIME_R
+
 /* Define to 1 if you have the <cursesX.h> header file. */
 #undef HAVE_CURSESX_H
 
@@ -261,6 +264,9 @@ 
 /* Define to 1 if you have the <locale.h> header file. */
 #undef HAVE_LOCALE_H
 
+/* Define to 1 if you have the `localtime_r' function. */
+#undef HAVE_LOCALTIME_R
+
 /* Define to 1 if the compiler supports long double. */
 #undef HAVE_LONG_DOUBLE
 
diff --git a/gdb/configure b/gdb/configure
index 512f016..f5444df 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -13073,7 +13073,7 @@ 
 		sigaction sigsetmask socketpair \
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
 		setrlimit getrlimit posix_madvise waitpid \
-		ptrace64 sigaltstack setns use_default_colors
+		ptrace64 sigaltstack setns use_default_colors localtime_r
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
@@ -13480,7 +13480,7 @@ 
 
 
   for ac_func in fdwalk getrlimit pipe pipe2 socketpair sigaction \
-		  sigprocmask strerror_r
+		  sigprocmask strerror_r ctime_r
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 354bb7b..3d6f763 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1318,7 +1318,7 @@ 
 		sigaction sigsetmask socketpair \
 		ttrace wborder wresize setlocale iconvlist libiconvlist btowc \
 		setrlimit getrlimit posix_madvise waitpid \
-		ptrace64 sigaltstack setns use_default_colors])
+		ptrace64 sigaltstack setns use_default_colors localtime_r])
 AM_LANGINFO_CODESET
 GDB_AC_COMMON
 
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 2984281..547a942 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -21,6 +21,9 @@ 
 /* Define to 1 if you have the <arpa/inet.h> header file. */
 #undef HAVE_ARPA_INET_H
 
+/* Define to 1 if you have the `ctime_r' function. */
+#undef HAVE_CTIME_R
+
 /* define if the compiler supports basic C++11 syntax */
 #undef HAVE_CXX11
 
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index 3f1f1c1..352836f 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -6822,7 +6822,7 @@ 
 
 
   for ac_func in fdwalk getrlimit pipe pipe2 socketpair sigaction \
-		  sigprocmask strerror_r
+		  sigprocmask strerror_r ctime_r
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
diff --git a/gdb/gdbsupport/common.m4 b/gdb/gdbsupport/common.m4
index 2e44cf4..81f3fec 100644
--- a/gdb/gdbsupport/common.m4
+++ b/gdb/gdbsupport/common.m4
@@ -33,7 +33,7 @@ 
 		   dlfcn.h)
 
   AC_CHECK_FUNCS([fdwalk getrlimit pipe pipe2 socketpair sigaction \
-		  sigprocmask strerror_r])
+		  sigprocmask strerror_r ctime_r])
 
   AC_CHECK_DECLS([strerror, strstr])
 
diff --git a/gdb/maint.c b/gdb/maint.c
index ec9f4ab..36ab716 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -1039,7 +1039,12 @@ 
   auto millis = ticks % 1000;
 
   std::time_t as_time = system_clock::to_time_t (now);
+#ifdef HAVE_LOCALTIME_R
+  struct tm buf;
+  struct tm *tm = localtime_r (&as_time, &buf);
+#else
   struct tm *tm = localtime (&as_time);
+#endif
 
   char out[100];
   strftime (out, sizeof (out), "%F %H:%M:%S", tm);
diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
index 67f9f3a..e0bad81 100644
--- a/gdb/nat/linux-osdata.c
+++ b/gdb/nat/linux-osdata.c
@@ -912,7 +912,13 @@ 
     {
       time_t t = (time_t) seconds;
 
-      strncpy (time, ctime (&t), maxlen);
+      char buf[30];
+#ifdef HAVE_CTIME_R
+      const char *time_str = ctime_r (&t, buf);
+#else
+      const char *time_str = ctime (&t);
+#endif
+      strncpy (time, time_str, maxlen);
       time[maxlen - 1] = '\0';
     }
 }