Use strerror_r in safe_strerror if available
authorChristian Biesinger <cbiesinger@google.com>
Thu, 31 Oct 2019 19:41:19 +0000 (14:41 -0500)
committerChristian Biesinger <cbiesinger@google.com>
Thu, 31 Oct 2019 20:14:14 +0000 (15:14 -0500)
Also stores the result in a thread-local static variable and
changes the return value to a const char*.

This is already important because Guile creates threads and
Python can create threads, but with the patch series here:
https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/176
GDB itself will create threads, too.

gdb/ChangeLog:

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

* configure: Regenerate.
* configure.ac: Check for strerror_r.
* gdbsupport/common-utils.h (safe_strerror): Change return value
to const char * and document that this function is now threadsafe.
* gdbsupport/posix-strerror.c (safe_strerror): Make buf
thread_local and call strerror_r, if available.
* utils.c (perror_string): Update.
(print_sys_errmsg): Update.

Change-Id: I81048fbaf148035c221c528727f7efe58ba528eb

gdb/ChangeLog
gdb/configure
gdb/configure.ac
gdb/gdbsupport/common-utils.h
gdb/gdbsupport/posix-strerror.c
gdb/utils.c

index 7abe20ee59c0507a461b5856298f72ebb2c71f97..b7a7d98c8a2e956090207d24981a22ed085293c2 100644 (file)
@@ -1,3 +1,14 @@
+2019-10-31  Christian Biesinger  <cbiesinger@google.com>
+
+       * configure: Regenerate.
+       * configure.ac: Check for strerror_r.
+       * gdbsupport/common-utils.h (safe_strerror): Change return value
+       to const char * and document that this function is now threadsafe.
+       * gdbsupport/posix-strerror.c (safe_strerror): Make buf
+       thread_local and call strerror_r, if available.
+       * utils.c (perror_string): Update.
+       (print_sys_errmsg): Update.
+
 2019-10-31  Luis Machado  <luis.machado@linaro.org>
 
        * arm-tdep.c (arm_exidx_data_key): Use bfd_key instead of
index e8059039bd50011fda23ca60f1e467766a336fb5..018cc4ba4357723c773d9b6f0a8a9222e46c354c 100755 (executable)
@@ -13073,7 +13073,7 @@ for ac_func in getauxval getrusage getuid getgid \
                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 strerror_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"
index 354bb7b4b68e2f8d2bea5499f0b2925203bac912..987507a17d7fca1093db0715f4672aa7ea6705f5 100644 (file)
@@ -1318,7 +1318,7 @@ AC_CHECK_FUNCS([getauxval getrusage getuid getgid \
                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 strerror_r])
 AM_LANGINFO_CODESET
 GDB_AC_COMMON
 
index 23bf354a8bd1bedff5c6855ec0759acb430173bf..89868fcf9e8eefb558dc062ac04eb6ee2478d3a4 100644 (file)
@@ -108,9 +108,9 @@ std::string extract_string_maybe_quoted (const char **arg);
 
 /* The strerror() function can return NULL for errno values that are
    out of range.  Provide a "safe" version that always returns a
-   printable string.  */
+   printable string.  This version is also thread-safe.  */
 
-extern char *safe_strerror (int);
+extern const char *safe_strerror (int);
 
 /* Return true if the start of STRING matches PATTERN, false otherwise.  */
 
index a8651b706d79f1fd76ab6940955ded87309c5a93..34420cf537766214bcf9db13282e20e1baeb4640 100644 (file)
 
 /* Implementation of safe_strerror as defined in common-utils.h.  */
 
-char *
+const char *
 safe_strerror (int errnum)
 {
-  char *msg;
-
+  static thread_local char buf[1024];
+
+  char *msg = nullptr;
+#ifdef HAVE_STRERROR_R
+#  if !__GLIBC__ || ((_POSIX_C_SOURCE >= 200112L) && !  _GNU_SOURCE)
+  /* Glibc has two different, incompatible versions of strerror_r.  */
+  if (strerror_r (errnum, buf, sizeof (buf)) == 0)
+    msg = buf;
+#  else
+  msg = strerror_r (errnum, buf, sizeof (buf));
+#  endif
+#else
   msg = strerror (errnum);
-  if (msg == NULL)
+#endif
+  if (msg == nullptr)
     {
-      static char buf[32];
 
       xsnprintf (buf, sizeof buf, "(undocumented errno %d)", errnum);
       msg = buf;
index 0c133d15614948707b4380f03bbc19ef545a326b..e06eeddeeff9b47c034b11a10e7ffa4bbc074ea9 100644 (file)
@@ -582,9 +582,7 @@ add_internal_problem_command (struct internal_problem *problem)
 static std::string
 perror_string (const char *prefix)
 {
-  char *err;
-
-  err = safe_strerror (errno);
+  const char *err = safe_strerror (errno);
   return std::string (prefix) + ": " + err;
 }
 
@@ -630,11 +628,8 @@ perror_warning_with_name (const char *string)
 void
 print_sys_errmsg (const char *string, int errcode)
 {
-  char *err;
-  char *combined;
-
-  err = safe_strerror (errcode);
-  combined = (char *) alloca (strlen (err) + strlen (string) + 3);
+  const char *err = safe_strerror (errcode);
+  char *combined = (char *) alloca (strlen (err) + strlen (string) + 3);
   strcpy (combined, string);
   strcat (combined, ": ");
   strcat (combined, err);
This page took 0.040016 seconds and 4 git commands to generate.