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.
---
string/strerror_l.c | 16 +++++++--------
sysdeps/generic/tls-internal-struct.h | 1 +
sysdeps/mach/strerror_l.c | 28 +++++++++++++--------------
3 files changed, 21 insertions(+), 24 deletions(-)
@@ -18,11 +18,8 @@
#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 *
@@ -45,12 +42,13 @@ __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",
+ 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;
- r = last_value;
+ r = tls_internal->strerror_l_buf;
}
else
r = (char *) translate (_sys_errlist_internal[errnum], loc);
@@ -64,6 +62,6 @@ libc_hidden_def (__strerror_l)
void
__strerror_thread_freeres (void)
{
- free (last_value);
+ free (__glibc_tls_internal ()->strerror_l_buf);
}
text_set_element (__libc_subfreeres, __strerror_thread_freeres);
@@ -22,6 +22,7 @@
struct tls_internal_t
{
char *strsignal_buf;
+ char *strerror_l_buf;
};
#endif
@@ -23,11 +23,7 @@
#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,14 +71,15 @@ __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);
@@ -89,10 +87,10 @@ __strerror_l (int errnum, locale_t 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. */
+/* This is called when a thread is exiting to free the allocated string. */
void
__strerror_thread_freeres (void)
{
- free (last_value);
+ free (__glibc_tls_internal ()->strerror_l_buf);
}
text_set_element (__libc_subfreeres, __strerror_thread_freeres);