Files
PowerShell/src
Patrick Meinecke fb25469579 Merged PR 41444: [release/v7.6.6] Fix potential pointer allocation issue in PdhHelper
<!-- Anything that looks like this is a comment and can't be seen after the Pull Request is created. -->

# PR Summary
This PR hardens the unmanaged-memory handling in `PdhHelper.LookupPerfNameByIndex()` to prevent a potential double-free scenario during buffer reallocation after `PDH_MORE_DATA` condition is met.

Changes:

* After freeing `localizedPathPtr` on `PDH_MORE_DATA`, explicitly resets it to `IntPtr.Zero` before attempting a second allocation.
<!-- Summarize your PR between here and the checklist. -->

## PR Context

<!-- Provide a little reasoning as to why this Pull Request helps and why you have opened it. -->

## PR Checklist

- [ ] [PR has a meaningful title](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
  - Use the present tense and imperative mood when describing your changes
- [ ] [Summarized changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [ ] [Make sure all `.h`, `.cpp`, `.cs`, `.ps1` and `.psm1` files have the correct copyright header](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
- [ ] This PR is ready to merge. If this PR is a work in progress, please open this as a [Draft Pull Request and mark it as Ready to Review when it is ready to merge](https://docs.github.com/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests).
- **[Breaking changes](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#making-breaking-changes)**
  - [ ] None
  - **OR**
  - [ ] [Experimental feature(s) needed](https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/7.5/Microsoft.PowerShell.Core/About/about_Experimental_Features.md)
    - [ ] Experimental feature name(s): <!-- Experimental feature name(s) here -->
- **User-facing changes**
  - [ ] Not Applicable
  - **OR**
  - [ ] [Documentation needed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#pull-request---submission)
    - [ ] Issue filed: <!-- Number/link of that issue here -->
- **Testing - New and feature**
  - [ ] N/A or can only be tested interactively
  - **OR**
  - [ ] [Make sure you've added a new test if existing tests do not effectively test the code changed](https://github.com/PowerShell/PowerShell/blob/master/.github/CONTRIBUTING.md#before-submitting)

----
#### AI description  (iteration 1)
#### PR Classification
Bug fix to prevent potential double-free memory corruption in pointer allocation.

#### PR Summary
This PR fixes a memory safety issue in the PdhHelper class where a stale pointer could be double-freed if memory reallocation fails. The fix ensures the pointer is nullified after being freed but before attempting reallocation.

- `PdhHelper.cs`: Added `localizedPathPtr = IntPtr.Zero` assignment after `Marshal.FreeHGlobal` to prevent double-free ...
2026-09-01 20:56:35 +00:00
..
2020-04-14 14:34:53 -07:00