public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Ken Brown <kbrown@cornell.edu>
To: newlib@sourceware.org
Subject: Re: crash in newlocale()
Date: Thu, 11 Aug 2022 12:29:31 -0400	[thread overview]
Message-ID: <84729a34-a9dc-0098-3230-6a944d3f7c37@cornell.edu> (raw)
In-Reply-To: <20220811211806.bc7f5426ce52f57d1c1538c2@nifty.ne.jp>

On 8/11/2022 8:18 AM, Takashi Yano wrote:
> On Thu, 11 Aug 2022 11:29:28 +1000
> Tony Cook wrote:
>> $ cat newlocale-test.c
>> #include <locale.h>
>> #include <stdio.h>
>>
>> int main() {
>>    locale_t st = newlocale(LC_ALL_MASK, "C", (locale_t)0);
>>
>>    locale_t st2 = newlocale(LC_CTYPE_MASK, "en_US.UTF-8", st);
>>    printf("Done\n");
>>    return 0;
>> }
>> Program terminated with signal SIGTRAP, Trace/breakpoint trap.
>> The program no longer exists.
>> (gdb)
> 
> I looked into this problem and found the access violation
> occurs at:
> newlib/libc/locale/newlocale.c
> @@ 188,7 @@ _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; <-- Here!!!
> 	  }
> #endif /* __HAVE_LOCALE_INFO__ */
>        _freelocale_r (p, base);
> 
> This is because
> locale_t st = newlocale(LC_ALL_MASK, "C", (locale_t)0);
> returns
>    extern const struct __locale_t __C_locale;
>    return (struct __locale_t *) &__C_locale;
> , which is in the const area that cannot be modified.
> 
> This seems to be a newlib bug.
> 
> I also found following patch solves the issue.
> 
> diff --git a/newlib/libc/locale/newlocale.c b/newlib/libc/locale/newlocale.c
> index 0789d5fd9..1974665ec 100644
> --- a/newlib/libc/locale/newlocale.c
> +++ b/newlib/libc/locale/newlocale.c
> @@ -108,10 +108,12 @@ _newlocale_r (struct _reent *p, int category_mask, const char *locale,
>       }
>     /* If the new locale is supposed to be all default locale, just return
>        a pointer to the default locale. */
> +#if 0
>     if ((!base && category_mask == 0)
>         || (category_mask == LC_VALID_MASK
>   	  && (!strcmp (locale, "C") || !strcmp (locale, "POSIX"))))
>       return __get_C_locale ();
> +#endif
>     /* Start with setting all values to the default locale values. */
>     tmp_locale = *__get_C_locale ();
>     /* Fill out new category strings. */

Good work tracking this down!

I was about to suggest that instead of just disabling this optimization, you 
call _duplocale_r to return a duplicate of __get_C_locale ().  But then I saw 
the following in the definition of _duplocale_r in duplocale.c:

   /* The "C" locale is used statically, never copied. */
   else if (locobj == __get_C_locale ())
     return __get_C_locale ();

This looks like another bug.  Shouldn't duplocale always return a modifiable object?

Ken

  reply	other threads:[~2022-08-11 16:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <YvRbeAzxwtYwhkkE@venus.tony.develop-help.com>
2022-08-11 12:18 ` Takashi Yano
2022-08-11 16:29   ` Ken Brown [this message]
2022-08-11 23:41     ` Takashi Yano
2022-08-11 17:35   ` Corinna Vinschen
2022-08-11 17:39     ` Corinna Vinschen
2022-08-11 23:38     ` Takashi Yano
2022-08-12 10:50       ` Corinna Vinschen

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=84729a34-a9dc-0098-3230-6a944d3f7c37@cornell.edu \
    --to=kbrown@cornell.edu \
    --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).