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

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