Loading
http: reject WGINT_MAX last-byte in Content-Range
Small follow-up to 43d3ba93 and 01ff771c in parse_content_range().
Right now parse_content_range() rejects values that set errno = ERANGE, but it still accepts last_byte == WGINT_MAX (9223372036854775807) when the total length is *.
When gethttp() uses that value, last_byte_pos + 1 overflows signed wgint (int64_t):
- On a normal GET with
Content-Range: bytes 0-9223372036854775807/*,contlenwraps toINT64_MIN, overridesContent-Length, saves an empty 0-byte file, and exits0. - On a resumed download (
-c) withContent-Range: bytes 4-9223372036854775807/*,hs->contlen = contlen + contrangewraps toINT64_MINand hitsabort()inhttp_loop(). - Under UBSan, both paths trap on signed integer overflow in
gethttp().
Since byte offsets are 0-indexed, a valid wgint-sized resource cannot contain byte offset WGINT_MAX.
This change:
- Rejects
*last_byte_ptr == WGINT_MAXinparse_content_range(). - Adds
WGINT_MAX - 1,WGINT_MAX, andfirst_byte/last_byteERANGEcases totest_parse_range_header(), checking output pointers only when parsing succeeds. - Adds
tests/Test-c-content-range-overflow.pxfor the HTTP 206 truncation case.
(Note: the contlen overflow in gethttp() was previously mentioned on the unofficial GitHub mirror in https://github.com/mirror/wget/issues/25.)
Tested on master with unit-tests and PX_TESTS (release and UBSan builds), testenv, and make syntax-check.
Edited by Jabar Asadi