diff mbox series

[RFC,4/4] string: Move strerror_l pointer to tls_internal.h

Message ID 20200513202630.2123238-5-adhemerval.zanella@linaro.org
State New
Headers show
Series Make strsignal and strerror async-signal-safe | expand

Commit Message

Adhemerval Zanella May 13, 2020, 8:26 p.m. UTC
Similar to strsignal_l, move the per-thread message pointer to
tls_internal.h (which on Linux is allocated on struct per-thread).

Checked on x86-64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
and s390x-linux-gnu.
---
 string/strerror_l.c                   | 34 ++++++++++++---------------
 sysdeps/generic/tls_internal-struct.h |  1 +
 sysdeps/mach/strerror_l.c             | 24 +++++++++----------
 3 files changed, 27 insertions(+), 32 deletions(-)
diff mbox series

Patch

diff --git a/string/strerror_l.c b/string/strerror_l.c
index 40e7d0e896..baa489dd90 100644
--- a/string/strerror_l.c
+++ b/string/strerror_l.c
@@ -15,15 +15,11 @@ 
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <stdio.h>
 #include <libintl.h>
 #include <locale.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
-#include <sys/param.h>
-#include <libc-symbols.h>
-
-static __thread char *last_value;
+#include <tls_internal.h>
 
 
 static const char *
@@ -40,23 +36,23 @@  translate (const char *str, locale_t loc)
 char *
 strerror_l (int errnum, locale_t loc)
 {
-  if (__glibc_unlikely (errnum < 0 || errnum >= _sys_nerr_internal
-			|| _sys_errlist_internal[errnum] == NULL))
-    {
-      free (last_value);
-      if (__asprintf (&last_value, "%s%d",
-		      translate ("Unknown error ", loc), errnum) == -1)
-	last_value = NULL;
-
-      return last_value;
-    }
-
-  return (char *) translate (_sys_errlist_internal[errnum], loc);
+  char *r = __strerror_lookup (errnum);
+  if (r != NULL)
+    return (char *) translate (r, loc);
+
+  struct tls_internal_t *tls_internal = __glibc_tls_internal ();
+  free (tls_internal->strerror_l_buf);
+  if (__asprintf (&tls_internal->strerror_l_buf, "%s%d",
+		  translate ("Unknown error ", loc), errnum) == -1)
+    tls_internal->strerror_l_buf = NULL;
+
+  return tls_internal->strerror_l_buf;
 }
 
 void
 __strerror_thread_freeres (void)
 {
-  free (last_value);
+  free (__glibc_tls_internal()->strerror_l_buf);
 }
 text_set_element (__libc_subfreeres, __strerror_thread_freeres);
+
diff --git a/sysdeps/generic/tls_internal-struct.h b/sysdeps/generic/tls_internal-struct.h
index 4416228757..a5ae278f81 100644
--- a/sysdeps/generic/tls_internal-struct.h
+++ b/sysdeps/generic/tls_internal-struct.h
@@ -30,6 +30,7 @@  struct tls_internal_t
   char strsignal_buf[strsignal_str_len + INT_STRLEN_BOUND (int) + 1];
   char *strsignal_l_buf;
   char strerror_buf[strerror_str_len + INT_STRLEN_BOUND (int) + 1];
+  char *strerror_l_buf;
 };
 
 #define TLS_INTERNAL_STRERROR_LEN \
diff --git a/sysdeps/mach/strerror_l.c b/sysdeps/mach/strerror_l.c
index f514af341e..d6a69c3784 100644
--- a/sysdeps/mach/strerror_l.c
+++ b/sysdeps/mach/strerror_l.c
@@ -24,10 +24,7 @@ 
 #include <mach/error.h>
 #include <errorlib.h>
 #include <sys/param.h>
-#include <libc-symbols.h>
-
-
-static __thread char *last_value;
+#include <tls_internal.h>
 
 
 static const char *
@@ -56,15 +53,16 @@  strerror_l (int errnum, locale_t loc)
   sub = err_get_sub (errnum);
   code = err_get_code (errnum);
 
+  struct tls_internal_t *tls_internal = __glibc_tls_internal ();
   if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
     {
-      free (last_value);
-      if (__asprintf (&last_value, "%s%X",
+      free (tls_internal->strerror_l_buf);
+      if (__asprintf (&tls_internal->strerror_l_buf, "%s%X",
 		      translate ("Error in unknown error system: ", loc),
 		      errnum) == -1)
-	last_value = NULL;
+	tls_internal->strerror_l_buf = NULL;
 
-      return last_value;
+      return tls_internal->strerror_l_buf;
     }
 
   es = &__mach_error_systems[system];
@@ -74,14 +72,14 @@  strerror_l (int errnum, locale_t loc)
 
   if (code >= es->subsystem[sub].max_code)
     {
-      free (last_value);
-      if (__asprintf (&last_value, "%s%s %d",
+      free (tls_internal->strerror_l_buf);
+      if (__asprintf (&tls_internal->strerror_l_buf, "%s%s %d",
 		      translate ("Unknown error ", loc),
 		      translate (es->subsystem[sub].subsys_name, loc),
 		      errnum) == -1)
-	last_value = NULL;
+	tls_internal->strerror_l_buf = NULL;
 
-      return last_value;
+      return tls_internal->strerror_l_buf;
     }
 
   return (char *) translate (es->subsystem[sub].codes[code], loc);
@@ -91,6 +89,6 @@  strerror_l (int errnum, locale_t loc)
 void
 __strerror_thread_freeres (void)
 {
-  free (last_value);
+  free (__glibc_tls_internal()->strerror_l_buf);
 }
 text_set_element (__libc_subfreeres, __strerror_thread_freeres);