From patchwork Wed Apr 12 12:28:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 82444 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp286965vqo; Wed, 12 Apr 2023 05:30:54 -0700 (PDT) X-Google-Smtp-Source: AKy350YAPZBEEkIhuqJTX49EWBCm6UU8l2iTerOIq9Ztba5GfsIPpqmvRGexulmPvmjuOVNPBRzX X-Received: by 2002:a17:907:c61a:b0:94e:6921:af2c with SMTP id ud26-20020a170907c61a00b0094e6921af2cmr1211148ejc.0.1681302654276; Wed, 12 Apr 2023 05:30:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681302654; cv=none; d=google.com; s=arc-20160816; b=CAdfc4MPMiAPJ727WHIKRLVOrENxMg500FOXUa7wy/VpdjdW6fnmnNPvgGO2NIZs3Y 6s1LHOXyv5AG08PeMbZzvEEwFdGtky6pnXlSNjtIJBC29Q20I0dpiRM32atojRjuo0bK l8z42XsONeC06JpEF9DT47v7F0a4ou1gFZ8Oi3fe69r+V2dgT6wCGtluXOxpzff3Tz/a VvucrlfxUS1XyUL3fMeG+w1iHw4mwJ2/BUWYvmtO6sCkmeVEB33kL0ljETkn4ZaCv2m8 BGWJKIWgbAs7Hkb2eyKihElUsmKffnx61FQYqW7jHD07QaZt/FuCbt3KX7SyXvSfSHp2 X6jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=/L7IuJgd17qn7noO3Hw/HWr3+Mepvreri/Uwdz/CNWM=; b=oMWj07/YMeeceaE5flMf4WSUaVLupO95lC27zb5+8vG7Tj81i+Dvtag2VFAxymGbNq H9DdtzPIWixYYoiB+Qz/dYFPqQK+UCzvhlut+nvMfl0yiLTb6DNN1eC6ptN8J6IkpdFw gMKrVOE5h1C0md8JVvTAf3Koe07aClmcgVMYQSzGp2yOyUylWhgoiV0MLfNVNF8fBsos jwEy+KIkSbO+q/KMLWB4prTMskmC8HTG6RZiApUZI4cq9jYc0SVtIddqvZok1Dihzb6E ZpPF2c2tfHKBvxQfnSXjU+6/tGy92irIMwGWbQQP8yU0xCGEu6G5uDYABRt8KS2ImQYJ w3xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=kdArxfq9; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a16-20020a17090682d000b0094b0ba5c017si2636905ejy.274.2023.04.12.05.30.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Apr 2023 05:30:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=kdArxfq9; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BD776385416A for ; Wed, 12 Apr 2023 12:30:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD776385416A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681302607; bh=/L7IuJgd17qn7noO3Hw/HWr3+Mepvreri/Uwdz/CNWM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=kdArxfq9+UAeo2aw12ASPs5TBCywOb3Ya8LDJFZz0UkdcrvEAsvoAx0XtQEzVVdwM zt/a518nkUABnoeZ1kE/ansq0eWXlrcwoDbCtIL9d7iYwjFddFHXCwc7oMmI3yFcXj 5dwd5L4yuqgj8QznDtGCN7+O5mAoQPawPYasHFKE= 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 B76F838582BD for ; Wed, 12 Apr 2023 12:28:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B76F838582BD Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-605-31eBX285Po-VTdkCnneeCw-1; Wed, 12 Apr 2023 08:28:53 -0400 X-MC-Unique: 31eBX285Po-VTdkCnneeCw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F3934185A792; Wed, 12 Apr 2023 12:28:52 +0000 (UTC) Received: from localhost (unknown [10.33.36.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id BE9FA40C6E70; Wed, 12 Apr 2023 12:28:52 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Initialize all members of basic_endpoint union [PR109482] Date: Wed, 12 Apr 2023 13:28:56 +0100 Message-Id: <20230412122856.114242-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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_H2, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762973612170600726?= X-GMAIL-MSGID: =?utf-8?q?1762973612170600726?= Tested x86_64-linux and sparc-solaris2.11. Pushed to trunk. -- >8 -- On Solaris the in_addr struct contains a union and value-initializing it does not make the s_addr member active. This means we can't access that member later during constant evaluation. Make the constructors explicitly set every member that we might want to read later in constexpr member functions. This means even the default constructor can only be constexpr for C++20, because we can't change the active member of a union in older standards. libstdc++-v3/ChangeLog: PR libstdc++/109482 * include/experimental/internet (basic_endpoint::basic_endpoint()): Ensure that the required union members are active. Only define as constexpr for C++20 and later. (basic_endpoint::basic_endpoint(const protocol_type&, port_type)): Likewise. * testsuite/experimental/net/internet/endpoint/cons.cc: Only check constexpr default constructor for C++20 and later. * testsuite/experimental/net/internet/endpoint/extensible.cc: Likewise. --- libstdc++-v3/include/experimental/internet | 22 ++++++++++++--- .../net/internet/endpoint/cons.cc | 27 +++++++++---------- .../net/internet/endpoint/extensible.cc | 4 +++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index eb23ae21cdc..1f63c61ce85 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -1512,9 +1512,14 @@ namespace ip // constructors: - constexpr + _GLIBCXX20_CONSTEXPR basic_endpoint() noexcept : _M_data() - { _M_data._M_v4.sin_family = protocol_type::v4().family(); } + { + _M_data._M_v4.sin_family = protocol_type::v4().family(); + // If in_addr contains a union, make the correct member active: + if (std::__is_constant_evaluated()) + std::_Construct(&_M_data._M_v4.sin_addr.s_addr); + } _GLIBCXX20_CONSTEXPR basic_endpoint(const protocol_type& __proto, @@ -1523,19 +1528,25 @@ namespace ip { if (__proto == protocol_type::v4()) { - _M_data._M_v4.sin_family = __proto.family(); + _M_data._M_v4.sin_family = protocol_type::v4().family(); _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); + if (std::__is_constant_evaluated()) + std::_Construct(&_M_data._M_v4.sin_addr.s_addr); } else if (__proto == protocol_type::v6()) { std::_Construct(&_M_data._M_v6); _M_data._M_v6.sin6_family = __proto.family(); _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num); + _M_data._M_v6.sin6_scope_id = 0; + if (std::__is_constant_evaluated()) + std::_Construct(&_M_data._M_v6.sin6_addr.s6_addr); } else { __glibcxx_assert(__proto == protocol_type::v4() || __proto == protocol_type::v6()); + } } @@ -1548,13 +1559,16 @@ namespace ip { _M_data._M_v4.sin_family = protocol_type::v4().family(); _M_data._M_v4.sin_port = address_v4::_S_hton_16(__port_num); - _M_data._M_v4.sin_addr.s_addr = __addr._M_v4._M_addr; + std::_Construct(&_M_data._M_v4.sin_addr.s_addr, + __addr._M_v4._M_addr); } else { std::_Construct(&_M_data._M_v6); _M_data._M_v6.sin6_family = protocol_type::v6().family(); _M_data._M_v6.sin6_port = address_v4::_S_hton_16(__port_num); + if (std::__is_constant_evaluated()) + std::_Construct(&_M_data._M_v6.sin6_addr.s6_addr); uint8_t* __s6a = _M_data._M_v6.sin6_addr.s6_addr; for (int __i = 0; __i < 16; ++__i) __s6a[__i] = __addr._M_v6._M_bytes[__i]; diff --git a/libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc b/libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc index b4bef88b4a3..d54b0c9550b 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/endpoint/cons.cc @@ -7,7 +7,10 @@ using namespace std::experimental::net; -constexpr void +#if __cplusplus >= 202002 +constexpr +#endif +void test_default() { ip::tcp::endpoint t1; @@ -57,23 +60,19 @@ test_addr() VERIFY( t2.port() == 80 ); } -constexpr bool -test_constexpr() -{ - test_default(); -#if __cplusplus >= 202002 - // Non-default basic_endpoint constructors are only constexpr in C++20. - test_proto(); - test_addr(); -#endif - return true; -} - int main() { test_default(); test_proto(); test_addr(); - static_assert( test_constexpr(), "valid in constant expressions" ); +#if __cplusplus >= 202002 + // basic_endpoint constructors are only constexpr in C++20. + constexpr bool b = []{ + test_default(); + test_proto(); + test_addr(); + return true; + }(); +#endif } diff --git a/libstdc++-v3/testsuite/experimental/net/internet/endpoint/extensible.cc b/libstdc++-v3/testsuite/experimental/net/internet/endpoint/extensible.cc index d205024c799..ffc43cf17b6 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/endpoint/extensible.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/endpoint/extensible.cc @@ -11,8 +11,12 @@ using namespace std::experimental::net; void test_extensible() { +#if __cplusplus >= 202002L static_assert(ip::tcp::endpoint().capacity() == sizeof(sockaddr_in6), "ip::tcp::endpoint::capacity() can store a sockaddr_in6"); +#else + VERIFY( ip::tcp::endpoint().capacity() == sizeof(sockaddr_in6) ); +#endif ip::tcp::endpoint t1(ip::tcp::v4(), 22); VERIFY(t1.size() == sizeof(sockaddr_in));