From patchwork Wed May 22 22:33:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1938082 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JyhJt7R/; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Vl5jb3kXTz20KL for ; Thu, 23 May 2024 08:35:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D1C1A38654AF for ; Wed, 22 May 2024 22:35:29 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B686B38654B2 for ; Wed, 22 May 2024 22:34:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B686B38654B2 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B686B38654B2 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716417259; cv=none; b=sA5DZOF0zv4qya7+t8dPvaFy0aPq17h0WrSnnWIsdoDZzEBjKeD3JRTWu36+X9IMRuQI31Gd28V2mLiS2y+8xTqBqN0iuraZLskF8RHBzk74s56NB6ozyYjO+XVwku44jbmn5Al8LvtECAPCcmUIogfDTGkUQ6Xhqq1UHz5rw+E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716417259; c=relaxed/simple; bh=dt6LtWZWXXend+oUIcFL27aCb6L5W8X53YCnXomlV54=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=pSFU/ulCIejOWyPII4kL/DuZsnElAlYKzzyPlWM6D6VBxcrhseYLhcrS5SeIePOYxtKHKee6tnjeL9n0HAjYeymLUcvpc/Zvt/iyUpcDK3HzF09AkO3ncBlq/RJkYEZq7zMoaxXO6uMcmN/+II7EZVX0r0R22wWPEQcw36KiKo4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1716417257; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=gE3S5idoWxsTwx7WzWycrgTGy57N724pnx40ue7O2oo=; b=JyhJt7R/SkuKponLNCobQJ1Lh9FFh7A+kgGv21A+RGjLG9t2rP31P0raUYxGtFmPDB0zaN P79F8LSc0wQ3jEimXjJeU8AcWrRZ1cQzzyamjoFxyXXcJgA3QKohwrWopGDqA2TCXJAUIa JyI+2nECIiNmW4kXh68IbZjnZ9cBSek= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-147-dp1UNG1oPs-zZb4K6o9kjQ-1; Wed, 22 May 2024 18:34:15 -0400 X-MC-Unique: dp1UNG1oPs-zZb4K6o9kjQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9B9651C05148; Wed, 22 May 2024 22:34:15 +0000 (UTC) Received: from localhost (unknown [10.42.28.18]) by smtp.corp.redhat.com (Postfix) with ESMTP id 693F5C15BB9; Wed, 22 May 2024 22:34:15 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix effects of combining locales [PR108323] Date: Wed, 22 May 2024 23:33:24 +0100 Message-ID: <20240522223414.345835-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-linux. Pushed to trunk. -- >8 -- This fixes a bug in locale::combine where we fail to meet the standard's requirement that the result is unnamed. It also implements two library issues related to the names of combined locales (2295 and 3676). libstdc++-v3/ChangeLog: PR libstdc++/108323 * include/bits/locale_classes.tcc (locale(const locale&, Facet*)): Return a copy of the first argument when the facet pointer is null, as per LWG 2295. (locale::combine): Ensure the result is unnamed. * src/c++11/localename.cc (_M_replace_categories): Ignore whether the second locale has a name when cat == none, as per LWG 3676. * src/c++98/locale.cc (_M_install_facet): Use __builtin_expect to predict that the facet pointer is non-null. * testsuite/22_locale/locale/cons/names.cc: New test. --- libstdc++-v3/include/bits/locale_classes.tcc | 13 +++- libstdc++-v3/src/c++11/localename.cc | 4 +- libstdc++-v3/src/c++98/locale.cc | 2 +- .../testsuite/22_locale/locale/cons/names.cc | 61 +++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 libstdc++-v3/testsuite/22_locale/locale/cons/names.cc diff --git a/libstdc++-v3/include/bits/locale_classes.tcc b/libstdc++-v3/include/bits/locale_classes.tcc index 63097582dec..00eeb7dd9f8 100644 --- a/libstdc++-v3/include/bits/locale_classes.tcc +++ b/libstdc++-v3/include/bits/locale_classes.tcc @@ -44,6 +44,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION locale:: locale(const locale& __other, _Facet* __f) { + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 2295. Locale name when the provided Facet is a nullptr + if (__builtin_expect(!__f, 0)) + { + _M_impl = __other._M_impl; + _M_impl->_M_add_reference(); + return; + } + _M_impl = new _Impl(*__other._M_impl, 1); __try @@ -72,6 +81,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __tmp->_M_remove_reference(); __throw_exception_again; } + delete[] __tmp->_M_names[0]; + __tmp->_M_names[0] = 0; // Unnamed. return locale(__tmp); } @@ -163,7 +174,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION */ template inline bool - has_facet(const locale& __loc) throw() + has_facet(const locale& __loc) _GLIBCXX_USE_NOEXCEPT { #if __cplusplus >= 201103L static_assert(__is_base_of(locale::facet, _Facet), diff --git a/libstdc++-v3/src/c++11/localename.cc b/libstdc++-v3/src/c++11/localename.cc index cde94ec6e19..909cf4c66d3 100644 --- a/libstdc++-v3/src/c++11/localename.cc +++ b/libstdc++-v3/src/c++11/localename.cc @@ -326,7 +326,9 @@ const int num_facets = ( _M_replace_categories(const _Impl* __imp, category __cat) { category __mask = 1; - if (!_M_names[0] || !__imp->_M_names[0]) + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3676. Name of locale composed using std::locale::none + if (!_M_names[0] || (__cat != none && !__imp->_M_names[0])) { if (_M_names[0]) { diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc index 3749408115e..0e7533e1e15 100644 --- a/libstdc++-v3/src/c++98/locale.cc +++ b/libstdc++-v3/src/c++98/locale.cc @@ -323,7 +323,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION locale::_Impl:: _M_install_facet(const locale::id* __idp, const facet* __fp) { - if (__fp) + if (__builtin_expect(__fp != 0, 1)) { size_t __index = __idp->_M_id(); diff --git a/libstdc++-v3/testsuite/22_locale/locale/cons/names.cc b/libstdc++-v3/testsuite/22_locale/locale/cons/names.cc new file mode 100644 index 00000000000..2a9cfe4c14d --- /dev/null +++ b/libstdc++-v3/testsuite/22_locale/locale/cons/names.cc @@ -0,0 +1,61 @@ +// { dg-do run } + +#include +#include + +void +test_pr108323() +{ + std::locale named = std::locale::classic(); + std::locale unnamed = named.combine >(named); + + // Bug libstdc++/108323 - combine does not change the locale name + VERIFY( unnamed.name() == "*" ); +} + +void +test_lwg2295() +{ + std::locale named = std::locale::classic(); + std::locale unnamed(named, &std::use_facet >(named)); + VERIFY( unnamed.name() == "*" ); + + // LWG 2295. Locale name when the provided Facet is a nullptr + std::locale loc(named, (std::ctype*)0); + VERIFY( loc.name() != "*" ); + VERIFY( loc.name() == named.name() ); +} + +void +test_lwg3676() +{ + std::locale named = std::locale::classic(); + std::locale unnamed = named.combine >(named); + std::locale combo; + + // LWG 3676. Name of locale composed using std::locale::none + + combo = std::locale(named, named, std::locale::numeric); + VERIFY( combo.name() != "*" ); + combo = std::locale(named, named, std::locale::none); + VERIFY( combo.name() != "*" ); + combo = std::locale(named, unnamed, std::locale::numeric); + VERIFY( combo.name() == "*" ); + combo = std::locale(named, unnamed, std::locale::none); + VERIFY( combo.name() != "*" ); + combo = std::locale(unnamed, named, std::locale::numeric); + VERIFY( combo.name() == "*" ); + combo = std::locale(unnamed, named, std::locale::none); + VERIFY( combo.name() == "*" ); + combo = std::locale(unnamed, unnamed, std::locale::numeric); + VERIFY( combo.name() == "*" ); + combo = std::locale(unnamed, unnamed, std::locale::none); + VERIFY( combo.name() == "*" ); +} + +int main() +{ + test_pr108323(); + test_lwg2295(); + test_lwg3676(); +}