Reject a Range first-byte-pos that overflows ssize_t (#2580)

* Reject a Range first-byte-pos that overflows ssize_t

parse_range_header initializes first to the -1 sentinel that means "no
first-byte-pos" and only overwrites it when detail::from_chars succeeds.
On std::errc::result_out_of_range the assignment is skipped and -1
survives, so "bytes=9223372036854775808-100" is parsed as the suffix
range "bytes=-100" and range_error serves the last 100 bytes instead of
returning 416.

Before the parser was rewritten onto detail::from_chars, std::stoll threw
std::out_of_range on the same input, the catch arm added in 8f8761e for
issue #705 returned false, and the request was answered with 416. The
catch arm is still there but from_chars reports through an error code, so
nothing reaches it any more.

get_header_value_u64 and parse_port already reject an out-of-range value
at their from_chars call sites; this was the remaining one that dropped
the error.

The last-byte-pos side is deliberately unchanged: -1 there is the
documented RFC 9110 14.1.2 "remainder of the representation" value, so an
oversized last-byte-pos stays accepted.

* Simplify the Range first-byte-pos overflow check

Parse the first-byte-pos straight into first, since a failed parse now
returns before first is read, and fold the overflow test into the
existing batch of rejected ranges. Also note on the last-byte-pos side
why an overflow there deliberately keeps -1.

Claude-Session: https://claude.ai/code/session_01JYPWKpbp4a881EdpEf2xSi

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
This commit is contained in:
KBS
2026-09-11 13:48:04 -04:00
committed by GitHub
co-authored by yhirose
parent 515b8f84af
commit 8d25b6a3ac
2 changed files with 25 additions and 3 deletions
+15
View File
@@ -2010,8 +2010,23 @@ TEST(ParseHeaderValueTest, Range) {
EXPECT_FALSE(detail::parse_range_header("bytes=0--1", ranges));
EXPECT_FALSE(detail::parse_range_header("bytes=0- 1", ranges));
EXPECT_FALSE(detail::parse_range_header("bytes=0 -1", ranges));
// Overflows ssize_t; must not be read as the suffix range "bytes=-100".
EXPECT_FALSE(
detail::parse_range_header("bytes=9223372036854775808-100", ranges));
EXPECT_TRUE(ranges.empty());
}
{
// RFC 9110 14.1.2: a last-byte-pos greater than the content length is the
// remainder of the representation, so it stays accepted.
Ranges ranges;
auto ret =
detail::parse_range_header("bytes=0-99999999999999999999", ranges);
EXPECT_TRUE(ret);
ASSERT_EQ(1u, ranges.size());
EXPECT_EQ(0, ranges[0].first);
EXPECT_EQ(-1, ranges[0].second);
}
}
TEST(ParseAcceptEncoding1, AcceptEncoding) {