From patchwork Sun Jan 23 22:50:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 50375 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 42DEE3858400 for ; Sun, 23 Jan 2022 22:55:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 42DEE3858400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642978523; bh=rYZhKJJE/rbKfTFEBRxVPRg1VE3I7pjkSXePMAInscg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=lngPLN22nrpxTzb3lKUWAG4+wdM7xWnCCd7/9BTsTBPa1YRegsAYNzp7Mt4FBncum +88AQ68HXdYe6V1hdGONnUiTnO9sOutMmbz+8FY3STzbAVDxE8nlZPkIftuHuKRDvR zlO/10Y1mYbX9043Qyi9bUcCE4GQ8+M0QNA4SJAA= 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 9EEF73858410 for ; Sun, 23 Jan 2022 22:50:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9EEF73858410 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-205-nVzWWTNAM52aH6WXt05U3w-1; Sun, 23 Jan 2022 17:50:20 -0500 X-MC-Unique: nVzWWTNAM52aH6WXt05U3w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D6F77835B47; Sun, 23 Jan 2022 22:50:19 +0000 (UTC) Received: from localhost (unknown [10.33.36.2]) by smtp.corp.redhat.com (Postfix) with ESMTP id 842E266E03; Sun, 23 Jan 2022 22:50:19 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::spanstream move assignment [PR104032] Date: Sun, 23 Jan 2022 22:50:18 +0000 Message-Id: <20220123225018.641966-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. libstdc++-v3/ChangeLog: PR libstdc++/104032 * include/std/spanstream (basic_spanbuf(basic_spanbuf&&)): Use mem-initializer for _M_buf. (basic_spanbuf::Operator=(basic_spanbuf&&)): Fix ill-formed member access. * testsuite/27_io/spanstream/2.cc: New test. --- libstdc++-v3/include/std/spanstream | 15 ++- libstdc++-v3/testsuite/27_io/spanstream/2.cc | 113 +++++++++++++++++++ 2 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/testsuite/27_io/spanstream/2.cc diff --git a/libstdc++-v3/include/std/spanstream b/libstdc++-v3/include/std/spanstream index 240866ff26f..000bda52a1e 100644 --- a/libstdc++-v3/include/std/spanstream +++ b/libstdc++-v3/include/std/spanstream @@ -75,10 +75,17 @@ template> basic_spanbuf(const basic_spanbuf&) = delete; - /// Move constructor. In this implementation `rhs` is left unchanged. + /** Move constructor. + * + * Transfers the buffer and pointers into the get and put areas from + * `__rhs` to `*this`. + * + * In this implementation `rhs` is left unchanged, + * but that is not guaranteed by the standard. + */ basic_spanbuf(basic_spanbuf&& __rhs) - : __streambuf_type(__rhs), _M_mode(__rhs._M_mode) - { span(__rhs._M_buf); } + : __streambuf_type(__rhs), _M_mode(__rhs._M_mode), _M_buf(__rhs._M_buf) + { } // [spanbuf.assign], assignment and swap basic_spanbuf& operator=(const basic_spanbuf&) = delete; @@ -86,7 +93,7 @@ template> basic_spanbuf& operator=(basic_spanbuf&& __rhs) { - basic_spanbuf(std::move(__rhs))->swap(*this); + basic_spanbuf(std::move(__rhs)).swap(*this); return *this; } diff --git a/libstdc++-v3/testsuite/27_io/spanstream/2.cc b/libstdc++-v3/testsuite/27_io/spanstream/2.cc new file mode 100644 index 00000000000..a13a50b0dce --- /dev/null +++ b/libstdc++-v3/testsuite/27_io/spanstream/2.cc @@ -0,0 +1,113 @@ +// { dg-options "-std=gnu++23" } +// { dg-do run { target c++23 } } + +#include +#include + +using std::ispanstream; +using std::ospanstream; +using std::span; + +void +test_move() +{ + char c; + { + const char str[] = "chars"; + std::ispanstream a(str); + std::ispanstream b = std::move(a); + VERIFY( b.span().data() == str && b.span().size() == 6 ); + VERIFY( b >> c ); + VERIFY( c == 'c' ); + + a = std::move(b); + VERIFY( a.span().data() == str && a.span().size() == 6 ); + VERIFY( a >> c >> c ); + VERIFY( c == 'a' ); + } + + { + char buf[10] = {}; + std::ospanstream a(buf); + std::ospanstream b = std::move(a); + VERIFY( b << 'c' ); + VERIFY( buf[0] == 'c' ); + VERIFY( !std::char_traits::compare(buf, "c", 2) ); + + a = std::move(b); + VERIFY( a << 'h' << 'a' << "rs" ); + VERIFY( !std::char_traits::compare(buf, "chars", 6) ); + } + + { + char buf[10] = {}; + std::spanstream a(buf); + std::spanstream b = std::move(a); + VERIFY( b.span().empty() ); + VERIFY( b << 'c' ); + VERIFY( buf[0] == 'c' ); + VERIFY( !std::char_traits::compare(buf, "c", 2) ); + VERIFY( b.span().data() == buf && b.span().size() == 1 ); + VERIFY( b >> c ); + VERIFY( c == 'c' ); + + a = std::move(b); + VERIFY( a.span().data() == buf && a.span().size() == 1 ); + VERIFY( a << 'h' << 'a' << "rs" ); + VERIFY( !std::char_traits::compare(buf, "chars", 6) ); + VERIFY( a.span().data() == buf && a.span().size() == 5 ); + } +} + +void +test_swap() +{ + { + const char str1[] = "chars"; + const char str2[] = "STRING"; + std::ispanstream a(str1); + std::ispanstream b(str2); + a.swap(b); + VERIFY( a.span().data() == str2 && a.span().size() == 7 ); + VERIFY( b.span().data() == str1 && b.span().size() == 6 ); + char c; + VERIFY( a >> c ); + VERIFY( c == 'S' ); + VERIFY( b >> c ); + VERIFY( c == 'c' ); + + swap(a, b); + VERIFY( a.span().data() == str1 && a.span().size() == 6 ); + VERIFY( b.span().data() == str2 && b.span().size() == 7 ); + VERIFY( a >> c >> c ); + VERIFY( c == 'a' ); + VERIFY( b >> c >> c ); + VERIFY( c == 'R' ); + } + + { + char buf1[] = "xxxxxxxxxxxxxxx"; + char buf2[] = "xxxxxxxxxxxxxxx"; + std::ospanstream a(buf1); + std::ospanstream b(buf2); + a.swap(b); + VERIFY( a << "STR" ); + VERIFY( !std::char_traits::compare(buf2, "STRx", 4) ); + VERIFY( b << 'c' << 'h' ); + VERIFY( !std::char_traits::compare(buf1, "chx", 3) ); + + swap(a, b); + VERIFY( a.span().size() == 2 ); + VERIFY( b.span().size() == 3 ); + VERIFY( a << 'a' << "rs" ); + VERIFY( !std::char_traits::compare(buf1, "charsx", 6) ); + VERIFY( b << "IN" << 'G' ); + VERIFY( !std::char_traits::compare(buf2, "STRINGx", 7) ); + } +} + +int main() +{ + test_move(); + test_swap(); +}