Skip to content

Commit e414486

Browse files
committed
Do not permit copying FastRandomContexts
1 parent 022cf47 commit e414486

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
@@ -464,6 +464,20 @@ FastRandomContext::FastRandomContext(bool fDeterministic) : requires_seed(!fDete
464464
rng.SetKey(seed.begin(), 32);
465465
}
466466

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

src/random.h

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

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

0 commit comments

Comments
 (0)