From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from conssluserg-03.nifty.com (conssluserg-03.nifty.com [210.131.2.82]) by sourceware.org (Postfix) with ESMTPS id 2B0633858D28 for ; Thu, 11 Aug 2022 23:41:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2B0633858D28 Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=nifty.ne.jp Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=nifty.ne.jp Received: from HP-Z230 (aj135236.dynamic.ppp.asahi-net.or.jp [220.150.135.236]) (authenticated) by conssluserg-03.nifty.com with ESMTP id 27BNfR9E010682 for ; Fri, 12 Aug 2022 08:41:27 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-03.nifty.com 27BNfR9E010682 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.ne.jp; s=dec2015msa; t=1660261287; bh=ALGpRtvgHqnHEmTXWjZKPbrtVN/j5lZHhPmg/w8NGV0=; h=Date:From:To:Subject:In-Reply-To:References:From; b=vqMjwO3f69gpf7RS1Q4ruKhTQksy5bCw30Z2lHOhmit5ed2oho0KEpBamvxWkRng3 xDyO1+R7+yJOUaQdwYv4oYKGQnabF0PYgiP5jBrIheO3YelLWuV9U9Ds9tWQ9nES4P cN92iDW4IuC58+roIpks5CRrY8OITWWtzaoqzIpbeJqhCudaI6EaY0i4nryQn9Gdlg wV1+p8HbFZmYC1Xo/67S9W5yfFyQHtIAB5AksEh6CgJCH02y92SLQjvM+qnNq1wRWP mJwadUxjEpYfpY+dxdY7xiS0r83nz5kqaZPLQ8UfqVaFe7yg9PVk+cYSkEBuQRfhE2 cVINivWg0RHgw== X-Nifty-SrcIP: [220.150.135.236] Date: Fri, 12 Aug 2022 08:41:28 +0900 From: Takashi Yano To: newlib@sourceware.org Subject: Re: crash in newlocale() Message-Id: <20220812084128.ce3834120add831a6d18aeee@nifty.ne.jp> In-Reply-To: <84729a34-a9dc-0098-3230-6a944d3f7c37@cornell.edu> References: <20220811211806.bc7f5426ce52f57d1c1538c2@nifty.ne.jp> <84729a34-a9dc-0098-3230-6a944d3f7c37@cornell.edu> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 11 Aug 2022 23:41:49 -0000 On Thu, 11 Aug 2022 12:29:31 -0400 Ken Brown wrote: > 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 > >> #include > >> > >> 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? Thanks! Corinna's patch avoids the problem also for this case. -- Takashi Yano