Skip to content

Commit 8d98d42

Browse files
committed
Bugfix: randbytes should seed when needed (non reachable issue)
1 parent 273d025 commit 8d98d42

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/random.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ uint256 FastRandomContext::rand256()
398398

399399
std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
400400
{
401+
if (requires_seed) RandomSeed();
401402
std::vector<unsigned char> ret(len);
402403
if (len > 0) {
403404
rng.Output(&ret[0], len);

src/test/random_tests.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
3838
BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));
3939

4040
// Check that a nondeterministic ones are not
41-
FastRandomContext ctx3;
42-
FastRandomContext ctx4;
43-
BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal
44-
BOOST_CHECK(ctx3.rand256() != ctx4.rand256());
45-
BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7));
41+
{
42+
FastRandomContext ctx3, ctx4;
43+
BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal
44+
}
45+
{
46+
FastRandomContext ctx3, ctx4;
47+
BOOST_CHECK(ctx3.rand256() != ctx4.rand256());
48+
}
49+
{
50+
FastRandomContext ctx3, ctx4;
51+
BOOST_CHECK(ctx3.randbytes(7) != ctx4.randbytes(7));
52+
}
4653
}
4754

4855
BOOST_AUTO_TEST_CASE(fastrandom_randbits)

0 commit comments

Comments
 (0)