]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
string: Use tls-internal on strerror_l
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Thu, 14 May 2020 20:56:25 +0000 (17:56 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 7 Jul 2020 17:10:58 +0000 (14:10 -0300)
The buffer allocation uses the same strategy of strsignal.

Checked on x86-64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
and s390x-linux-gnu.

Tested-by: Carlos O'Donell <carlos@redhat.com>
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
include/string.h
malloc/thread-freeres.c
string/strerror_l.c
sysdeps/generic/tls-internal-struct.h
sysdeps/generic/tls-internal.h
sysdeps/mach/strerror_l.c
sysdeps/unix/sysv/linux/tls-internal.h

index 3a33327cc00f3da02501fbb801290137496fa7bc..ce01ad82540d61af2180136457ba32015777080c 100644 (file)
@@ -53,9 +53,6 @@ extern char *__strerror_r (int __errnum, char *__buf, size_t __buflen);
 
 extern char *__strerror_l (int __errnum, locale_t __loc);
 
-/* Called as part of the thread shutdown sequence.  */
-void __strerror_thread_freeres (void) attribute_hidden;
-
 /* Get _STRING_ARCH_unaligned.  */
 #include <string_private.h>
 #endif
index 3408bdbefd33321a979215468196f3570f3f2e66..eb29d53310c7eb1554af2ec95cf3bebe7ed91ad4 100644 (file)
@@ -32,7 +32,6 @@ __libc_thread_freeres (void)
 {
   call_function_static_weak (__rpc_thread_destroy);
   call_function_static_weak (__res_thread_freeres);
-  call_function_static_weak (__strerror_thread_freeres);
   __glibc_tls_internal_free ();
 
   /* This should come last because it shuts down malloc for this
index 017bd14b99d8d42a8fa81af106e9565e585d63ff..a203e9e80b3e43b01d20aba2d2e87c8364c6205f 100644 (file)
 #include <libintl.h>
 #include <locale.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
-#include <errno.h>
-
-static __thread char *last_value;
+#include <tls-internal.h>
 
 
 static const char *
@@ -43,12 +40,13 @@ __strerror_l (int errnum, locale_t loc)
   char *err = (char *) __get_errlist (errnum);
   if (__glibc_unlikely (err == NULL))
     {
-      free (last_value);
-      if (__asprintf (&last_value, "%s%d",
+      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)
-       last_value = NULL;
+       tls_internal->strerror_l_buf = NULL;
 
-      err = last_value;
+      err = tls_internal->strerror_l_buf;
     }
   else
     err = (char *) translate (err, loc);
@@ -58,10 +56,3 @@ __strerror_l (int errnum, locale_t loc)
 }
 weak_alias (__strerror_l, strerror_l)
 libc_hidden_def (__strerror_l)
-
-void
-__strerror_thread_freeres (void)
-{
-  free (last_value);
-}
-text_set_element (__libc_subfreeres, __strerror_thread_freeres);
index 33a9079ee9ecbbae6445996e27d22ae955c0f81d..156e722bd8616682e22be30c868ef98ac499a9d8 100644 (file)
@@ -22,6 +22,7 @@
 struct tls_internal_t
 {
   char *strsignal_buf;
+  char *strerror_l_buf;
 };
 
 #endif
index 1f6a117d76855881492c5f23f81c702b07ddda89..b4b03b4ecf97cbb01b711e1e2c5f1a8e63511773 100644 (file)
@@ -34,6 +34,7 @@ static inline void
 __glibc_tls_internal_free (void)
 {
   free (__tls_internal.strsignal_buf);
+  free (__tls_internal.strerror_l_buf);
 }
 
 #endif
index b6c9fdbe800cb80a8c8b50e29cb5a8d51400b032..72b857f4a09ce34baabe5f7b4207b39baeb978c5 100644 (file)
 #include <string.h>
 #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 *
@@ -58,13 +54,14 @@ __strerror_l (int errnum, locale_t loc)
 
   if (system > err_max_system || ! __mach_error_systems[system].bad_sub)
     {
-      free (last_value);
-      if (__asprintf (&last_value, "%s%X",
+      struct tls_internal_t *tls_internal = __glibc_tls_internal ();
+      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,25 +71,18 @@ __strerror_l (int errnum, locale_t loc)
 
   if (code >= es->subsystem[sub].max_code)
     {
-      free (last_value);
-      if (__asprintf (&last_value, "%s%s %d",
+      struct tls_internal_t *tls_internal = __glibc_tls_internal ();
+      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);
 }
 weak_alias (__strerror_l, strerror_l)
 libc_hidden_def (__strerror_l)
-
-/* This is called when a thread is exiting to free the last_value string.  */
-void
-__strerror_thread_freeres (void)
-{
-  free (last_value);
-}
-text_set_element (__libc_subfreeres, __strerror_thread_freeres);
index 5d712abd4ae2954636d769c0a0aeaa18d08f559a..d07f4b680d8cee68382a9d9a1c69ccdfe68f6591 100644 (file)
@@ -32,6 +32,7 @@ static inline void
 __glibc_tls_internal_free (void)
 {
   free (THREAD_SELF->tls_state.strsignal_buf);
+  free (THREAD_SELF->tls_state.strerror_l_buf);
 }
 
 #endif