Skip to content

Commit 27663b8

Browse files
sipaWarrows
authored andcommitted
Do not permit copying FastRandomContexts
1 parent 64e03e6 commit 27663b8

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/random.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,20 @@ FastRandomContext::FastRandomContext(bool fDeterministic) : requires_seed(!fDete
462462
rng.SetKey(seed.begin(), 32);
463463
}
464464

465+
FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexcept
466+
{
467+
requires_seed = from.requires_seed;
468+
rng = from.rng;
469+
std::copy(std::begin(from.bytebuf), std::end(from.bytebuf), std::begin(bytebuf));
470+
bytebuf_size = from.bytebuf_size;
471+
bitbuf = from.bitbuf;
472+
bitbuf_size = from.bitbuf_size;
473+
from.requires_seed = true;
474+
from.bytebuf_size = 0;
475+
from.bitbuf_size = 0;
476+
return *this;
477+
}
478+
465479
void RandomInit()
466480
{
467481
RDRandInit();

src/random.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ class FastRandomContext {
7575
/** Initialize with explicit seed (only for testing) */
7676
explicit FastRandomContext(const uint256& seed);
7777

78+
// Do not permit copying a FastRandomContext (move it, or create a new one to get reseeded).
79+
FastRandomContext(const FastRandomContext&) = delete;
80+
FastRandomContext(FastRandomContext&&) = delete;
81+
FastRandomContext& operator=(const FastRandomContext&) = delete;
82+
83+
/** Move a FastRandomContext. If the original one is used again, it will be reseeded. */
84+
FastRandomContext& operator=(FastRandomContext&& from) noexcept;
85+
7886
/** Generate a random 64-bit integer. */
7987
uint64_t rand64()
8088
{

0 commit comments

Comments
 (0)