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

Code before:
                {
                    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)
                if (!ReadEqualsSign(input, ref offset))
                {
                    return 0;
                }
                // We don't do any detailed validation on the path.
                result.Path = ReadToSemicolonOrEnd(input, ref offset);

Review comment (bug):
Using try-catch for control flow can impact performance. Consider validating the maxAge value against TimeSpan bounds (922337203685 seconds) before calling TimeSpan.FromSeconds to avoid the exception entirely.
```suggestion
                // Validate maxAge is within TimeSpan bounds before creating TimeSpan
                if (maxAge < TimeSpan.MinValue.TotalSeconds || maxAge > TimeSpan.MaxValue.TotalSeconds)
                {
                    // MaxAge value would overflow TimeSpan, abort
                    return 0;
                }
                result.MaxAge = TimeSpan.FromSeconds(maxAge);
```

Code after:
                {
                    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))
            {
                // = (no spaces)
                if (!ReadEqualsSign(input, ref offset))
                {
                    return 0;
                }