From b5e53868de8df98e337312dff782899181e4b225 Mon Sep 17 00:00:00 2001 From: Bhagirath Mehta Date: Sun, 23 Aug 2026 21:59:27 -0500 Subject: [PATCH] Fix unsafe resource lifetimes found by audit Correct four independent ownership defects that can over-release borrowed CoreFoundation data, leak a newly created manager on registry allocation failure, corrupt self-assigned EventProperty values, or pair array allocations with scalar deletion. Files changed: - examples/cpp/MacProxy/main.cpp: preserve borrowed proxy values and guard empty proxy arrays. - lib/api/LogManagerFactory.cpp: retain manager ownership until registry insertion succeeds. - lib/system/EventProperty.cpp: make copy self-assignment safe. - lib/tracing/api/DebugProviders.hpp: pair new[] buffers with delete[]. - tests/unittests/EventPropertiesTests.cpp: cover EventProperty self-assignment. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d7d2f27a-7339-4585-ad02-9f89ce20ef40 --- examples/cpp/MacProxy/main.cpp | 7 ++----- lib/api/LogManagerFactory.cpp | 8 ++++---- lib/system/EventProperty.cpp | 6 +++++- lib/tracing/api/DebugProviders.hpp | 5 ++--- tests/unittests/EventPropertiesTests.cpp | 9 +++++++++ 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/examples/cpp/MacProxy/main.cpp b/examples/cpp/MacProxy/main.cpp index 84bb2194a..90596baf5 100644 --- a/examples/cpp/MacProxy/main.cpp +++ b/examples/cpp/MacProxy/main.cpp @@ -58,6 +58,8 @@ std::string GetProxyForURL(const std::string& url) urlProxArrayRef = CFNetworkCopyProxiesForURL(urlRef, proxyDicRef); if (!urlProxArrayRef) goto cleanup; + if (CFArrayGetCount(urlProxArrayRef) == 0) + goto cleanup; defProxyDic = (CFDictionaryRef)CFArrayGetValueAtIndex(urlProxArrayRef, 0); if (!defProxyDic) @@ -82,11 +84,6 @@ std::string GetProxyForURL(const std::string& url) cleanup: - if (hostNameRef) - { - CFRelease(hostNameRef); - hostNameRef = NULL; - } if (urlProxArrayRef) { CFRelease(urlProxArrayRef); diff --git a/lib/api/LogManagerFactory.cpp b/lib/api/LogManagerFactory.cpp index 0f6a31171..f6e68a5c3 100644 --- a/lib/api/LogManagerFactory.cpp +++ b/lib/api/LogManagerFactory.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -33,9 +34,9 @@ namespace MAT_NS_BEGIN ILogManager* LogManagerFactory::Create(ILogConfiguration& configuration) { LOCKGUARD(ILogManagerInternal::managers_lock); - auto logManager = new LogManagerImpl(configuration); - ILogManagerInternal::managers.emplace(logManager); - return logManager; + auto logManager = std::make_unique(configuration); + ILogManagerInternal::managers.emplace(logManager.get()); + return logManager.release(); } /// @@ -254,4 +255,3 @@ namespace MAT_NS_BEGIN } MAT_NS_END - diff --git a/lib/system/EventProperty.cpp b/lib/system/EventProperty.cpp index 6d0582440..cce939ee8 100644 --- a/lib/system/EventProperty.cpp +++ b/lib/system/EventProperty.cpp @@ -504,6 +504,11 @@ namespace MAT_NS_BEGIN { /// EventProperty& EventProperty::operator=(const EventProperty& source) { + if (this == &source) + { + return *this; + } + clear(); memcpy((void*)this, (void*)&source, sizeof(EventProperty)); copydata(&source); @@ -958,4 +963,3 @@ namespace MAT_NS_BEGIN { } } MAT_NS_END - diff --git a/lib/tracing/api/DebugProviders.hpp b/lib/tracing/api/DebugProviders.hpp index 90f6740af..61669a673 100644 --- a/lib/tracing/api/DebugProviders.hpp +++ b/lib/tracing/api/DebugProviders.hpp @@ -344,8 +344,8 @@ class ETWStringStream : public DebugStringStream { guid.Data4[6] = buffer2[14]; guid.Data4[7] = buffer2[15]; - delete buffer; - delete buffer2; + delete[] buffer; + delete[] buffer2; return guid; } @@ -393,4 +393,3 @@ class ETWStringStream : public DebugStringStream { #endif #endif - diff --git a/tests/unittests/EventPropertiesTests.cpp b/tests/unittests/EventPropertiesTests.cpp index d2867340b..9e8a408c0 100644 --- a/tests/unittests/EventPropertiesTests.cpp +++ b/tests/unittests/EventPropertiesTests.cpp @@ -114,6 +114,15 @@ TEST(EventPropertiesTests, Properties) EXPECT_THAT(ep.GetPiiProperties(), IsEmpty()); } +TEST(EventPropertiesTests, SelfAssignmentPreservesValue) +{ + EventProperty property("value"); + + property = property; + + EXPECT_EQ(property, EventProperty("value")); +} + TEST(EventPropertiesTests, NumericProperties) { EventProperties ep("test");