public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed 1/2] libstdc++: Fix deserialization for std::normal_distribution [PR105502]
@ 2022-05-06 22:56 Jonathan Wakely
  2022-05-06 22:56 ` [committed 2/2] libstdc++: Simplify std::normal_distribution equality operator Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2022-05-06 22:56 UTC (permalink / raw)
  To: libstdc++, 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<C,T>&, normal_distribution<R>&)):
	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 <random>
 #include <sstream>
+#include <testsuite_hooks.h>
 
 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();
 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [committed 2/2] libstdc++: Simplify std::normal_distribution equality operator
  2022-05-06 22:56 [committed 1/2] libstdc++: Fix deserialization for std::normal_distribution [PR105502] Jonathan Wakely
@ 2022-05-06 22:56 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2022-05-06 22:56 UTC (permalink / raw)
  To: libstdc++, 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<double> 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<std::size_t,
 
 // { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 169 }
 
-// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3356 }
+// { dg-error "static assertion failed: template argument must be a floating point type" "" { target *-*-* } 3348 }
-- 
2.34.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-06 22:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-06 22:56 [committed 1/2] libstdc++: Fix deserialization for std::normal_distribution [PR105502] Jonathan Wakely
2022-05-06 22:56 ` [committed 2/2] libstdc++: Simplify std::normal_distribution equality operator Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).