Skip to content

Commit cdffe88

Browse files
committed
Fix memory leak in TlsRandomNumberGenerator() constructor #2660
1 parent da8e377 commit cdffe88

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

sdk/src/common/random.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "src/common/random.h"
66
#include "src/common/platform/fork.h"
77

8+
#include <atomic>
89
#include <cstring>
910
#include <random>
1011

@@ -26,12 +27,18 @@ class TlsRandomNumberGenerator
2627
TlsRandomNumberGenerator() noexcept
2728
{
2829
Seed();
29-
platform::AtFork(nullptr, nullptr, OnFork);
30+
if (flag)
31+
{
32+
platform::AtFork(nullptr, nullptr, OnFork);
33+
flag = false;
34+
}
3035
}
3136

3237
static FastRandomNumberGenerator &engine() noexcept { return engine_; }
3338

3439
private:
40+
static std::atomic<bool> flag;
41+
3542
static thread_local FastRandomNumberGenerator engine_;
3643

3744
static void OnFork() noexcept { Seed(); }
@@ -44,6 +51,7 @@ class TlsRandomNumberGenerator
4451
}
4552
};
4653

54+
std::atomic<bool> TlsRandomNumberGenerator::flag{true};
4755
thread_local FastRandomNumberGenerator TlsRandomNumberGenerator::engine_{};
4856
} // namespace
4957

0 commit comments

Comments
 (0)