From cf08ca7e383607a3b6f37253c2011ec352c9274a Mon Sep 17 00:00:00 2001 From: Antoine Beauchamp Date: Sun, 18 Aug 2024 20:32:25 -0400 Subject: [PATCH] Fixed an infinite loop initialization because of a circular reference with PropertyManager. --- src/core/PropertyManager.cpp | 17 +++++++++++++---- src/core/PropertyManager.h | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/core/PropertyManager.cpp b/src/core/PropertyManager.cpp index f73036b..cac531f 100644 --- a/src/core/PropertyManager.cpp +++ b/src/core/PropertyManager.cpp @@ -56,11 +56,9 @@ namespace shellanything const std::string PropertyManager::SYSTEM_RANDOM_PATH_PROPERTY_NAME = "random.path"; const std::string PropertyManager::SYSTEM_LOGGING_VERBOSE_PROPERTY_NAME = "system.logging.verbose"; - PropertyManager::PropertyManager() + PropertyManager::PropertyManager() : + mInitialized(false) { - RegisterEnvironmentVariables(); - RegisterFixedAndDefaultProperties(); - RegisterLiveProperties(); } PropertyManager::~PropertyManager() @@ -71,6 +69,17 @@ namespace shellanything PropertyManager& PropertyManager::GetInstance() { static PropertyManager _instance; + if (!_instance.mInitialized) + { + _instance.mInitialized = true; + + // Initialize PropertyManager with default properties. + // Note: The next calls will likely lead to another call to PropertyManager::GetInstance(). + // We are using the PropertyManager::mInitialized flag to prevent running into a circular reference + _instance.RegisterEnvironmentVariables(); + _instance.RegisterFixedAndDefaultProperties(); + _instance.RegisterLiveProperties(); + } return _instance; } diff --git a/src/core/PropertyManager.h b/src/core/PropertyManager.h index 94470d3..ab7c2c6 100644 --- a/src/core/PropertyManager.h +++ b/src/core/PropertyManager.h @@ -261,6 +261,7 @@ namespace shellanything void RegisterEnvironmentVariables(); void RegisterFixedAndDefaultProperties(); + bool mInitialized; // to prevent calling PropertyManager::GetInstance() while in PropertyManager ctor, creating a circular reference. PropertyStore properties; LivePropertyMap live_properties; };