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

Code before:
            uint bufferSize = MAX_PATH;

#if DEBUG
            // In Debug mode buffer size is initially set to 3 and if additional buffer is required, the
            // required buffer size is allocated and the WNetGetConnection API is executed with the newly
            // allocated buffer size.
            bufferSize = 3;
#endif

            ReadOnlySpan<char> driveName = stackalloc char[] { drive, ':', '\0' };
            Span<char> uncBuffer = stackalloc char[(int)bufferSize];
            int errorCode = ERROR_NO_NETWORK;

            try
            {
                errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);
            }
            catch (System.DllNotFoundException)
            {
                s_WNetApiNotAvailable = true;
                return ERROR_NOT_SUPPORTED;
            }

            if (errorCode == ERROR_SUCCESS)
            {
                uncPath = uncBuffer.Slice(0, (int)bufferSize).ToString();
            }
            else if (errorCode == ERROR_MORE_DATA)
            {
                char[]? rentedArray = null;
                try
                {
                    uncBuffer = rentedArray = ArrayPool<char>.Shared.Rent((int)bufferSize);
                    errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);

                    if (errorCode == ERROR_SUCCESS)
                    {
                        uncPath = uncBuffer.Slice(0, (int)bufferSize).ToString();
                    }
                }
                finally
                {
                    if (rentedArray is not null)
                    {
                        ArrayPool<char>.Shared.Return(rentedArray);
                    }
                }
            }

            return errorCode;
        }

Review comment (bug):
In debugger I see extra `\0` last char in `uncPath`. If you see the same we should (below too) something like:
```suggestion
                uncPath = uncBuffer.Slice(0, (int)(bufferSize - 1)).ToString();
```

Code after:
            uint bufferSize = MAX_PATH;

#if DEBUG
            // In Debug mode buffer size is initially set to 3 and if additional buffer is required, the
            // required buffer size is allocated and the WNetGetConnection API is executed with the newly
            // allocated buffer size.
            bufferSize = 3;
#endif

            ReadOnlySpan<char> driveName = stackalloc char[] { drive, ':', '\0' };
            Span<char> uncBuffer = stackalloc char[(int)bufferSize];
            int errorCode = ERROR_NO_NETWORK;

            try
            {
                errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);
            }
            catch (System.DllNotFoundException)
            {
                s_WNetApiNotAvailable = true;
                return ERROR_NOT_SUPPORTED;
            }

            if (errorCode == ERROR_SUCCESS)
            {
                // exclude null terminator
                uncPath = uncBuffer.Slice(0, (int)bufferSize - 1).ToString();
            }
            else if (errorCode == ERROR_MORE_DATA)
            {
                char[]? rentedArray = null;
                try
                {
                    uncBuffer = rentedArray = ArrayPool<char>.Shared.Rent((int)bufferSize);
                    errorCode = WNetGetConnection(driveName, uncBuffer, ref bufferSize);

                    if (errorCode == ERROR_SUCCESS)
                    {
                        // exclude null terminator
                        uncPath = uncBuffer.Slice(0, (int)bufferSize - 1).ToString();
                    }
                }
                finally
                {
                    if (rentedArray is not null)
                    {
                        ArrayPool<char>.Shared.Return(rentedArray);
                    }
                }
            }