[review,v4] Use ctime_r and localtime_r if available
Commit Message
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.
* 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 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/gdbserver/config.in
M gdb/gdbserver/configure
M gdb/gdbsupport/common.m4
M gdb/maint.c
M gdb/nat/linux-osdata.c
7 files changed, 19 insertions(+), 6 deletions(-)
@@ -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
@@ -13480,7 +13480,7 @@
for ac_func in fdwalk getrlimit pipe pipe2 socketpair sigaction \
- sigprocmask
+ sigprocmask 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"
@@ -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
@@ -6822,7 +6822,7 @@
for ac_func in fdwalk getrlimit pipe pipe2 socketpair sigaction \
- sigprocmask
+ sigprocmask 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"
@@ -33,7 +33,7 @@
dlfcn.h)
AC_CHECK_FUNCS([fdwalk getrlimit pipe pipe2 socketpair sigaction \
- sigprocmask])
+ sigprocmask ctime_r])
AC_CHECK_DECLS([strerror, strstr])
@@ -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);
}
@@ -913,7 +913,13 @@
{
time_t t = (time_t) seconds;
- strncpy (time, ctime (&t), maxlen);
+#ifdef HAVE_CTIME_R
+ char buf[30];
+ 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';
}
}