[PATCH 1/1] locale: Avoid use-after-invalidate
Simon Richter
Simon.Richter@hogyros.de
Fri Nov 6 18:18:59 GMT 2026
The return value of setlocale() points to statically allocated memory that
can be overwritten by the next call to setlocale(), so strdup() must be
called immediately after to create a copy.
If strdup() fails, the ENOMEM is hidden from the caller, but that is
inconsequential, and may even have been intended originally.
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34964
Signed-off-by: Simon Richter <Simon.Richter@hogyros.de>
---
locale/programs/record-status.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/locale/programs/record-status.c b/locale/programs/record-status.c
index f2d1131d31..dc40690ea8 100644
--- a/locale/programs/record-status.c
+++ b/locale/programs/record-status.c
@@ -57,7 +57,9 @@ push_locale (void)
saved_errno = errno;
orig = setlocale (LC_CTYPE, NULL);
- if (orig == NULL)
+ if (orig != NULL)
+ copy = strdup (orig);
+ else
error (0, 0, "failed to read locale!");
if (setlocale (LC_CTYPE, "") == NULL)
@@ -65,9 +67,6 @@ push_locale (void)
errno = saved_errno;
- if (orig != NULL)
- copy = strdup (orig);
-
/* We will return either a valid locale or NULL if we failed
to save the locale. */
return (struct locale_state) { .cur_locale = copy };
--
2.47.3
More information about the Libc-alpha
mailing list