From 6105af8279b609107f5116b0ac5039b8dbcd17b8 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Thu, 30 Apr 2020 18:37:30 -0700 Subject: [PATCH] fix(ipc): allow IPC under windows by not using std::random_device with mingw https://en.cppreference.com/w/cpp/numeric/random/random_device "A notable implementation where std::random_device is deterministic is old versions of MinGW (bug 338, fixed since GCC 9.2)." Although time is less random off Windows, we don't need a strongly random number for this, and using time avoids having to use platform specific code. Fix #2917 --- src/ipc.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ipc.cpp b/src/ipc.cpp index 2467b791b..b1ec605e7 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -85,9 +86,11 @@ IPC::IPC(uint32_t profileId) // If the owner exits normally, it can set the timestamp to 0 first to immediately give // ownership - std::default_random_engine randEngine((std::random_device())()); + // use the clock rather than std::random_device because std::random_device may return constant values, and does + // under mingw on Windows. We don't actually need cryptographic guarantees, so using the clock in all cases. + static std::mt19937 rng(std::chrono::high_resolution_clock::now().time_since_epoch().count()); std::uniform_int_distribution distribution; - globalId = distribution(randEngine); + globalId = distribution(rng); qDebug() << "Our global IPC ID is " << globalId; if (globalMemory.create(sizeof(IPCMemory))) { if (globalMemory.lock()) {