Skip to content

Fix memory leak in GetFileShares#25896

Merged
iSazonov merged 3 commits intoPowerShell:masterfrom
xtqqczze:NetApiBufferFree
Oct 7, 2025
Merged

Fix memory leak in GetFileShares#25896
iSazonov merged 3 commits intoPowerShell:masterfrom
xtqqczze:NetApiBufferFree

Conversation

@xtqqczze
Copy link
Copy Markdown
Contributor

@xtqqczze xtqqczze commented Aug 23, 2025

Documentation for NetShareEnum states: "This buffer is allocated by the system and must be freed using the NetApiBufferFree function."

References: NetShareEnum function

Fix #26137

@iSazonov
Copy link
Copy Markdown
Collaborator

Bug fix and refactoring must be in separated PRs. While I agree with the bug fix I am not sure we want return back to unsafe code.

@xtqqczze
Copy link
Copy Markdown
Contributor Author

Bug fix and refactoring must be in separated PRs. While I agree with the bug fix I am not sure we want return back to unsafe code.

rebased for bug fix only b0f487a

{

[LibraryImport("Netapi32.dll")]
internal static partial int NetApiBufferFree(nint Buffer);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-dfsnm/a6913914-6f16-4aa4-95eb-d2ad93aabaf5

Suggested change
internal static partial int NetApiBufferFree(nint Buffer);
internal static partial uint NetApiBufferFree(nint Buffer);

Copy link
Copy Markdown
Contributor Author

@xtqqczze xtqqczze Aug 30, 2025

Choose a reason for hiding this comment

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

Yes, this is correct, but I kept int for consistency with NetShareEnum, rather than refactoring in this PR.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since it is new definition we should make it correct.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The correct definition according to C#/Win32 P/Invoke Source Generator is:

internal static extern unsafe uint NetApiBufferFree([Optional] void* Buffer);

So the Buffer parameter should also be typed as an unmanaged pointer, not an integer.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We have no need to use unsafe context.
In original definition LPVOID is used. It is IntPtr in C# or nint.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We have no need to use unsafe context. In original definition LPVOID is used. It is IntPtr in C# or nint.

I suggest using an unmanaged pointer (void*) rather than nint.

LPVOID means "any pointer", not a handle. Mapping it to void* better reflects the native contract, whereas nint is more appropriate for handles or opaque values. This keeps the API surface closer to Win32 and clearer for maintainers.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

(LPVOID is a native pointer. In C# it can be IntPtr or nint. )

The main thing here is that we should follow the style used earlier. It comes from what the standard source generator usually does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed c514be1

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Sep 9, 2025
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 3, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 3, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@iSazonov iSazonov enabled auto-merge (squash) October 3, 2025 04:22
@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Review - Needed The PR is being reviewed label Oct 3, 2025
@iSazonov iSazonov disabled auto-merge October 3, 2025 05:20
Comment thread src/System.Management.Automation/engine/Interop/Windows/NetApiBufferFree.cs Outdated
…BufferFree.cs

Co-authored-by: Ilya <darpa@yandex.ru>
@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 3, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@iSazonov iSazonov self-assigned this Oct 7, 2025
@iSazonov iSazonov merged commit 60d4bc3 into PowerShell:master Oct 7, 2025
43 of 45 checks passed
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

microsoft-github-policy-service Bot commented Oct 7, 2025

📣 Hey @@xtqqczze, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@xtqqczze xtqqczze deleted the NetApiBufferFree branch October 7, 2025 14:40
@xtqqczze xtqqczze mentioned this pull request Oct 7, 2025
@xtqqczze
Copy link
Copy Markdown
Contributor Author

xtqqczze commented Oct 7, 2025

Bug fix and refactoring must be in separated PRs

Refactoring is in #26155.

SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak in GetFileShares

2 participants