newlib: getlocalename_l: Avoid strcpy onto const area on "C" locale

Message ID 20260824080300.805-1-takashi.yano@nifty.ne.jp
State New
Headers
Series newlib: getlocalename_l: Avoid strcpy onto const area on "C" locale |

Commit Message

Takashi Yano Aug. 24, 2026, 8:02 a.m. UTC
  With the commit a0fe984953dd ("getlocalename_l: allow LC_ALL category"),
the code attempts to copy locale string to `locobj->locale_string`,
in __currentlocale() in _getlocalename_l_r(). However, if `locobj` is
the "C" locale, it points to the const area. This results in a crash.

This patch simply returns "C" when the `locobj` represents the C
locale, instead of cunstructing LC_ALL string from `categories[]`
because all the `categories[]` are always "C" for the C locale.

Fixes: a0fe984953dd ("getlocalename_l: allow LC_ALL category")
Addresses: https://cygwin.com/pipermail/cygwin/2026-August/259965.html
Reported-by: Tony Cook <tony@develop-help.com>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
---
 newlib/libc/locale/getlocalename_l.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Jeff Johnston Aug. 25, 2026, 12:14 a.m. UTC | #1
Patch applied.  Thanks.

-- Jeff J.

On Mon, Aug 24, 2026 at 4:03 AM Takashi Yano <takashi.yano@nifty.ne.jp>
wrote:

> With the commit a0fe984953dd ("getlocalename_l: allow LC_ALL category"),
> the code attempts to copy locale string to `locobj->locale_string`,
> in __currentlocale() in _getlocalename_l_r(). However, if `locobj` is
> the "C" locale, it points to the const area. This results in a crash.
>
> This patch simply returns "C" when the `locobj` represents the C
> locale, instead of cunstructing LC_ALL string from `categories[]`
> because all the `categories[]` are always "C" for the C locale.
>
> Fixes: a0fe984953dd ("getlocalename_l: allow LC_ALL category")
> Addresses: https://cygwin.com/pipermail/cygwin/2026-August/259965.html
> Reported-by: Tony Cook <tony@develop-help.com>
> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
> ---
>  newlib/libc/locale/getlocalename_l.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/newlib/libc/locale/getlocalename_l.c
> b/newlib/libc/locale/getlocalename_l.c
> index 1f18f828a..7ad4331b4 100644
> --- a/newlib/libc/locale/getlocalename_l.c
> +++ b/newlib/libc/locale/getlocalename_l.c
> @@ -63,6 +63,8 @@ _getlocalename_l_r (struct _reent *ptr, int category,
> struct __locale_t *locobj)
>        if (locobj == LC_GLOBAL_LOCALE)
>         return __currentlocale (__get_global_locale (),
>                                 _REENT_GETLOCALENAME_L_BUF (ptr));
> +      else if (locobj == __get_C_locale ())
> +       return "C";
>        return __currentlocale (locobj, locobj->locale_string);
>      }
>    if (locobj == LC_GLOBAL_LOCALE)
> --
> 2.51.0
>
>
  

Patch

diff --git a/newlib/libc/locale/getlocalename_l.c b/newlib/libc/locale/getlocalename_l.c
index 1f18f828a..7ad4331b4 100644
--- a/newlib/libc/locale/getlocalename_l.c
+++ b/newlib/libc/locale/getlocalename_l.c
@@ -63,6 +63,8 @@  _getlocalename_l_r (struct _reent *ptr, int category, struct __locale_t *locobj)
       if (locobj == LC_GLOBAL_LOCALE)
 	return __currentlocale (__get_global_locale (),
 				_REENT_GETLOCALENAME_L_BUF (ptr));
+      else if (locobj == __get_C_locale ())
+	return "C";
       return __currentlocale (locobj, locobj->locale_string);
     }
   if (locobj == LC_GLOBAL_LOCALE)