From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 112DE3948A5B; Fri, 6 May 2022 22:59:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 112DE3948A5B MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-8354] libstdc++: Fix deserialization for std::normal_distribution [PR105502] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: e6b1ac334ac61f72536f3479f735ea3514f1309d X-Git-Newrev: 03257e7ee31385a364b711898c8f1510fd638efe Message-Id: <20220506225910.112DE3948A5B@sourceware.org> Date: Fri, 6 May 2022 22:59:10 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 May 2022 22:59:10 -0000 https://gcc.gnu.org/g:03257e7ee31385a364b711898c8f1510fd638efe commit r12-8354-g03257e7ee31385a364b711898c8f1510fd638efe Author: Jonathan Wakely Date: Fri May 6 21:19:17 2022 +0100 libstdc++: Fix deserialization for std::normal_distribution [PR105502] 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. (cherry picked from commit 909ef4e2727ddc50a32d6ad379a1f1ccc1043c6a) Diff: --- libstdc++-v3/include/bits/random.tcc | 2 +- .../normal_distribution/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(); }