public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin/cygwin-3_3-branch] newlocale: fix crash when trying to write to __C_locale
@ 2022-08-12 10:29 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2022-08-12 10:29 UTC (permalink / raw)
  To: cygwin-cvs, newlib-cvs

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

commit b612db5b14728214ca09355af5d1490df2fa1c2f
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Thu Aug 11 19:27:48 2022 +0200

    newlocale: fix crash when trying to write to __C_locale
    
    This simple testcase:
    
      locale_t st = newlocale(LC_ALL_MASK, "C", (locale_t)0);
      locale_t st2 = newlocale(LC_CTYPE_MASK, "en_US.UTF-8", st);
    
    is sufficient to reproduce a crash in _newlocale_r.  After the first call
    to newlocale, `st' points to __C_locale, which is const.  When using `st'
    as locale base in the second call, _newlocale_r tries to set pointers
    inside base to NULL.  This is bad if base is __C_locale, obviously.
    
    Add a test to avoid trying to overwrite pointer values inside base if
    base is __C_locale.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 newlib/libc/locale/newlocale.c | 3 ++-
 winsup/cygwin/release/3.3.6    | 3 +++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/newlib/libc/locale/newlocale.c b/newlib/libc/locale/newlocale.c
index 0789d5fd9..08f29dbcc 100644
--- a/newlib/libc/locale/newlocale.c
+++ b/newlib/libc/locale/newlocale.c
@@ -188,7 +188,8 @@ _newlocale_r (struct _reent *p, int category_mask, const char *locale,
 	if (tmp_locale.lc_cat[i].buf == (const void *) -1)
 	  {
 	    tmp_locale.lc_cat[i].buf = base->lc_cat[i].buf;
-	    base->lc_cat[i].ptr = base->lc_cat[i].buf = NULL;
+	    if (base != __get_C_locale ())
+	      base->lc_cat[i].ptr = base->lc_cat[i].buf = NULL;
 	  }
 #endif /* __HAVE_LOCALE_INFO__ */
       _freelocale_r (p, base);
diff --git a/winsup/cygwin/release/3.3.6 b/winsup/cygwin/release/3.3.6
index 364e0cb0d..1da4fa26c 100644
--- a/winsup/cygwin/release/3.3.6
+++ b/winsup/cygwin/release/3.3.6
@@ -39,3 +39,6 @@ Bug Fixes
 - Fix a path handling bug that could cause a non-existing file to be
   treated as the current directory.
   Addresses: https://cygwin.com/pipermail/cygwin/2022-August/252030.html
+
+- Fix a crash in newlocale.
+  Addresses: https://cygwin.com/pipermail/cygwin/2022-August/252043.html


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-08-12 10:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-12 10:29 [newlib-cygwin/cygwin-3_3-branch] newlocale: fix crash when trying to write to __C_locale Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).