Fix memory leak in GetFileShares (#25896)

This commit is contained in:
xtqqczze
2025-10-07 14:28:19 +05:00
committed by GitHub
parent b5279a25a1
commit 60d4bc39bc
2 changed files with 51 additions and 25 deletions
@@ -5204,38 +5204,48 @@ namespace System.Management.Automation
uint numEntries = 0;
uint totalEntries;
uint resumeHandle = 0;
int result = Interop.Windows.NetShareEnum(
machine,
level: 1,
out shBuf,
Interop.Windows.MAX_PREFERRED_LENGTH,
out numEntries,
out totalEntries,
ref resumeHandle);
var shares = new List<string>();
if (result == Interop.Windows.ERROR_SUCCESS || result == Interop.Windows.ERROR_MORE_DATA)
try
{
for (int i = 0; i < numEntries; ++i)
int result = Interop.Windows.NetShareEnum(
machine,
level: 1,
out shBuf,
Interop.Windows.MAX_PREFERRED_LENGTH,
out numEntries,
out totalEntries,
ref resumeHandle);
var shares = new List<string>();
if (result == Interop.Windows.ERROR_SUCCESS || result == Interop.Windows.ERROR_MORE_DATA)
{
nint curInfoPtr = shBuf + (Marshal.SizeOf<SHARE_INFO_1>() * i);
SHARE_INFO_1 shareInfo = Marshal.PtrToStructure<SHARE_INFO_1>(curInfoPtr);
if ((shareInfo.type & Interop.Windows.STYPE_MASK) != Interop.Windows.STYPE_DISKTREE)
for (int i = 0; i < numEntries; ++i)
{
continue;
}
nint curInfoPtr = shBuf + (Marshal.SizeOf<SHARE_INFO_1>() * i);
SHARE_INFO_1 shareInfo = Marshal.PtrToStructure<SHARE_INFO_1>(curInfoPtr);
if (ignoreHidden && shareInfo.netname.EndsWith('$'))
{
continue;
}
if ((shareInfo.type & Interop.Windows.STYPE_MASK) != Interop.Windows.STYPE_DISKTREE)
{
continue;
}
shares.Add(shareInfo.netname);
if (ignoreHidden && shareInfo.netname.EndsWith('$'))
{
continue;
}
shares.Add(shareInfo.netname);
}
}
return shares;
}
finally
{
if (shBuf != nint.Zero)
{
Interop.Windows.NetApiBufferFree(shBuf);
}
}
return shares;
#endif
}
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System.Runtime.InteropServices;
internal static partial class Interop
{
internal static unsafe partial class Windows
{
[LibraryImport("Netapi32.dll")]
internal static partial uint NetApiBufferFree(nint Buffer);
}
}