From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dmta1001.nifty.com (mta-snd01012.nifty.com [106.153.227.44]) by sourceware.org (Postfix) with ESMTPS id 87E523858C27 for ; Fri, 22 Sep 2023 04:08:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 87E523858C27 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 by dmta1001.nifty.com with ESMTP id <20230922040837660.QHMD.124194.HP-Z230@nifty.com> for ; Fri, 22 Sep 2023 13:08:37 +0900 Date: Fri, 22 Sep 2023 13:08:38 +0900 From: Takashi Yano To: cygwin@cygwin.com Subject: Re: std::runtime_error on std::locale("") Message-Id: <20230922130838.ced40a712c8a4de95be6d5a4@nifty.ne.jp> In-Reply-To: <07777266-f285-be52-0bff-752419352e85@t-online.de> References: <20230922011204.bb166498090e6cbe163349f3@nifty.ne.jp> <20230922012856.47872090281f2a303fd7b99f@nifty.ne.jp> <0fab8831-c206-14fe-4350-3092e62fca98@Shaw.ca> <07777266-f285-be52-0bff-752419352e85@t-online.de> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00,BODY_8BITS,KAM_DMARC_STATUS,KAM_SHORT,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Thu, 21 Sep 2023 21:06:59 +0200 Christian Franke wrote: > Brian Inglis via Cygwin wrote: > > On 2023-09-21 10:28, Takashi Yano via Cygwin wrote: > >> On Fri, 22 Sep 2023 01:12:04 +0900 > >> Takashi Yano wrote: > >>> I wonder why the following code throws std::runtime_error > >>> even though the LC_ALL is set to valid locale other than "C". > >>> This does not occur only when LC_ALL is set to "C". > >>> > >>> #include > >>> int main() > >>> { > >>>     std::locale(""); > >>>     return 0; > >>> } > >>> > >>> In linux, this occurs only when the LC_ALL is set to invalid > >>> locale (i.e. locale that is not registered in system). > >> > >> Similarly, > >> std::locale("ja_JP.UTF-8") > >> throws std::runtime_error in cygwin. > > > > Looks like the implementation does not like any default "" or explicit > > "en_US.UTF-8" strings there! See example at link and below; results > > are always the same: > > > >     https://en.cppreference.com/w/cpp/locale/locale > > > > #include > > #include > > > > int main() > > { > >     std::wcout << "User-preferred locale setting is " > >            << std::locale().name().c_str() << '\n'; > > > >     // on startup, the global locale is the "C" locale > >     std::wcout << 1000.01 << '\n'; > > > >     // replace the C++ global locale and the "C" locale with the > > user-preferred locale > >     std::locale::global(std::locale("")); > >     // use the new global locale for future wide character output > >     std::wcout.imbue(std::locale()); > > > >     // output the same number again > >     std::wcout << 1000.01 << '\n'; > > } > > > > $ g++ -o c++locale{,.cc} > > $ ./c++locale > > User-preferred locale setting is C > > 1000.01 > > terminate called after throwing an instance of 'std::runtime_error' > >   what():  locale::facet::_S_create_c_locale name not valid > > Aborted (core dumped) > > > > According to libstdc++ source, the internal function > locale::facet::_S_create_c_locale() calls some __newlocale() which > apparently does not arrive at newlocale() from cygwin1.dll. But > cygstdc++-6.dll imports newlocale() from cygwin1.dll. Thanks for the pointer. I looked into the cygstdc++6.dll source code, and noticed that the code you mentioned is for glibc. In glibc, __newlocale() is defined and newlocale() is a weak alias for that. For generic libc (i.e. other than glibc), _S_create_c_locale() is defined as: void locale::facet::_S_create_c_locale(__c_locale& __cloc, const char* __s, __c_locale) { // Currently, the generic model only supports the "C" locale. // See http://gcc.gnu.org/ml/libstdc++/2003-02/msg00345.html __cloc = 0; if (strcmp(__s, "C")) __throw_runtime_error(__N("locale::facet::_S_create_c_locale " "name not valid")); } in /libstdc++-v3/config/locale/generic/c_locale.cc. Obviously from the code, locale other than "C" causes runtime_error. The behaviour is as designed but not a bug in cygwin environment. Thanks for discussion. -- Takashi Yano