Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
using System.Management.Automation.Runspaces;
using System.Reflection;
using System.Runtime.InteropServices;
#if !UNIX
using System.Runtime.InteropServices.Marshalling;
#endif
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -5181,14 +5184,6 @@ private static string NewPathCompletionText(string parent, string leaf, StringCo
@"(^Microsoft\.PowerShell\.Core\\FileSystem::|^FileSystem::|^)(?:\\\\|//)(?![.|?])([^\\/]+)(?:\\|/)([^\\/]*)$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct SHARE_INFO_1
{
public string netname;
public int type;
public string remark;
}

private static readonly System.IO.EnumerationOptions _enumerationOptions = new System.IO.EnumerationOptions
{
MatchCasing = MatchCasing.CaseInsensitive,
Expand All @@ -5200,50 +5195,46 @@ internal static List<string> GetFileShares(string machine, bool ignoreHidden)
#if UNIX
return new List<string>();
#else
nint shBuf = nint.Zero;
uint numEntries = 0;
uint totalEntries;
uint resumeHandle = 0;
try
unsafe
{
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)
Interop.Windows.SHARE_INFO_1* pShareInfo = null;
try
{
for (int i = 0; i < numEntries; ++i)
{
nint curInfoPtr = shBuf + (Marshal.SizeOf<SHARE_INFO_1>() * i);
SHARE_INFO_1 shareInfo = Marshal.PtrToStructure<SHARE_INFO_1>(curInfoPtr);
uint result = Interop.Windows.NetShareEnum<Interop.Windows.SHARE_INFO_1>(
machine,
out pShareInfo,
out int count);

if ((shareInfo.type & Interop.Windows.STYPE_MASK) != Interop.Windows.STYPE_DISKTREE)
{
continue;
}
List<string> shares = new();

if (ignoreHidden && shareInfo.netname.EndsWith('$'))
if (result is Interop.Windows.ERROR_SUCCESS or Interop.Windows.ERROR_MORE_DATA)
{
foreach (Interop.Windows.SHARE_INFO_1 shareInfo in new ReadOnlySpan<Interop.Windows.SHARE_INFO_1>(pShareInfo, count))
{
continue;
}
if ((shareInfo.type & Interop.Windows.STYPE_MASK) != Interop.Windows.STYPE_DISKTREE)
{
continue;
}

string share = Utf16StringMarshaller.ConvertToManaged(shareInfo.netname);
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

Potential null reference exception: Utf16StringMarshaller.ConvertToManaged may return null if shareInfo.netname is null, but the result is used without null checking on line 5221. Add null handling, e.g., string? share = Utf16StringMarshaller.ConvertToManaged(shareInfo.netname); if (share == null) continue;

Suggested change
string share = Utf16StringMarshaller.ConvertToManaged(shareInfo.netname);
string? share = Utf16StringMarshaller.ConvertToManaged(shareInfo.netname);
if (share == null)
{
continue;
}

Copilot uses AI. Check for mistakes.

if (ignoreHidden && share.EndsWith('$'))
{
continue;
}

shares.Add(shareInfo.netname);
shares.Add(share);
}
}
}

return shares;
}
finally
{
if (shBuf != nint.Zero)
return shares;
}
finally
{
Interop.Windows.NetApiBufferFree(shBuf);
if (pShareInfo is not null)
{
Interop.Windows.NetApiBufferFree(pShareInfo);
}
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ internal static unsafe partial class Windows
{

[LibraryImport("Netapi32.dll")]
internal static partial uint NetApiBufferFree(nint Buffer);
internal static unsafe partial uint NetApiBufferFree(void* Buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,50 @@

#nullable enable

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static unsafe partial class Windows
{
internal const int MAX_PREFERRED_LENGTH = -1;
internal const int STYPE_DISKTREE = 0;
internal const int STYPE_MASK = 0x000000FF;
internal const uint MAX_PREFERRED_LENGTH = uint.MaxValue;
internal const uint STYPE_DISKTREE = 0u;
internal const uint STYPE_MASK = 255u;

[LibraryImport("Netapi32.dll", StringMarshalling = StringMarshalling.Utf16)]
internal static partial int NetShareEnum(
string serverName,
int level,
out nint bufptr,
int prefMaxLen,
out uint entriesRead,
out uint totalEntries,
ref uint resumeHandle);
private static unsafe partial uint NetShareEnum(
string? servername,
uint level,
out byte* bufptr,
uint prefmaxlen,
out uint entriesread,
out uint totalentries,
uint* resume_handle);

[StructLayout(LayoutKind.Sequential)]
internal unsafe struct SHARE_INFO_1
{
public ushort* netname;
public uint type;
public ushort* remark;
}

internal static uint NetShareEnum<T>(string? servername, out T* pShareInfo, out int count) where T : unmanaged
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 high level helper it could return just List so that we have no interop/unsafe code in call site.

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 need to free the unmanaged buffer, so we cannot return just a managed type.

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.

I considered implementing a class deriving from SafeBuffer, but this would be much more code and abstraction for a method that is only called once.

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.

If we build List in the method we can free the buffer in the method too.

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.

If we build List in the method we can free the buffer in the method too.

This is exactly what the GetFileShares method does already :p

Copy link
Copy Markdown
Contributor Author

@xtqqczze xtqqczze Oct 17, 2025

Choose a reason for hiding this comment

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

@iSazonov I think the current changes are a reasonable balance between safety and avoiding unnecessary abstraction. In terms of safety, we now avoid pointer arithmetic and use a span implementation.

{
uint level = (uint)GetLevelFromStructure<T>();
uint result = NetShareEnum(servername, level, out byte* pBuffer, MAX_PREFERRED_LENGTH, out uint entriesRead, out _, null);
pShareInfo = (T*)pBuffer;
count = (int)entriesRead;
return result;
}

private static int GetLevelFromStructure<T>()
{
if (typeof(T) == typeof(SHARE_INFO_1))
return 1;

throw new NotSupportedException();
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

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

The NotSupportedException lacks a descriptive message. Consider adding a message that specifies which type was unsupported, e.g., throw new NotSupportedException($\"Unsupported structure type: {typeof(T).Name}\");

Suggested change
throw new NotSupportedException();
throw new NotSupportedException($"Unsupported structure type: {typeof(T).Name}");

Copilot uses AI. Check for mistakes.
}
}
}