[review] Use strerror_r in safe_strerror if available

Message ID gerrit.1572551088000.I81048fbaf148035c221c528727f7efe58ba528eb@gnutoolchain-gerrit.osci.io
State New, archived
Headers

Commit Message

Simon Marchi (Code Review) Oct. 31, 2019, 7:44 p.m. UTC
  Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................

Use strerror_r in safe_strerror if available

Also stores the result in a thread-local static variable.

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/posix-strerror.c (safe_strerror): Make buf
	thread_local and call strerror_r, if available.

Change-Id: I81048fbaf148035c221c528727f7efe58ba528eb
---
M gdb/configure
M gdb/configure.ac
M gdb/gdbsupport/posix-strerror.c
3 files changed, 15 insertions(+), 5 deletions(-)
  

Comments

Simon Marchi (Code Review) Oct. 31, 2019, 7:53 p.m. UTC | #1
Simon Marchi has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Patch Set 1:

(2 comments)

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/1/gdb/gdbsupport/posix-strerror.c 
File gdb/gdbsupport/posix-strerror.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/1/gdb/gdbsupport/posix-strerror.c@24 
PS1, Line 24: 
19 | 
20 | #include "common-defs.h"
21 | 
22 | /* Implementation of safe_strerror as defined in common-utils.h.  */
23 | 
24 > char *
25 | safe_strerror (int errnum)
26 | {
27 |   static thread_local char buf[1024];
28 | 
29 |   char *msg = nullptr;

Just wondering, maybe we could make safe_strerror return a `const char *`?  I suppose we never want the caller to modify what we return, since it could point to some immutable string.


https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/1/gdb/gdbsupport/posix-strerror.c@31 
PS1, Line 31: 
26 | {
27 |   static thread_local char buf[1024];
28 | 
29 |   char *msg = nullptr;
30 | #ifdef HAVE_STRERROR_R
31 > #if !__GLIBC__ || ((_POSIX_C_SOURCE >= 200112L) && !  _GNU_SOURCE)
32 |   /* Glibc has two different, incompatible versions of strerror_r.  */
33 |   if (strerror_r (errnum, buf, sizeof (buf)) == 0)
34 |     msg = buf;
35 | #else
36 |   msg = strerror_r (errnum, buf, sizeof (buf));

Just a nit, can you "indent" the preprocessor declarations, like

 #ifdef HAVE_STRERROR_R
 # if ...

?  I think it makes it easier to read.
  
Simon Marchi (Code Review) Oct. 31, 2019, 8:05 p.m. UTC | #2
Christian Biesinger has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Uploaded patch set 2.

(2 comments)

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/1/gdb/gdbsupport/posix-strerror.c 
File gdb/gdbsupport/posix-strerror.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/1/gdb/gdbsupport/posix-strerror.c@24 
PS1, Line 24: 
19 | 
20 | #include "common-defs.h"
21 | 
22 | /* Implementation of safe_strerror as defined in common-utils.h.  */
23 | 
24 > char *
25 | safe_strerror (int errnum)
26 | {
27 |   static thread_local char buf[1024];
28 | 
29 |   char *msg = nullptr;

> Just wondering, maybe we could make safe_strerror return a `const char *`?  I suppose we never want  […]

Done, only two callers needed changes.


https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/1/gdb/gdbsupport/posix-strerror.c@31 
PS1, Line 31: 
26 | {
27 |   static thread_local char buf[1024];
28 | 
29 |   char *msg = nullptr;
30 | #ifdef HAVE_STRERROR_R
31 > #if !__GLIBC__ || ((_POSIX_C_SOURCE >= 200112L) && !  _GNU_SOURCE)
32 |   /* Glibc has two different, incompatible versions of strerror_r.  */
33 |   if (strerror_r (errnum, buf, sizeof (buf)) == 0)
34 |     msg = buf;
35 | #else
36 |   msg = strerror_r (errnum, buf, sizeof (buf));

> Just a nit, can you "indent" the preprocessor declarations, like […]

Good idea, done.
  
Simon Marchi (Code Review) Oct. 31, 2019, 8:10 p.m. UTC | #3
Simon Marchi has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Patch Set 2:

Thanks!
  
Simon Marchi (Code Review) Oct. 31, 2019, 8:10 p.m. UTC | #4
Simon Marchi has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Patch Set 2: Code-Review+2
  
Simon Marchi (Code Review) Nov. 1, 2019, 3:21 p.m. UTC | #5
Hannes Domani has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Patch Set 3:

(1 comment)

> Patch Set 1:
> 
> (2 comments)

New build failure for windows.

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/common-utils.h 
File gdb/gdbsupport/common-utils.h:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/common-utils.h@113 
PS3, Line 113: 
108 | 
109 | /* The strerror() function can return NULL for errno values that are
110 |    out of range.  Provide a "safe" version that always returns a
111 |    printable string.  This version is also thread-safe.  */
112 | 
113 > extern const char *safe_strerror (int);
114 | 
115 | /* Return true if the start of STRING matches PATTERN, false otherwise.  */
116 | 
117 | static inline bool
118 | startswith (const char *string, const char *pattern)

The windows build now fails, because safe_strerror() of mingw-strerror.c doesn't match this prototype anymore:

C:/src/repos/binutils-gdb.git/gdb/gdbsupport/mingw-strerror.c:32:1: error: ambiguating new declaration of 'char* safe_strerror(int)'
   32 | safe_strerror (int errnum)
      | ^~~~~~~~~~~~~
In file included from C:/src/repos/binutils-gdb.git/gdb/gdbsupport/common-defs.h:122,
                 from C:/src/repos/binutils-gdb.git/gdb/gdbsupport/mingw-strerror.c:20:
C:/src/repos/binutils-gdb.git/gdb/gdbsupport/common-utils.h:113:20: note: old declaration 'const char* safe_strerror(int)'
  113 | extern const char *safe_strerror (int);
      |                    ^~~~~~~~~~~~~
  
Simon Marchi (Code Review) Nov. 1, 2019, 3:22 p.m. UTC | #6
Christian Biesinger has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Patch Set 3:

(1 comment)

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/common-utils.h 
File gdb/gdbsupport/common-utils.h:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/common-utils.h@113 
PS3, Line 113: 
108 | 
109 | /* The strerror() function can return NULL for errno values that are
110 |    out of range.  Provide a "safe" version that always returns a
111 |    printable string.  This version is also thread-safe.  */
112 | 
113 > extern const char *safe_strerror (int);
114 | 
115 | /* Return true if the start of STRING matches PATTERN, false otherwise.  */
116 | 
117 | static inline bool
118 | startswith (const char *string, const char *pattern)

> The windows build now fails, because safe_strerror() of mingw-strerror. […]

Thanks; tromey has a patch for that at https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/478
  
Tom Tromey Nov. 1, 2019, 5:10 p.m. UTC | #7
>>>>> "Christian" == Christian Biesinger (Code Review) <gerrit@gnutoolchain-gerrit.osci.io> writes:

>> The windows build now fails, because safe_strerror() of mingw-strerror. […]

Christian> Thanks; tromey has a patch for that at https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/478

I pushed it a little while ago.

Tom
  
Simon Marchi (Code Review) Nov. 6, 2019, 1:08 a.m. UTC | #8
Pedro Alves has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Patch Set 3:

(1 comment)

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/posix-strerror.c 
File gdb/gdbsupport/posix-strerror.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/posix-strerror.c@37 
PS3, Line 37: 
32 |   /* Glibc has two different, incompatible versions of strerror_r.  */
33 |   if (strerror_r (errnum, buf, sizeof (buf)) == 0)
34 |     msg = buf;
35 | #  else
36 |   msg = strerror_r (errnum, buf, sizeof (buf));
37 > #  endif
38 | #else
39 |   msg = strerror (errnum);
40 | #endif
41 |   if (msg == nullptr)
42 |     {

Any reason we don't use strerror_r from gnulib instead?

https://www.gnu.org/software/gnulib/manual/html_node/strerror_005fr.html

We don't import that module currently, but I'd assume there'd be no blocker.

I looked at the lib/strerror_r.c in gnulib's sources, and checked that it handles Windows' socket error codes too.  Though it does it differently.
  
Simon Marchi (Code Review) Nov. 6, 2019, 7:36 p.m. UTC | #9
Christian Biesinger has posted comments on this change.

Change URL: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474
......................................................................


Patch Set 3:

(1 comment)

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/posix-strerror.c 
File gdb/gdbsupport/posix-strerror.c:

https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/474/3/gdb/gdbsupport/posix-strerror.c@37 
PS3, Line 37: 
32 |   /* Glibc has two different, incompatible versions of strerror_r.  */
33 |   if (strerror_r (errnum, buf, sizeof (buf)) == 0)
34 |     msg = buf;
35 | #  else
36 |   msg = strerror_r (errnum, buf, sizeof (buf));
37 > #  endif
38 | #else
39 |   msg = strerror (errnum);
40 | #endif
41 |   if (msg == nullptr)
42 |     {

> Any reason we don't use strerror_r from gnulib instead? […]

Thanks for the suggestion, uploaded a patch for that at https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/514
  

Patch

diff --git a/gdb/configure b/gdb/configure
index e805903..018cc4b 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 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"
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 354bb7b..987507a 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 strerror_r])
 AM_LANGINFO_CODESET
 GDB_AC_COMMON
 
diff --git a/gdb/gdbsupport/posix-strerror.c b/gdb/gdbsupport/posix-strerror.c
index a8651b7..727eb7b 100644
--- a/gdb/gdbsupport/posix-strerror.c
+++ b/gdb/gdbsupport/posix-strerror.c
@@ -24,12 +24,22 @@ 
 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;