[PATCH v2] libdwfl: use GNU strerror_r only when available.

Érico Nogueira ericonr@disroot.org
Tue Feb 2 00:16:56 GMT 2021


From: Érico Rolim <erico.erc@gmail.com>

Some C libraries don't provide the GNU version of strerror_r, only the
XSI-compliant one. We use the GNU version when available, since it fits
the code better, and otherwise use the XSI-compliant one.

https://sourceware.org/bugzilla/show_bug.cgi?id=21010

Signed-off-by: Érico Rolim <erico.erc@gmail.com>
---

I'm not sure if it's a bug in this configure.ac or this macro, but
_GNU_SOURCE wasn't defined and lead to XSI strerror_r being used on
glibc until I added the trick with CFLAGS and old_CFLAGS. It's still way
slimmer than the previous version :)

I see that -D_GNU_SOURCE is passed to the build via config/eu.am, maybe
it could somehow be plugged into configure.ac earlier? I'm not entirely
sure what's best.

 ChangeLog            |  4 ++++
 configure.ac         |  5 +++++
 libdwfl/ChangeLog    |  4 ++++
 libdwfl/dwfl_error.c | 17 ++++++++++++++++-
 4 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 142caa27..c8f921a5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2021-02-01  Érico Nogueira  <ericonr@disroot.org>
+
+	* configure.ac: Check for GNU strerror_r.
+
 2021-01-12  Dmitry V. Levin  <ldv@altlinux.org>
 
 	* configure.ac [--enable-gcov]: Check for gcov, lcov, and genhtml.
diff --git a/configure.ac b/configure.ac
index 346ab800..47db8472 100644
--- a/configure.ac
+++ b/configure.ac
@@ -428,6 +428,11 @@ AC_CHECK_DECLS([mempcpy],[],[],
 
 AC_CHECK_FUNCS([process_vm_readv])
 
+old_CFLAGS="$CFLAGS"
+CFLAGS="$CFLAGS -D_GNU_SOURCE"
+AC_FUNC_STRERROR_R()
+CFLAGS="$old_CFLAGS"
+
 AC_CHECK_LIB([stdc++], [__cxa_demangle], [dnl
 AC_DEFINE([USE_DEMANGLE], [1], [Defined if demangling is enabled])])
 AM_CONDITIONAL(DEMANGLE, test "x$ac_cv_lib_stdcpp___cxa_demangle" = "xyes")
diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 5058f5f8..d107e78f 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,7 @@
+2021-02-01  Érico Nogueira  <ericonr@disroot.org>
+
+	* dwfl_error.c (strerror_r): Only use the GNU version when available.
+
 2021-01-08  Timm Bäder  <tbaeder@redhat.com>
 
 	* elf-from-memory.c (elf_from_remote_memory): Add for loop over
diff --git a/libdwfl/dwfl_error.c b/libdwfl/dwfl_error.c
index 7bcf61cc..a5c683a9 100644
--- a/libdwfl/dwfl_error.c
+++ b/libdwfl/dwfl_error.c
@@ -137,6 +137,21 @@ __libdwfl_seterrno (Dwfl_Error error)
 }
 
 
+static const char *
+errnomsg(int error)
+{
+  /* Won't be changed by strerror_r, but not const so compiler doesn't throw warning */
+  static char unknown[] = "unknown error";
+
+#ifdef STRERROR_R_CHAR_P
+  return strerror_r (error, unknown, 0);
+#else
+  /* To store the error message from strerror_r in a thread-safe manner */
+  static __thread char msg[128];
+  return strerror_r (error, msg, sizeof (msg)) ? unknown : msg;
+#endif
+}
+
 const char *
 dwfl_errmsg (int error)
 {
@@ -154,7 +169,7 @@ dwfl_errmsg (int error)
   switch (error &~ 0xffff)
     {
     case OTHER_ERROR (ERRNO):
-      return strerror_r (error & 0xffff, "bad", 0);
+      return errnomsg (error & 0xffff);
     case OTHER_ERROR (LIBELF):
       return elf_errmsg (error & 0xffff);
     case OTHER_ERROR (LIBDW):
-- 
2.30.0



More information about the Elfutils-devel mailing list