From patchwork Fri Feb 24 14:28:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 61342 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp941261wrd; Fri, 24 Feb 2023 06:31:47 -0800 (PST) X-Google-Smtp-Source: AK7set8nlwJz3sAH6P9QGyEf/W4umBsi9uD09EqbHYbhYl/OjYy+p5++/KebYY1Sdbyl757WVbCi X-Received: by 2002:aa7:cfd4:0:b0:4ab:1c69:5c4 with SMTP id r20-20020aa7cfd4000000b004ab1c6905c4mr13832173edy.26.1677249107330; Fri, 24 Feb 2023 06:31:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677249107; cv=none; d=google.com; s=arc-20160816; b=JS6ukNv+dUlRe8l2Hk4sVqN6yaZRic+Bcr0N/MnBGPbwPdZ7XKYUnJf0CMvZXZ8UvA eH1D0rWxgrsbI1sMmNiP75AI9o3YBKelPd1CAyKlpTXpavzRYNMpFa14WHsKDlapi4Hq Kbjrt5jVx2jtHRxzMAZxezMLcxRZrugpB42up8YfGodlKzEEfPZaJG6oav8K7Mi5lgPm Cw03j8ny9JqGlRT1tlqqonhnGxxNkZDTTj7yRte1fhJ3CnDzvsHETYa075HiNXNimWO9 zVx5MoPJSk2ctMRhm54QHD+JIdsB3cKYjBsa+yV5ZgAngfMR6ktPRxIyQ2giNcjGVhBM 7mxA== 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:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=tapazxQK7dT5gP+5bx/z1qB45sYMtrQjh+/wIWZhV6Y=; b=P/cFWDPBCTuxWW1dnHz8xbwJlPENudhy0iX+9EAkBvQFDGWe8ALTrtn3FmTEJm6pSk 4cJwc8AzcdpGOu6t77hZssdHpLdIRbQHhPbFB8ytMQX3dycBwo2phbPWlUiQ1UAIkQsV R4wDpT3LGZJivMIdGCNYbCgOTvO9PpdTRn21ZjEBZx8I7/59FQAuSr2LXEv4dNHteFpJ va6JguPta/1qaqXY3eUH2vuWsP+5hQmJnjlTg82u2rhZQaZMJ9jhEWJuSBfxFuFSCrGY UKj3t+o2wM0IE39wV7X7eFJC2ogTj1rZeulaBF+HuzwzqDZ9LzS4axB7ZbKj38+6aDNw J/tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=ycR6x39E; 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 bo13-20020a0564020b2d00b004ad15601457si10590130edb.629.2023.02.24.06.31.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Feb 2023 06:31:47 -0800 (PST) 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=ycR6x39E; 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 BFBF038493EC for ; Fri, 24 Feb 2023 14:29:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BFBF038493EC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677248998; bh=tapazxQK7dT5gP+5bx/z1qB45sYMtrQjh+/wIWZhV6Y=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ycR6x39EtcgNK2ZGhRtFnb4QKtPtAVZYZ1Tadm23S3MDRSeWLXo1hylPa1/f8HxoS MEHTMC/RYkSJOsRqc2yd9dlUKIYR+RGAjYyRqw9un/+C0xjs/uLpqmZDHJVTiknchK 27xI96jrvTlkoig0a/6/nr36HGDLiAeUEHxjIVLE= 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 AB636385559A for ; Fri, 24 Feb 2023 14:28:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB636385559A 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-541-Ens_XiQLOvSuL3Ydp46tzg-1; Fri, 24 Feb 2023 09:28:10 -0500 X-MC-Unique: Ens_XiQLOvSuL3Ydp46tzg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 035F2887409; Fri, 24 Feb 2023 14:28:10 +0000 (UTC) Received: from localhost (unknown [10.33.36.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD5B6492C3E; Fri, 24 Feb 2023 14:28:09 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 2/5] libstdc++: Fix conversion to/from net::ip::address_v4::bytes_type Date: Fri, 24 Feb 2023 14:28:05 +0000 Message-Id: <20230224142808.714075-2-jwakely@redhat.com> In-Reply-To: <20230224142808.714075-1-jwakely@redhat.com> References: <20230224142808.714075-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 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=unavailable 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?1758723159649261447?= X-GMAIL-MSGID: =?utf-8?q?1758723159649261447?= Tested x86_64-linux. Pushed to trunk. -- >8 -- I messed up the endianness of the address_v4::bytes_type array, which should always be in network byte order. We can just use bit_cast to convert the _M_addr member to/from bytes_type. libstdc++-v3/ChangeLog: * include/experimental/internet (address_4(const bytes_type&)): Use __builtin_bit_cast if available, otherwise convert to network byte order. (address_v4::to_bytes()): Likewise, but convert from network byte order. * testsuite/experimental/net/internet/address/v4/cons.cc: Fix incorrect tests. Check for constexpr too. * testsuite/experimental/net/internet/address/v4/creation.cc: Likewise. * testsuite/experimental/net/internet/address/v4/members.cc: Check that bytes_type is a standard-layout type. --- libstdc++-v3/include/experimental/internet | 20 ++++++++--- .../net/internet/address/v4/cons.cc | 33 ++++++++++++------- .../net/internet/address/v4/creation.cc | 24 +++++++++++--- .../net/internet/address/v4/members.cc | 3 ++ 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/libstdc++-v3/include/experimental/internet b/libstdc++-v3/include/experimental/internet index 08bd0db4bb2..3fd200251fa 100644 --- a/libstdc++-v3/include/experimental/internet +++ b/libstdc++-v3/include/experimental/internet @@ -198,7 +198,12 @@ namespace ip constexpr address_v4(const bytes_type& __b) - : _M_addr((__b[0] << 24) | (__b[1] << 16) | (__b[2] << 8) | __b[3]) +#if __has_builtin(__builtin_bit_cast) + : _M_addr(__builtin_bit_cast(uint_type, __b)) +#else + : _M_addr(_S_hton_32((__b[0] << 24) | (__b[1] << 16) + | (__b[2] << 8) | __b[3])) +#endif { } explicit constexpr @@ -227,12 +232,17 @@ namespace ip constexpr bytes_type to_bytes() const noexcept { +#if __has_builtin(__builtin_bit_cast) + return __builtin_bit_cast(bytes_type, _M_addr); +#else + auto __host = to_uint(); return bytes_type{ - (_M_addr >> 24) & 0xFF, - (_M_addr >> 16) & 0xFF, - (_M_addr >> 8) & 0xFF, - _M_addr & 0xFF + (__host >> 24) & 0xFF, + (__host >> 16) & 0xFF, + (__host >> 8) & 0xFF, + __host & 0xFF }; +#endif } constexpr uint_type diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc index 65f23642de4..af9fef2215e 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/cons.cc @@ -24,41 +24,45 @@ using std::experimental::net::ip::address_v4; -void +#if __cplusplus < 202002L +// Naughty, but operator== for std::array is not constexpr until C++20. +constexpr bool +operator==(const address_v4::bytes_type& lhs, const address_v4::bytes_type& rhs) +{ + return lhs[0] == rhs[0] && lhs[1] == rhs[1] + && lhs[2] == rhs[2] && lhs[3] == rhs[3]; +} +#endif + +constexpr void test01() { - bool test __attribute__((unused)) = false; - address_v4 a0; VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); } -void +constexpr void test02() { - bool test __attribute__((unused)) = false; - address_v4 a0{ address_v4::bytes_type{} }; VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); address_v4::bytes_type b1{ 1, 2, 3, 4 }; address_v4 a1{ b1 }; - VERIFY( a1.to_uint() == ntohl((1 << 24) | (2 << 16) | (3 << 8) | 4) ); + VERIFY( a1.to_uint() == ((1 << 24) | (2 << 16) | (3 << 8) | 4) ); VERIFY( a1.to_bytes() == b1 ); } -void +constexpr void test03() { - bool test __attribute__((unused)) = false; - address_v4 a0{ 0u }; VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); - address_v4::uint_type u1 = ntohl((5 << 24) | (6 << 16) | (7 << 8) | 8); + address_v4::uint_type u1 = (5 << 24) | (6 << 16) | (7 << 8) | 8; address_v4 a1{ u1 }; VERIFY( a1.to_uint() == u1 ); VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) ); @@ -70,4 +74,11 @@ main() test01(); test02(); test03(); + + constexpr bool c = []{ + test01(); + test02(); + test03(); + return true; + }; } diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc index 441c832bf54..84aebbb7adc 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/creation.cc @@ -25,7 +25,17 @@ namespace net = std::experimental::net; using net::ip::address_v4; -void +#if __cplusplus < 202002L +// Naughty, but operator== for std::array is not constexpr until C++20. +constexpr bool +operator==(const address_v4::bytes_type& lhs, const address_v4::bytes_type& rhs) +{ + return lhs[0] == rhs[0] && lhs[1] == rhs[1] + && lhs[2] == rhs[2] && lhs[3] == rhs[3]; +} +#endif + +constexpr void test01() { auto a0 = make_address_v4( address_v4::bytes_type{} ); @@ -34,18 +44,18 @@ test01() address_v4::bytes_type b1{ 1, 2, 3, 4 }; auto a1 = make_address_v4( b1 ); - VERIFY( a1.to_uint() == ntohl((1 << 24) | (2 << 16) | (3 << 8) | 4) ); + VERIFY( a1.to_uint() == ((1 << 24) | (2 << 16) | (3 << 8) | 4) ); VERIFY( a1.to_bytes() == b1 ); } -void +constexpr void test02() { auto a0 = net::ip::make_address_v4(0u); VERIFY( a0.to_uint() == 0 ); VERIFY( a0.to_bytes() == address_v4::bytes_type{} ); - address_v4::uint_type u1 = ntohl((5 << 24) | (6 << 16) | (7 << 8) | 8); + address_v4::uint_type u1 = ((5 << 24) | (6 << 16) | (7 << 8) | 8); auto a1 = net::ip::make_address_v4( u1 ); VERIFY( a1.to_uint() == u1 ); VERIFY( a1.to_bytes() == address_v4::bytes_type( 5, 6, 7, 8 ) ); @@ -84,4 +94,10 @@ main() test01(); test02(); test03(); + + constexpr bool c = []{ + test01(); + test02(); + return true; + }; } diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc index c40a8103664..ac59405c599 100644 --- a/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc +++ b/libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc @@ -26,6 +26,9 @@ using std::experimental::net::ip::address_v4; +static_assert(std::is_standard_layout::value, + "net::ip::address_v4::bytes_type is a standard layout type"); + constexpr bool test01() {