Repository: dotnet/aspnetcore
File: src/Http/Headers/src/SetCookieHeaderValue.cs (C#)

Code before:
                    isNegative = true;
                    offset++;
                }

                itemLength = HttpRuleParser.GetNumberLength(input, offset, allowDecimal: false);
                if (itemLength == 0)
                {
                    return 0;
                }

                var numberString = input.Subsegment(offset, itemLength);
                long maxAge;
                if (!HeaderUtilities.TryParseNonNegativeInt64(numberString, out maxAge))
                {
                    // Invalid expiration date, abort
                    return 0;
                }

                if (isNegative)
                {
                    maxAge = -maxAge;
                }

                try
                {
                    result.MaxAge = TimeSpan.FromSeconds(maxAge);
                }
                catch (ArgumentOutOfRangeException)
                {
                    // MaxAge value would overflow TimeSpan, abort
                    return 0;
                }
                offset += itemLength;
            }
            // domain-av = "Domain=" domain-value
            // domain-value = <subdomain> ; defined in [RFC1034], Section 3.5, as enhanced by [RFC1123], Section 2.1
            else if (StringSegment.Equals(token, DomainToken, StringComparison.OrdinalIgnoreCase))
            {
                // = (no spaces)
                if (!ReadEqualsSign(input, ref offset))
                {
                    return 0;
                }
                // We don't do any detailed validation on the domain.
                result.Domain = ReadToSemicolonOrEnd(input, ref offset);
            }
            // path-av = "Path=" path-value
            // path-value = <any CHAR except CTLs or ";">
            else if (StringSegment.Equals(token, PathToken, StringComparison.OrdinalIgnoreCase))
            {
                // = (no spaces)

Review comment (suggestion):
I would definitely prefer not throwing here. Not for perf, but just for cleanliness and readability (more obvious that we're bounds checking)

Code after:
                    isNegative = true;
                    offset++;
                }

                itemLength = HttpRuleParser.GetNumberLength(input, offset, allowDecimal: false);
                if (itemLength == 0)
                {
                    return 0;
                }

                var numberString = input.Subsegment(offset, itemLength);
                long maxAge;
                if (!HeaderUtilities.TryParseNonNegativeInt64(numberString, out maxAge))
                {
                    // Invalid expiration date, abort
                    return 0;
                }

                if (isNegative)
                {
                    maxAge = -maxAge;
                }

                // Check if maxAge would cause TimeSpan.FromSeconds to overflow
                // TimeSpan.MaxValue.TotalSeconds is approximately 922337203685.4775
                const long MaxTimeSpanSeconds = 922337203685L;
                const long MinTimeSpanSeconds = -922337203685L;
                if (maxAge is > MaxTimeSpanSeconds or < MinTimeSpanSeconds)
                {
                    // MaxAge value would overflow TimeSpan, abort
                    return 0;
                }

                result.MaxAge = TimeSpan.FromSeconds(maxAge);
                offset += itemLength;
            }
            // domain-av = "Domain=" domain-value
            // domain-value = <subdomain> ; defined in [RFC1034], Section 3.5, as enhanced by [RFC1123], Section 2.1
            else if (StringSegment.Equals(token, DomainToken, StringComparison.OrdinalIgnoreCase))
            {
                // = (no spaces)
                if (!ReadEqualsSign(input, ref offset))
                {
                    return 0;
                }
                // We don't do any detailed validation on the domain.
                result.Domain = ReadToSemicolonOrEnd(input, ref offset);
            }
            // path-av = "Path=" path-value
            // path-value = <any CHAR except CTLs or ";">
            else if (StringSegment.Equals(token, PathToken, StringComparison.OrdinalIgnoreCase))