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

The Linux man pages recommend this version of the function for portable
applications.
---

This change could be made into its own tiny compat source file, if we
want to keep _GNU_SOURCE undefined for the smallest piece of code.
I'm okay with either approach.

I ran the testsuite on a glibc system, didn't spot any issues.

 libdwfl/ChangeLog    |  4 ++++
 libdwfl/dwfl_error.c | 11 ++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index f9f6f01f..d22f9892 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,7 @@
+2020-12-16  Érico Nogueira  <eric...@disroot.org>
+
+       * dwfl_error.c (strerror_r): Always use the XSI-compliant version.
+
 2020-12-16  Dmitry V. Levin  <l...@altlinux.org>
 
        * argp-std.c (_): Remove.
diff --git a/libdwfl/dwfl_error.c b/libdwfl/dwfl_error.c
index 7bcf61cc..e5db1217 100644
--- a/libdwfl/dwfl_error.c
+++ b/libdwfl/dwfl_error.c
@@ -30,6 +30,11 @@
 # include <config.h>
 #endif
 
+/* Guarantee that we get the XSI compliant strerror_r */
+#ifdef _GNU_SOURCE
+#undef _GNU_SOURCE
+#endif
+
 #include <assert.h>
 #include <libintl.h>
 #include <stdbool.h>
@@ -136,6 +141,8 @@ __libdwfl_seterrno (Dwfl_Error error)
   global_error = canonicalize (error);
 }
 
+/* To store the error message from strerror_r */
+static __thread char errormsg[128];
 
 const char *
 dwfl_errmsg (int error)
@@ -154,7 +161,9 @@ dwfl_errmsg (int error)
   switch (error &~ 0xffff)
     {
     case OTHER_ERROR (ERRNO):
-      return strerror_r (error & 0xffff, "bad", 0);
+      if (strerror_r (error & 0xffff, errormsg, sizeof errormsg))
+       return "strerror_r() failed()";
+      return errormsg;
     case OTHER_ERROR (LIBELF):
       return elf_errmsg (error & 0xffff);
     case OTHER_ERROR (LIBDW):
-- 
2.29.2

Reply via email to