[newlib-cygwin/main] jp2uc/up2jc: avoid crash due to invalid pointer

Corinna Vinschen corinna@sourceware.org
Fri Apr 24 10:46:27 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=4ba7eff4d15af0f749d1913703a25c045d40fa90

commit 4ba7eff4d15af0f749d1913703a25c045d40fa90
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Fri Apr 24 12:46:08 2026 +0200
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Fri Apr 24 12:46:08 2026 +0200

    jp2uc/up2jc: avoid crash due to invalid pointer
    
    Commit 4356ccd02c9c ("towupper/towlower: handle Turkic language special
    casing") changed the locale pointer in calls to towctrans_l from a NULL
    pointer to LC_GLOBAL_LOCALE, which is defined as -1.
    
    Unfortunately jp2uc_l/up2jc_l just expect a NULL or non-NULL pointer,
    so LC_GLOBAL_LOCALE as input is derefenced later on and the call crashes.
    
    To fix this problem, handle LC_GLOBAL_LOCALE just as if a NULL pointer
    has been passed to the function.  Keep the NULL pointer handling intact
    for backward compatibility.
    
    Fixes: 4356ccd02c9c ("towupper/towlower: handle Turkic language special casing")
    Reported-by: Christophe Lyon <christophe.lyon.oss@gmail.com>
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 newlib/libc/ctype/jp2uc.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/newlib/libc/ctype/jp2uc.c b/newlib/libc/ctype/jp2uc.c
index 5e30f09bedcd..a32561eb7ad2 100644
--- a/newlib/libc/ctype/jp2uc.c
+++ b/newlib/libc/ctype/jp2uc.c
@@ -166,7 +166,8 @@ __uc2jp (wint_t c, int type)
 wint_t
 _jp2uc_l (wint_t c, struct __locale_t * l)
 {
-  const char * cs = l ? __locale_charset(l) : __current_locale_charset();
+  const char *cs = (l && l != LC_GLOBAL_LOCALE)
+		   ? __locale_charset(l) : __current_locale_charset();
   if (0 == strcmp (cs, "JIS"))
     c = __jp2uc (c, JP_JIS);
   else if (0 == strcmp (cs, "SJIS"))
@@ -179,14 +180,15 @@ _jp2uc_l (wint_t c, struct __locale_t * l)
 wint_t
 _jp2uc (wint_t c)
 {
-  return _jp2uc_l (c, 0);
+  return _jp2uc_l (c, NULL);
 }
 
 /* Unicode to Japanese conversion interface */
 wint_t
 _uc2jp_l (wint_t c, struct __locale_t * l)
 {
-  const char * cs = l ? __locale_charset(l) : __current_locale_charset();
+  const char *cs = (l && l != LC_GLOBAL_LOCALE)
+		   ? __locale_charset(l) : __current_locale_charset();
   if (0 == strcmp (cs, "JIS"))
     c = __uc2jp (c, JP_JIS);
   else if (0 == strcmp (cs, "SJIS"))


More information about the Newlib-cvs mailing list