public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@vinschen.de>
To: newlib@sourceware.org
Subject: [PATCH] newlocale: fix crash when trying to write to __C_locale
Date: Thu, 11 Aug 2022 20:00:07 +0200	[thread overview]
Message-ID: <20220811180007.296636-1-corinna@vinschen.de> (raw)

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>
---
 newlib/libc/locale/newlocale.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/newlib/libc/locale/newlocale.c b/newlib/libc/locale/newlocale.c
index 0789d5fd95ec..08f29dbcc0c1 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);
-- 
2.37.1


                 reply	other threads:[~2022-08-11 18:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220811180007.296636-1-corinna@vinschen.de \
    --to=corinna@vinschen.de \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).