Repository: dotnet/runtime
File: src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.cs (C#)

Code before:

                    case AddressFamily.InterNetworkV6:
                        Span<byte> address = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
                        SocketAddressPal.GetIPv6Address(buffer, address, out uint scope);
                        _rightEndPoint = new IPEndPoint(
                            new IPAddress(address, scope),
                            SocketAddressPal.GetPort(buffer));
                        break;

                    case AddressFamily.Unix:
                        socketAddress = new Internals.SocketAddress(_addressFamily, buffer);
                        _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
                        break;
                }

                // Try to determine if we're connected, based on querying for a peer, just as we would in RemoteEndPoint,
                // but ignoring any failures; this is best-effort (RemoteEndPoint also does a catch-all around the Create call).
                if (_rightEndPoint != null)
                {
                    try
                    {
                        socketAddress ??= new Internals.SocketAddress(_addressFamily, buffer);

                        SocketError result = SocketPal.GetPeerName(_handle, socketAddress.Buffer, ref socketAddress.InternalSize);
                        if ((RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && result == SocketError.Fault) ||
                            (socketAddress.InternalSize > socketAddress.Buffer.Length))
                        {
                            // GetPeerName simply fails on Windows it buffer is too small.
                            // On Unix, we may get partial result.
                            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                            {
                                socketAddress.InternalSize = UnixDomainSocketEndPoint.MaxAddressSize;
                            }

                            socketAddress.Buffer = new byte[socketAddress.InternalSize];
                            result = SocketPal.GetPeerName(_handle, socketAddress.Buffer, ref socketAddress.InternalSize);
                        }

                        if (result != SocketError.Success)
                        {
                            return;
                        }

                        _remoteEndPoint = _rightEndPoint.Create(socketAddress);
                        _isConnected = true;
                    }
                    catch { }
                }
            }
            catch
            {

Review comment (suggestion):
The code above that calls `GetSockName` is subject to the same behavior on Windows. It doesn't get into that situation, because the buffer used is `512` bytes, which is sufficiently large. We should use the same buffer here to start with, and not a slice.

On Linux `sockaddr_storage` is `128`, so that is the larges buffer one may need. I don't know what the value is on Windows? Maybe we can capture the cross-platform max in a `const`, and assert it in PAL? Or add a PAL function to retrieve it?

In https://github.com/dotnet/runtime/pull/34727 some code was also added to handle the case where the provided buffer was too small. I don't think we get in that situation, but the enlarged buffer isn't actually larger:
```
                if (bufferLength > buffer.Length)
                {
                    buffer = new byte[buffer.Length];
```
The new buffer allocated should be `bufferLength` length.

Code after:
                        _rightEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
                        break;
                }

                // Try to determine if we're connected, based on querying for a peer, just as we would in RemoteEndPoint,
                // but ignoring any failures; this is best-effort (RemoteEndPoint also does a catch-all around the Create call).
                if (_rightEndPoint != null)
                {
                    try
                    {
                        // Local and Remote EP may be different sizes for something like UDS.
                        bufferLength = buffer.Length;
                        if (SocketPal.GetPeerName(handle, buffer, ref bufferLength) != SocketError.Success)
                        {
                            return;
                        }

                        switch (_addressFamily)
                        {
                            case AddressFamily.InterNetwork:
                            _remoteEndPoint = new IPEndPoint(
                                new IPAddress((long)SocketAddressPal.GetIPv4Address(buffer.Slice(0, bufferLength)) & 0x0FFFFFFFF),
                                SocketAddressPal.GetPort(buffer));
                            break;

                            case AddressFamily.InterNetworkV6:
                                Span<byte> address = stackalloc byte[IPAddressParserStatics.IPv6AddressBytes];
                                SocketAddressPal.GetIPv6Address(buffer.Slice(0, bufferLength), address, out uint scope);
                                _remoteEndPoint = new IPEndPoint(
                                    new IPAddress(address, scope),
                                    SocketAddressPal.GetPort(buffer));
                                break;

                            case AddressFamily.Unix:
                                socketAddress = new Internals.SocketAddress(_addressFamily, buffer.Slice(0, bufferLength));
                                _remoteEndPoint = new UnixDomainSocketEndPoint(IPEndPointExtensions.GetNetSocketAddress(socketAddress));
                                break;
                        }

                        _isConnected = true;
                    }
                    catch { }
                }
            }
            catch
            {
                _handle = null!;
                GC.SuppressFinalize(this);
                throw;
            }
        }