http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60037
Bug ID: 60037
Summary: SIGFPE in std::generate_canonical<unsigned int...>
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: ppluzhnikov at google dot com
When
libstdc++-v3/testsuite/ext/random/hypergeometric_distribution/operators/values.cc
is built with -O0, I get a crash:
Program received signal SIGFPE, Arithmetic exception.
0x0000000000401b07 in std::generate_canonical<unsigned int, 32ul,
std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul,
2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul,
1812433253ul> > (__urng=...)
at
/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/random.tcc:3480
3480 return __sum / __tmp;
(gdb) bt
#0 0x0000000000401b07 in std::generate_canonical<unsigned int, 32ul,
std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul,
2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul,
1812433253ul> > (__urng=...)
at
/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/random.tcc:3480
#1 0x00000000004018d9 in
std::__detail::_Adaptor<std::mersenne_twister_engine<unsigned long, 32ul,
624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul,
4022730752ul, 18ul, 1812433253ul>, unsigned int>::operator()
(this=0x7fffffff9020)
at /build/x86_64-unknown-linux-gnu/libstdc++-v3/include/bits/random.h:190
#2 0x0000000000401785 in __gnu_cxx::hypergeometric_distribution<unsigned
int>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul,
397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul,
4022730752ul, 18ul, 1812433253ul> > (
this=0x7fffffffab40, __urng=..., __param=...) at
/gcc-svn/libstdc++-v3/include/ext/random.tcc:1328
#3 0x00000000004016fb in __gnu_cxx::hypergeometric_distribution<unsigned
int>::operator()<std::mersenne_twister_engine<unsigned long, 32ul, 624ul,
397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul,
4022730752ul, 18ul, 1812433253ul> > (
this=0x7fffffffab40, __urng=...) at
/gcc-svn/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/ext/random:3016
#4 0x0000000000401642 in
std::_Bind<__gnu_cxx::hypergeometric_distribution<unsigned int>
(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul,
2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul,
1812433253ul>)>::__call<unsigned int, , 0ul>(std::tuple<>&&,
std::_Index_tuple<0ul>) (this=0x7fffffffab40, __args=<unknown type in
/gcc-svn/build/x86_64-unknown-linux-gnu/libstdc++-v3/testsuite/./values.exe, CU
0x0, DIE 0x445d>)
at
/gcc-svn/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/functional:1264
#5 0x0000000000401578 in
std::_Bind<__gnu_cxx::hypergeometric_distribution<unsigned int>
(std::mersenne_twister_engine<unsigned long, 32ul, 624ul, 397ul, 31ul,
2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul,
1812433253ul>)>::operator()<, unsigned int>() (this=0x7fffffffab40) at
/build/x86_64-unknown-linux-gnu/libstdc++-v3/include/functional:1323
#6 0x0000000000400d4c in __gnu_test::testDiscreteDist<100ul, 100000ul,
std::_Bind<__gnu_cxx::hypergeometric_distribution<>(std::mersenne_twister_engine<long
unsigned int, 32ul, 624ul, 397ul, 31ul, 2567483615ul, 11ul, 4294967295ul, 7ul,
2636928640ul, 15ul, 4022730752ul, 18ul, 1812433253ul>)>,
test01()::<lambda(int)>
>(std::_Bind<__gnu_cxx::hypergeometric_distribution<unsigned
int>(std::mersenne_twister_engine<long unsigned int, 32ul, 624ul, 397ul, 31ul,
2567483615ul, 11ul, 4294967295ul, 7ul, 2636928640ul, 15ul, 4022730752ul, 18ul,
1812433253ul>)> &, <lambda(int)>) (f=..., pdf=...) at
/gcc-svn/libstdc++-v3/testsuite/util/testsuite_random.h:50
#7 0x0000000000400aa9 in test01 () at
/gcc-svn/libstdc++-v3/testsuite/ext/random/hypergeometric_distribution/operators/values.cc:42
#8 0x0000000000400b72 in main () at
/gcc-svn/libstdc++-v3/testsuite/ext/random/hypergeometric_distribution/operators/values.cc:58
AFAICT, this is happening because:
3461 template<typename _RealType, size_t __bits,
3462 typename _UniformRandomNumberGenerator>
3463 _RealType
3464 generate_canonical(_UniformRandomNumberGenerator& __urng)
3465 {
3466 const size_t __b
3467 =
std::min(static_cast<size_t>(std::numeric_limits<_RealType>::digits),
3468 __bits);
3469 const long double __r = static_cast<long double>(__urng.max())
3470 - static_cast<long double>(__urng.min()) + 1.0L;
3471 const size_t __log2r = std::log(__r) / std::log(2.0L);
3472 size_t __k = std::max<size_t>(1UL, (__b + __log2r - 1UL) /
__log2r);
3473 _RealType __sum = _RealType(0);
3474 _RealType __tmp = _RealType(1);
3475 for (; __k != 0; --__k)
3476 {
3477 __sum += _RealType(__urng() - __urng.min()) * __tmp;
3478 __tmp *= __r;
3479 }
3480 return __sum / __tmp;
3481 }
3482
3483 _GLIBCXX_END_NAMESPACE_VERSION
3484 } // namespace
at line 3475, we have __r=4294967296, __tmp=1, __k=1.
On line 3478, __tmp (which is unsigned int) overflows and becomes 0,
and on line 3480 we divide by zero and get SIGFPE.