public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/105502] New: std::normal_distribution deserialization issue
@ 2022-05-06  8:28 frank.marschner at cognitec dot com
  2022-05-06 14:35 ` [Bug libstdc++/105502] [9/12/11/12/13 Regression] " redi at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: frank.marschner at cognitec dot com @ 2022-05-06  8:28 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

            Bug ID: 105502
           Summary: std::normal_distribution deserialization issue
           Product: gcc
           Version: 9.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: frank.marschner at cognitec dot com
  Target Milestone: ---

Found an issue deserializing an serialized instance of
std::normal_distribution.
Depending on the amount of random numbers generated deserialization
works or not.

The issue was introduced with commit
160e95dc3d73329d2367fb405ef9f0e12bd2cc7b on file bits/random.tcc from Jan 9,
2020.

The issue is in the implementation of

template< class CharT, class Traits >
friend std::basic_istream<CharT,Traits>&
    operator>>( std::basic_istream<CharT,Traits>& ist,
                normal_distribution& d );

The commit introduces wrong initialization of members param (mean and
stddev) and also _M_saved_available depending on __saved_avail.


The following example demonstrates the issue
---8<----------------------------------------------------------------
#include <iostream>
#include <sstream>
#include <random>

void test_serialization( const std::normal_distribution<>& nd) 
{
   std::stringstream sss;
   sss << nd;

   std::normal_distribution<> newND;
   sss >> newND;

   if( nd == newND) {
       std::cout << "Serialization successful." << std::endl;
   } else {
       std::cout << "Serialization not successful." << std::endl;
   }
}

int main( int argc, char** argv) 
{
   std::mt19937 gen{ 42};

   std::normal_distribution<> d{ 4, 2};

   test_serialization( d);
   const auto result0 = d(gen);
   test_serialization( d);
   const auto result1 = d(gen);
   test_serialization( d);
   const auto result2 = d(gen);
   test_serialization( d);
}
---8<----------------------------------------------------------------

The output running the code shows

Serialization not successful.
Serialization successful.
Serialization not successful.
Serialization successful.

The issues covers gcc versions since 8.4 up to recent.

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

* [Bug libstdc++/105502] [9/12/11/12/13 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
@ 2022-05-06 14:35 ` redi at gcc dot gnu.org
  2022-05-06 20:51 ` [Bug libstdc++/105502] [9/10/11/12/13 " redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-05-06 14:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org
      Known to work|                            |8.3.0, 9.2.0
            Summary|std::normal_distribution    |[9/12/11/12/13 Regression]
                   |deserialization issue       |std::normal_distribution
                   |                            |deserialization issue
   Last reconfirmed|                            |2022-05-06
           Keywords|                            |wrong-code
      Known to fail|                            |10.1.0, 11.1.0, 12.1.0,
                   |                            |13.0, 8.4.0, 9.3.0
             Status|UNCONFIRMED                 |ASSIGNED
     Ever confirmed|0                           |1
   Target Milestone|---                         |9.5

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Ouch, yes, thanks for the report.

This is the fix:

--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -1962,10 +1962,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       if (__is >> __mean >> __stddev >> __saved_avail)
        {
          if (__saved_avail && (__is >> __x._M_saved))
-           {
-             __x._M_saved_available = __saved_avail;
-             __x.param(param_type(__mean, __stddev));
-           }
+           __x._M_saved_available = __saved_avail;
+
+         if (__is)
+           __x.param(param_type(__mean, __stddev));
        }

       __is.flags(__flags);

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

* [Bug libstdc++/105502] [9/10/11/12/13 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
  2022-05-06 14:35 ` [Bug libstdc++/105502] [9/12/11/12/13 Regression] " redi at gcc dot gnu.org
@ 2022-05-06 20:51 ` redi at gcc dot gnu.org
  2022-05-06 22:55 ` cvs-commit at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-05-06 20:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Or:

--- 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));

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

* [Bug libstdc++/105502] [9/10/11/12/13 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
  2022-05-06 14:35 ` [Bug libstdc++/105502] [9/12/11/12/13 Regression] " redi at gcc dot gnu.org
  2022-05-06 20:51 ` [Bug libstdc++/105502] [9/10/11/12/13 " redi at gcc dot gnu.org
@ 2022-05-06 22:55 ` cvs-commit at gcc dot gnu.org
  2022-05-06 22:59 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-06 22:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:909ef4e2727ddc50a32d6ad379a1f1ccc1043c6a

commit r13-163-g909ef4e2727ddc50a32d6ad379a1f1ccc1043c6a
Author: Jonathan Wakely <jwakely@redhat.com>
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<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.

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

* [Bug libstdc++/105502] [9/10/11/12/13 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
                   ` (2 preceding siblings ...)
  2022-05-06 22:55 ` cvs-commit at gcc dot gnu.org
@ 2022-05-06 22:59 ` cvs-commit at gcc dot gnu.org
  2022-05-06 22:59 ` [Bug libstdc++/105502] [9/10/11 " redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-06 22:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:03257e7ee31385a364b711898c8f1510fd638efe

commit r12-8354-g03257e7ee31385a364b711898c8f1510fd638efe
Author: Jonathan Wakely <jwakely@redhat.com>
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<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.

    (cherry picked from commit 909ef4e2727ddc50a32d6ad379a1f1ccc1043c6a)

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

* [Bug libstdc++/105502] [9/10/11 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
                   ` (3 preceding siblings ...)
  2022-05-06 22:59 ` cvs-commit at gcc dot gnu.org
@ 2022-05-06 22:59 ` redi at gcc dot gnu.org
  2022-05-09 15:09 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-05-06 22:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[9/10/11/12/13 Regression]  |[9/10/11 Regression]
                   |std::normal_distribution    |std::normal_distribution
                   |deserialization issue       |deserialization issue

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for trunk and 12.2 so far.

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

* [Bug libstdc++/105502] [9/10/11 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
                   ` (4 preceding siblings ...)
  2022-05-06 22:59 ` [Bug libstdc++/105502] [9/10/11 " redi at gcc dot gnu.org
@ 2022-05-09 15:09 ` cvs-commit at gcc dot gnu.org
  2022-05-09 16:35 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-09 15:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:eb049ef0f4651786aa49110189487e7f8bf343a6

commit r11-9970-geb049ef0f4651786aa49110189487e7f8bf343a6
Author: Jonathan Wakely <jwakely@redhat.com>
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<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.

    (cherry picked from commit 909ef4e2727ddc50a32d6ad379a1f1ccc1043c6a)

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

* [Bug libstdc++/105502] [9/10/11 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
                   ` (5 preceding siblings ...)
  2022-05-09 15:09 ` cvs-commit at gcc dot gnu.org
@ 2022-05-09 16:35 ` cvs-commit at gcc dot gnu.org
  2022-05-09 16:44 ` cvs-commit at gcc dot gnu.org
  2022-05-09 16:44 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-09 16:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:f3a0d3a778643bcb82bdf41a67a5ddc1f95c6f53

commit r10-10600-gf3a0d3a778643bcb82bdf41a67a5ddc1f95c6f53
Author: Jonathan Wakely <jwakely@redhat.com>
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<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.

    (cherry picked from commit 909ef4e2727ddc50a32d6ad379a1f1ccc1043c6a)

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

* [Bug libstdc++/105502] [9/10/11 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
                   ` (6 preceding siblings ...)
  2022-05-09 16:35 ` cvs-commit at gcc dot gnu.org
@ 2022-05-09 16:44 ` cvs-commit at gcc dot gnu.org
  2022-05-09 16:44 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-05-09 16:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:b005000525ab0a5116d21217c41fb1da5bd03796

commit r9-10066-gb005000525ab0a5116d21217c41fb1da5bd03796
Author: Jonathan Wakely <jwakely@redhat.com>
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<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.

    (cherry picked from commit 909ef4e2727ddc50a32d6ad379a1f1ccc1043c6a)

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

* [Bug libstdc++/105502] [9/10/11 Regression] std::normal_distribution deserialization issue
  2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
                   ` (7 preceding siblings ...)
  2022-05-09 16:44 ` cvs-commit at gcc dot gnu.org
@ 2022-05-09 16:44 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-05-09 16:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105502

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 9.5, 10.4, 11.4 and 12.2

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

end of thread, other threads:[~2022-05-09 16:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-06  8:28 [Bug libstdc++/105502] New: std::normal_distribution deserialization issue frank.marschner at cognitec dot com
2022-05-06 14:35 ` [Bug libstdc++/105502] [9/12/11/12/13 Regression] " redi at gcc dot gnu.org
2022-05-06 20:51 ` [Bug libstdc++/105502] [9/10/11/12/13 " redi at gcc dot gnu.org
2022-05-06 22:55 ` cvs-commit at gcc dot gnu.org
2022-05-06 22:59 ` cvs-commit at gcc dot gnu.org
2022-05-06 22:59 ` [Bug libstdc++/105502] [9/10/11 " redi at gcc dot gnu.org
2022-05-09 15:09 ` cvs-commit at gcc dot gnu.org
2022-05-09 16:35 ` cvs-commit at gcc dot gnu.org
2022-05-09 16:44 ` cvs-commit at gcc dot gnu.org
2022-05-09 16:44 ` redi at gcc dot gnu.org

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).