Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Interop/Windows/WNetGetConnection.cs (C#)

Code before:
using System;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Management.Automation.Internal;

internal static partial class Interop
{
    internal static unsafe partial class Windows
    {
        private static bool s_WNetApiNotAvailable;

        [LibraryImport("mpr.dll", EntryPoint = "WNetGetConnectionW", StringMarshalling = StringMarshalling.Utf16)]
        internal static partial int WNetGetConnection(ReadOnlySpan<char> localName, Span<char> remoteName, ref int remoteNameLength);

        internal static int GetUNCForNetworkDrive(char drive, out string? uncPath)
        {
            uncPath = null;
            if (s_WNetApiNotAvailable)
            {
                return ERROR_NOT_SUPPORTED;
            }

            ReadOnlySpan<char> driveName = stackalloc char[] { drive, ':', '\0' };
            int bufferSize = MAX_PATH;
            Span<char> uncBuffer = stackalloc char[bufferSize];
            if (InternalTestHooks.WNetGetConnectionBufferSize > 0)
            {
                bufferSize = InternalTestHooks.WNetGetConnectionBufferSize;
                uncBuffer = uncBuffer.Slice(0, bufferSize);
            }

            char[]? rentedArray = null;
            while (true)
            {
                int errorCode;
                try
                {
                    try
                    {
                        errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);
                    }
                    catch (DllNotFoundException)
                    {
                        s_WNetApiNotAvailable = true;
                        return ERROR_NOT_SUPPORTED;
                    }

                    if (errorCode == ERROR_SUCCESS)
                    {
                        // Cannot rely on bufferSize as it's only set if
                        // the first call ended with ERROR_MORE_DATA,

Review comment (suggestion):
If `WNetGetConnectionBufferSize > MAX_PATH` there will be an exception in line 34.
```suggestion
            if (InternalTestHooks.WNetGetConnectionBufferSize > 0 && InternalTestHooks.WNetGetConnectionBufferSize <= MAX_PATH)
```

Code after:
using System;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Management.Automation.Internal;

internal static partial class Interop
{
    internal static unsafe partial class Windows
    {
        private static bool s_WNetApiNotAvailable;

        [LibraryImport("mpr.dll", EntryPoint = "WNetGetConnectionW", StringMarshalling = StringMarshalling.Utf16)]
        internal static partial int WNetGetConnection(ReadOnlySpan<char> localName, Span<char> remoteName, ref int remoteNameLength);

        internal static int GetUNCForNetworkDrive(char drive, out string? uncPath)
        {
            uncPath = null;
            if (s_WNetApiNotAvailable)
            {
                return ERROR_NOT_SUPPORTED;
            }

            ReadOnlySpan<char> driveName = stackalloc char[] { drive, ':', '\0' };
            int bufferSize = MAX_PATH;
            Span<char> uncBuffer = stackalloc char[MAX_PATH];
            if (InternalTestHooks.WNetGetConnectionBufferSize > 0 && InternalTestHooks.WNetGetConnectionBufferSize <= MAX_PATH)
            {
                bufferSize = InternalTestHooks.WNetGetConnectionBufferSize;
                uncBuffer = uncBuffer.Slice(0, bufferSize);
            }

            char[]? rentedArray = null;
            while (true)
            {
                int errorCode;
                try
                {
                    try
                    {
                        errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);
                    }
                    catch (DllNotFoundException)
                    {
                        s_WNetApiNotAvailable = true;
                        return ERROR_NOT_SUPPORTED;
                    }

                    if (errorCode == ERROR_SUCCESS)
                    {
                        // Cannot rely on bufferSize as it's only set if
                        // the first call ended with ERROR_MORE_DATA,