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.