From patchwork Fri May 6 22:56:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1627790 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=TKWB9tkY; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from 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 RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kw5c54G6kz9sGJ for ; Sat, 7 May 2022 08:59:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C774E3948A5F for ; Fri, 6 May 2022 22:59:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C774E3948A5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651877968; bh=ngrUK7pXD0R9f/AuQWU03awr34TM45SMjy1hBndpDlo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=TKWB9tkYEUIsvw6iAelbU5gIy5CNumiWQ1ldh8Nlo5Q5+Qw998Hf4aTPTt0/+c9L6 IsidzsSNT1RzkSe9MXHZ7WxdjCHkdQ9ZA73AaCz01dDC4mXyqk2aMAQNayUNtqYGDQ U9R1NZ4T/qwTjXRkAveWE2j0PLUivchd1utQDshc= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id D064039490AD for ; Fri, 6 May 2022 22:57:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D064039490AD 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-158-hlRJ7MFdNlOgxgMU9tDEgA-1; Fri, 06 May 2022 18:57:01 -0400 X-MC-Unique: hlRJ7MFdNlOgxgMU9tDEgA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A245B800B21; Fri, 6 May 2022 22:57:00 +0000 (UTC) Received: from localhost (unknown [10.33.36.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6BEDB111E3E9; Fri, 6 May 2022 22:57:00 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 1/2] libstdc++: Fix deserialization for std::normal_distribution [PR105502] Date: Fri, 6 May 2022 23:56:58 +0100 Message-Id: <20220506225659.3368784-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. Backports to all branches needed. -- >8 -- This fixes a regression in std::normal_distribution deserialization that caused the object to be left unchanged if the __state_avail value read from the stream was false. libstdc++-v3/ChangeLog: PR libstdc++/105502 * include/bits/random.tcc (operator>>(basic_istream&, normal_distribution&)): Update state when __state_avail is false. * testsuite/26_numerics/random/normal_distribution/operators/serialize.cc: Check that deserialized object equals serialized one. --- libstdc++-v3/include/bits/random.tcc | 2 +- .../operators/serialize.cc | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index 6c72e991007..87a16a21336 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -1961,7 +1961,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION bool __saved_avail; if (__is >> __mean >> __stddev >> __saved_avail) { - if (__saved_avail && (__is >> __x._M_saved)) + if (!__saved_avail || (__is >> __x._M_saved)) { __x._M_saved_available = __saved_avail; __x.param(param_type(__mean, __stddev)); diff --git a/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/serialize.cc b/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/serialize.cc index 9d8f8278fb3..d4f9f374643 100644 --- a/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/serialize.cc +++ b/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/serialize.cc @@ -25,6 +25,7 @@ #include #include +#include void test01() @@ -37,10 +38,43 @@ test01() str << u; str >> v; + VERIFY( u == v ); +} + +void +test_pr105502() +{ + // PR libstdc++/105502 std::normal_distribution deserialization issue + std::stringstream str; + std::normal_distribution<> d{1, 2}, d2; + std::minstd_rand0 g; + str << d; + VERIFY( str ); + str >> d2; + VERIFY( str ); + VERIFY( d == d2 ); + + (void) d(g); // sets d._M_saved_available = true + str.str(""); + str.clear(); + str << d; + VERIFY( str ); + str >> d2; + VERIFY( str ); + VERIFY( d == d2 ); + + (void) d(g); // sets d._M_saved_available = false + str.str(""); + str.clear(); + str << d; + VERIFY( str ); + str >> d2; + VERIFY( str ); + VERIFY( d == d2 ); } int main() { test01(); - return 0; + test_pr105502(); } From patchwork Fri May 6 22:56:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1627789 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=j9+QWIfi; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4Kw5ZL1Sqsz9sG6 for ; Sat, 7 May 2022 08:58:00 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 48FD03949099 for ; Fri, 6 May 2022 22:57:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 48FD03949099 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651877877; bh=l+khDtRDXUdOzWMa/pC9YH0ajHH06Ms2ghVawrER4Zo=; 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=j9+QWIfisV/BlPZWMHSqjxAo/6vY/JoIl5uk0XQ98xDp5RoLqLGrVWbQaBs/TgjRj dlqfjCnAWbKQj3aWoGHr2aqQNaBJj87L4OlHbvt8r1moOgvie57N80JQCvLAJ9g5ZZ s/s+ei7SJGJpctoqGWia7/NEGFRF9Nlcg/PnJncY= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 2125D383801F for ; Fri, 6 May 2022 22:57:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2125D383801F 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-178-VzFv8I2fPPerlI8zk8d7ew-1; Fri, 06 May 2022 18:57:01 -0400 X-MC-Unique: VzFv8I2fPPerlI8zk8d7ew-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7E6E8101AA42; Fri, 6 May 2022 22:57:01 +0000 (UTC) Received: from localhost (unknown [10.33.36.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 45DA720296A9; Fri, 6 May 2022 22:57:01 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 2/2] libstdc++: Simplify std::normal_distribution equality operator Date: Fri, 6 May 2022 23:56:59 +0100 Message-Id: <20220506225659.3368784-2-jwakely@redhat.com> In-Reply-To: <20220506225659.3368784-1-jwakely@redhat.com> References: <20220506225659.3368784-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.4 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. -- >8 -- libstdc++-v3/ChangeLog: * include/bits/random.tcc (operator==): Only check normal_distribution::_M_saved_available once. * testsuite/26_numerics/random/normal_distribution/operators/equal.cc: Check equality after state changes. * testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-error lineno. --- libstdc++-v3/include/bits/random.tcc | 10 +--------- .../normal_distribution/operators/equal.cc | 20 +++++++++++++++++++ .../26_numerics/random/pr60037-neg.cc | 2 +- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc index 87a16a21336..cb1d3675783 100644 --- a/libstdc++-v3/include/bits/random.tcc +++ b/libstdc++-v3/include/bits/random.tcc @@ -1907,15 +1907,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { if (__d1._M_param == __d2._M_param && __d1._M_saved_available == __d2._M_saved_available) - { - if (__d1._M_saved_available - && __d1._M_saved == __d2._M_saved) - return true; - else if(!__d1._M_saved_available) - return true; - else - return false; - } + return __d1._M_saved_available ? __d1._M_saved == __d2._M_saved : true; else return false; } diff --git a/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/equal.cc b/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/equal.cc index a3435232961..81534e95797 100644 --- a/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/equal.cc +++ b/libstdc++-v3/testsuite/26_numerics/random/normal_distribution/operators/equal.cc @@ -34,8 +34,28 @@ test01() VERIFY( !(u == v) ); } +void +test02() +{ + std::normal_distribution u(5.0, 2.0), v(u); + VERIFY( u == v ); + u.reset(); + VERIFY( u == v ); + + std::minstd_rand0 g1, g2; + (void) u(g1); // u._M_saved_available = true + VERIFY( !(u == v) ); + (void) v(g2); // v._M_saved_available = true + VERIFY( u == v ); + u.reset(); // u._M_saved_available = false + VERIFY( !(u == v) ); + v.reset(); // v._M_saved_available = false + VERIFY( u == v ); +} + int main() { test01(); + test02(); return 0; } diff --git a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc index 3ab9c44232e..c58f480640f 100644 --- a/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc +++ b/libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc @@ -12,4 +12,4 @@ auto x = std::generate_canonical