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

Code before:
                    if (frame[1] == 3)
                    {
                        header.Length = ((frame[3] << 8) | frame[4]);
                        header.Version = TlsMinorVersionToProtocol(frame[2]);
                        return true;
                    }
                    else if (frame[2] == (byte)TlsHandshakeType.ClientHello &&
                       frame[3] == 3) // SSL3 or above
                    {
                        int length;
                        if ((frame[0] & 0x80) != 0)
                        {
                            // Two bytes
                            length = (((frame[0] & 0x7f) << 8) | frame[1]) + 2;
                        }
                        else
                        {
                            // Three bytes
                            length = (((frame[0] & 0x3f) << 8) | frame[1]) + 3;
                        }

                        // max frame for SSLv2 is 32767.
                        // However, we expect something reasonable for initial HELLO
                        // We don't have enough logic to verify full validity,
                        // the limits bellow are queses.
                        if (length > 20 && length < 1000)
                        {
#pragma warning disable CS0618 // Ssl2 and Ssl3 are obsolete
                            header.Version = SslProtocols.Ssl2;
#pragma warning restore CS0618
                            header.Length = length;
                            header.Type = TlsContentType.Handshake;
                            return true;
                        }
                    }
                }
            }

            header.Length = -1;
            header.Version = SslProtocols.None;

                return result;
        }

        // Returns frame size e.g. header + content
        public static int GetFrameSize(ReadOnlySpan<byte> frame)
        {
            if (frame.Length < 5 || frame[1] < 3)
            {
                return - 1;
            }

Review comment (bug):
This seems very wishy-washy.  On what were these "guesses" based?  What are the chances these are wrong in a direction that causes us to do the wrong thing?

Code after:
            if (frame[1] == ProtocolVersionTlsMajorValue)
            {
                header.Length = ((frame[3] << 8) | frame[4]);
                header.Version = TlsMinorVersionToProtocol(frame[2]);
            }
            // Sslv2 or Unified
            else if (frame[2] == (byte)TlsHandshakeType.ClientHello &&
                     frame[3] == ProtocolVersionTlsMajorValue) // SSL3 or above
            {
                header.Length = (frame[0] & 0x80) != 0 ?
                                    (((frame[0] & 0x7f) << 8) | frame[1]) + 2 : // two bytes
                                    (((frame[0] & 0x3f) << 8) | frame[1]) + 3;  // three bytes
#pragma warning disable CS0618 // Ssl2 and Ssl3 are obsolete
                header.Version = SslProtocols.Ssl2;
#pragma warning restore CS0618
                header.Type = TlsContentType.Handshake;
            }
            else
            {
                // neither looks like TLS nor Ssl2 Hello
                header.Length = -1;
                header.Version = SslProtocols.None;
                return false;
            }

            return true;
        }

        // Returns frame size e.g. header + content
        public static int GetFrameSize(ReadOnlySpan<byte> frame)
        {
            if (frame.Length < 5 || frame[1] < 3)
            {
                return - 1;
            }

            return ((frame[3] << 8) | frame[4]) + HeaderSize;
        }

        // This function will try to parse TLS hello frame and fill details in provided info structure.
        // If frame was fully processed without any error, function returns true.
        // Otherwise it returns false and info may have partial data.
        // It is OK to call it again if more data becomes available.
        // It is also possible to limit what information is processed.
        // If callback delegate is provided, it will be called on ALL extensions.
        public static bool TryGetFrameInfo(ReadOnlySpan<byte> frame, ref TlsFrameInfo info, ProcessingOptions options = ProcessingOptions.All, HelloExtensionCallback? callback = null)
        {
            const int HandshakeTypeOffset = 5;
            if (frame.Length < HeaderSize)
            {
                return false;