From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 8E2503857715 for ; Wed, 12 Apr 2023 12:28:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8E2503857715 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681302536; 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=/L7IuJgd17qn7noO3Hw/HWr3+Mepvreri/Uwdz/CNWM=; b=hvxhCxEfGLPwcm7jwAu1X+UmE0ezn+fDDF9XCZF/k8J2A0ErmfC/v/XfpvRm5yWzGfnpkx jHSJSnHVOGtN1j0VD0c2R5e9D2udNZpYoONlFrB2oacxdFbOQBSes3t/aYVeC5m0yiAPdn 2qwRmZ1ljFX02vHfNOCMVd/JDm47BqM= 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) From: Jonathan Wakely 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 Content-Type: text/plain Content-Transfer-Encoding: 8bit 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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)); -- 2.39.2