From 42eb4fdda607a3298fc8bf5e8667a8b03fc34aec Mon Sep 17 00:00:00 2001 From: bgbsww Date: Tue, 4 Jun 2024 10:02:13 -0400 Subject: [PATCH] Guard all uses of basic_random_generator for thread safety --- src/Mod/Part/App/Geometry.cpp | 7 +++++++ src/Mod/Sketcher/App/Constraint.cpp | 7 +++++++ src/Mod/TechDraw/App/CenterLine.cpp | 7 +++++++ src/Mod/TechDraw/App/Cosmetic.cpp | 12 ++++++++++++ src/Mod/TechDraw/App/CosmeticVertex.cpp | 7 +++++++ src/Mod/TechDraw/App/Geometry.cpp | 6 ++++++ 6 files changed, 46 insertions(+) diff --git a/src/Mod/Part/App/Geometry.cpp b/src/Mod/Part/App/Geometry.cpp index 20519fe1ee..9f52a3fd60 100644 --- a/src/Mod/Part/App/Geometry.cpp +++ b/src/Mod/Part/App/Geometry.cpp @@ -113,6 +113,8 @@ #include #include #include +#include +#include #include "Geometry.h" #include "ArcOfCirclePy.h" @@ -455,8 +457,13 @@ void Geometry::deleteExtension(const std::string & name) void Geometry::createNewTag() { // Initialize a random number generator, to avoid Valgrind false positives. + // The random number generator is not threadsafe so we guard it. See + // https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes static boost::mt19937 ran; static bool seeded = false; + static boost::mutex random_number_mutex; + + boost::lock_guard guard(random_number_mutex); if (!seeded) { ran.seed(static_cast(std::time(nullptr))); diff --git a/src/Mod/Sketcher/App/Constraint.cpp b/src/Mod/Sketcher/App/Constraint.cpp index bf4e8e8c23..1ee3e53753 100644 --- a/src/Mod/Sketcher/App/Constraint.cpp +++ b/src/Mod/Sketcher/App/Constraint.cpp @@ -30,6 +30,8 @@ #include #include +#include +#include #include "Constraint.h" #include "ConstraintPy.h" @@ -58,8 +60,13 @@ Constraint::Constraint() , isActive(true) { // Initialize a random number generator, to avoid Valgrind false positives. + // The random number generator is not threadsafe so we guard it. See + // https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes static boost::mt19937 ran; static bool seeded = false; + static boost::mutex random_number_mutex; + + boost::lock_guard guard(random_number_mutex); if (!seeded) { ran.seed(QDateTime::currentMSecsSinceEpoch() & 0xffffffff); diff --git a/src/Mod/TechDraw/App/CenterLine.cpp b/src/Mod/TechDraw/App/CenterLine.cpp index 7c32a01389..eac348ce0b 100644 --- a/src/Mod/TechDraw/App/CenterLine.cpp +++ b/src/Mod/TechDraw/App/CenterLine.cpp @@ -34,6 +34,8 @@ #include #include +#include +#include #include "CenterLine.h" #include "DrawUtil.h" @@ -1072,8 +1074,13 @@ std::string CenterLine::getTagAsString() const void CenterLine::createNewTag() { // Initialize a random number generator, to avoid Valgrind false positives. + // The random number generator is not threadsafe so we guard it. See + // https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes static boost::mt19937 ran; static bool seeded = false; + static boost::mutex random_number_mutex; + + boost::lock_guard guard(random_number_mutex); if (!seeded) { ran.seed(static_cast(std::time(nullptr))); diff --git a/src/Mod/TechDraw/App/Cosmetic.cpp b/src/Mod/TechDraw/App/Cosmetic.cpp index 7bce844792..8004c01f28 100644 --- a/src/Mod/TechDraw/App/Cosmetic.cpp +++ b/src/Mod/TechDraw/App/Cosmetic.cpp @@ -27,6 +27,8 @@ # include # include #endif +#include +#include #include #include @@ -312,8 +314,13 @@ std::string CosmeticEdge::getTagAsString() const void CosmeticEdge::createNewTag() { // Initialize a random number generator, to avoid Valgrind false positives. + // The random number generator is not threadsafe so we guard it. See + // https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes static boost::mt19937 ran; static bool seeded = false; + static boost::mutex random_number_mutex; + + boost::lock_guard guard(random_number_mutex); if (!seeded) { ran.seed(static_cast(std::time(nullptr))); @@ -483,8 +490,13 @@ std::string GeomFormat::getTagAsString() const void GeomFormat::createNewTag() { // Initialize a random number generator, to avoid Valgrind false positives. + // The random number generator is not threadsafe so we guard it. See + // https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes static boost::mt19937 ran; static bool seeded = false; + static boost::mutex random_number_mutex; + + boost::lock_guard guard(random_number_mutex); if (!seeded) { ran.seed(static_cast(std::time(nullptr))); diff --git a/src/Mod/TechDraw/App/CosmeticVertex.cpp b/src/Mod/TechDraw/App/CosmeticVertex.cpp index 9998e4793b..24401e78d2 100644 --- a/src/Mod/TechDraw/App/CosmeticVertex.cpp +++ b/src/Mod/TechDraw/App/CosmeticVertex.cpp @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include "CosmeticVertex.h" #include "CosmeticVertexPy.h" @@ -235,8 +237,13 @@ std::string CosmeticVertex::getTagAsString() const void CosmeticVertex::createNewTag() { // Initialize a random number generator, to avoid Valgrind false positives. + // The random number generator is not threadsafe so we guard it. See + // https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes static boost::mt19937 ran; static bool seeded = false; + static boost::mutex random_number_mutex; + + boost::lock_guard guard(random_number_mutex); if (!seeded) { ran.seed(static_cast(std::time(nullptr))); diff --git a/src/Mod/TechDraw/App/Geometry.cpp b/src/Mod/TechDraw/App/Geometry.cpp index d7fc413e03..d8fe538f30 100644 --- a/src/Mod/TechDraw/App/Geometry.cpp +++ b/src/Mod/TechDraw/App/Geometry.cpp @@ -81,6 +81,8 @@ #include #include #include +#include +#include #include #include @@ -1393,9 +1395,13 @@ void Vertex::Restore(Base::XMLReader &reader) void Vertex::createNewTag() { // Initialize a random number generator, to avoid Valgrind false positives. + // The random number generator is not threadsafe so we guard it. See + // https://www.boost.org/doc/libs/1_62_0/libs/uuid/uuid.html#Design%20notes static boost::mt19937 ran; static bool seeded = false; + static boost::mutex random_number_mutex; + boost::lock_guard guard(random_number_mutex); if (!seeded) { ran.seed(static_cast(std::time(nullptr))); seeded = true;