From 2e689901fc5dac9d512f85d6b7b051a1765e956e Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Wed, 23 Mar 2022 18:49:19 -0400 Subject: [PATCH] Ensure the kiid generator rng is actually uniquely seeded each start --- common/kiid.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/common/kiid.cpp b/common/kiid.cpp index d37fbd1c46..9b72a11977 100644 --- a/common/kiid.cpp +++ b/common/kiid.cpp @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -40,8 +41,22 @@ // boost:mt19937 is not thread-safe static std::mutex rng_mutex; -// Create only once, as seeding is *very* expensive +// We use a little helper class to ensure our static rng object is seeded +// with a random seed on startup before it's passed to the uuid generator object +class GENERATOR_INIT_HELPER +{ +public: + GENERATOR_INIT_HELPER( boost::mt19937& aRng ) + { + boost::uuids::detail::random_provider seeder; + aRng.seed( seeder ); + } +}; + +// Static rng and generators are used because the overhead of constant seeding is expensive +// We break out the rng separately from the generator because we want to control seeding in cases like unit tests static boost::mt19937 rng; +static GENERATOR_INIT_HELPER initHelper( rng ); static boost::uuids::basic_random_generator randomGenerator( rng ); // These don't have the same performance penalty, but we might as well be consistent