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

Code before:
        {
        }

        private unsafe Socket(SafeSocketHandle handle, bool loadPropertiesFromHandle)
        {
            InitializeSockets();

            _handle = handle;
            _addressFamily = AddressFamily.Unknown;
            _socketType = SocketType.Unknown;
            _protocolType = ProtocolType.Unknown;

            if (!loadPropertiesFromHandle)
            {
                return;
            }

            try
            {
                // Get properties like address family and blocking mode from the OS.
                LoadSocketTypeFromHandle(handle, out _addressFamily, out _socketType, out _protocolType, out _willBlockInternal, out _isListening);

                // Try to get the address of the socket.
                Span<byte> buffer = stackalloc byte[SocketPal.MaximumAddressSize];
                int bufferLength = buffer.Length;
                fixed (byte* bufferPtr = buffer)
                {
                    if (SocketPal.GetSockName(handle, bufferPtr, &bufferLength) != SocketError.Success)
                    {
                        return;
                    }
                }

                Debug.Assert(bufferLength <= buffer.Length);

                // Try to get the local end point.  That will in turn enable the remote
                // end point to be retrieved on-demand when the property is accessed.
                Internals.SocketAddress? socketAddress = null;
                switch (_addressFamily)
                {
                    case AddressFamily.InterNetwork:
                        _rightEndPoint = new IPEndPoint(
                            new IPAddress((long)SocketAddressPal.GetIPv4Address(buffer) & 0x0FFFFFFFF),
                            SocketAddressPal.GetPort(buffer));
                        break;

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

Review comment (bug):
It'd be good to assert that MaximumAddressSize is something reasonable to stackalloc, e.g.
```C#
Debug.Assert(SocketPal.MaximumAddressSize <= 512);
```

Code after:
        {
        }

        private unsafe Socket(SafeSocketHandle handle, bool loadPropertiesFromHandle)
        {
            InitializeSockets();

            _handle = handle;
            _addressFamily = AddressFamily.Unknown;
            _socketType = SocketType.Unknown;
            _protocolType = ProtocolType.Unknown;

            if (!loadPropertiesFromHandle)
            {
                return;
            }

            try
            {
                // Get properties like address family and blocking mode from the OS.
                LoadSocketTypeFromHandle(handle, out _addressFamily, out _socketType, out _protocolType, out _willBlockInternal, out _isListening);

                // We should change stackalloc if this ever grows too big.
                Debug.Assert(SocketPal.MaximumAddressSize <= 512);
                // Try to get the address of the socket.
                Span<byte> buffer = stackalloc byte[SocketPal.MaximumAddressSize];
                int bufferLength = buffer.Length;
                fixed (byte* bufferPtr = buffer)
                {
                    if (SocketPal.GetSockName(handle, bufferPtr, &bufferLength) != SocketError.Success)
                    {
                        return;
                    }
                }

                Debug.Assert(bufferLength <= buffer.Length);

                // Try to get the local end point.  That will in turn enable the remote
                // end point to be retrieved on-demand when the property is accessed.
                Internals.SocketAddress? socketAddress = null;
                switch (_addressFamily)
                {
                    case AddressFamily.InterNetwork:
                        _rightEndPoint = 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);