public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libdwfl: use XSI-compliant strerror_r.
@ 2020-12-16 20:22 Érico Nogueira
  0 siblings, 0 replies; only message in thread
From: Érico Nogueira @ 2020-12-16 20:22 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Érico Rolim

From: Érico Rolim <erico.erc@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  <ericonr@disroot.org>
+
+	* dwfl_error.c (strerror_r): Always use the XSI-compliant version.
+
 2020-12-16  Dmitry V. Levin  <ldv@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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-12-16 20:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-16 20:22 [PATCH] libdwfl: use XSI-compliant strerror_r Érico Nogueira

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).