]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Fix incorrect results in std::seed_seq::generate [PR 97311]
authorJonathan Wakely <jwakely@redhat.com>
Fri, 9 Oct 2020 15:10:31 +0000 (16:10 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 9 Oct 2020 15:58:32 +0000 (16:58 +0100)
commit3ee44d4c518d61c6bbf75fcf280edc6ce5326ce0
tree97860d67e7b00d0788d69ea08c4212eaad62569e
parentbb37ad8cc0fc937c7afcdab471e5d65d176041c3
libstdc++: Fix incorrect results in std::seed_seq::generate [PR 97311]

This ensures that intermediate results are done in uint32_t values,
meeting the requirement for operations to be done modulo 2^32.

If the target doesn't define __UINT32_TYPE__ then substitute uint32_t
with a class type that uses uint_least32_t and masks the value to
UINT32_MAX.

I've also split the first loop that goes from k=0 to k<m into three
loops, for k=0, [1,s] and [s+1,m). This avoids branching for those three
cases in the body of the loop, and also avoids the concerns in PR 94823
regarding the k-1 index when k==0.

libstdc++-v3/ChangeLog:

PR libstdc++/97311
* include/bits/random.tcc (seed_seq::generate): Use uint32_t for
calculations. Also split the first loop into three loops to
avoid branching on k on every iteration, resolving PR 94823.
* testsuite/26_numerics/random/seed_seq/97311.cc: New test.
* testsuite/26_numerics/random/pr60037-neg.cc: Adjust dg-erro
line number.
libstdc++-v3/include/bits/random.tcc
libstdc++-v3/testsuite/26_numerics/random/pr60037-neg.cc
libstdc++-v3/testsuite/26_numerics/random/seed_seq/97311.cc [new file with mode: 0644]