Skip to content

Fix unsafe resource lifetimes found by repository audit - #1524

Merged
bmehta001 merged 3 commits into
microsoft:mainfrom
bmehta001:bhamehta/resource-lifetime-audit
Aug 28, 2026
Merged

Fix unsafe resource lifetimes found by repository audit#1524
bmehta001 merged 3 commits into
microsoft:mainfrom
bmehta001:bhamehta/resource-lifetime-audit

Conversation

@bmehta001

Copy link
Copy Markdown
Contributor

Description

Fix four independent resource-lifetime defects found by a repository-wide audit of first-party cleanup and ownership paths:

  • Do not release hostNameRef returned by CFDictionaryGetValue; it is borrowed from the proxy dictionary. Also avoid indexing an empty proxy array.
  • Keep a newly constructed LogManagerImpl in a unique_ptr until insertion into the global manager registry succeeds, preventing an exception-path leak.
  • Make EventProperty copy self-assignment a no-op instead of clearing its own source storage and then dereferencing it.
  • Pair the two new[] allocations in ETW provider GUID generation with delete[] rather than scalar delete.

The audit excluded vendored/generated code. WinInet and curl cancellation lifetime findings are intentionally not duplicated here because #1520 already rewrites those ownership paths.

Validation

  • Configured and built the Linux SDK/unit-test targets with MATSDK_WARNINGS_AS_ERRORS=ON.
  • Ran the targeted EventPropertiesTests suite.
  • Ran all 645 Linux unit tests.
  • Performed a focused ownership review of the resulting diff; an initially considered Objective-C configuration cleanup was removed because custom log managers retain that configuration by reference and require a broader lifecycle redesign.

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
@bmehta001
bmehta001 requested a review from a team as a code owner August 24, 2026 03:00
@bmehta001 bmehta001 self-assigned this Aug 24, 2026
@bmehta001
bmehta001 enabled auto-merge (squash) August 28, 2026 21:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request addresses several resource-lifetime and ownership defects identified by an audit, tightening correctness around CoreFoundation borrow semantics, exception-safety during log manager creation, safe self-assignment for EventProperty, and correct array deallocation in ETW GUID generation.

Changes:

  • Add a self-assignment guard to EventProperty::operator= and add a unit test to prevent regressions.
  • Make LogManagerFactory::Create exception-safe by using std::unique_ptr until registry insertion succeeds.
  • Fix mismatched new[]/delete in ETW provider GUID generation and correct CoreFoundation lifetime handling in the Mac proxy example.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/unittests/EventPropertiesTests.cpp Adds a regression test covering EventProperty self-assignment behavior.
lib/tracing/api/DebugProviders.hpp Fixes incorrect deallocation by pairing new[] with delete[].
lib/system/EventProperty.cpp Makes copy self-assignment a no-op to avoid clearing and then reading from the same object.
lib/api/LogManagerFactory.cpp Uses std::unique_ptr to avoid leaks if insertion into the manager registry throws.
examples/cpp/MacProxy/main.cpp Avoids indexing an empty proxy array and stops releasing a borrowed CFDictionaryGetValue result.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@bmehta001
bmehta001 merged commit f1642eb into microsoft:main Aug 28, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants