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/*, contlen wraps to INT64_MIN, overrides Content-Length, saves an empty 0-byte file, and exits 0.
  • On a resumed download (-c) with Content-Range: bytes 4-9223372036854775807/*, hs->contlen = contlen + contrange wraps to INT64_MIN and hits abort() in http_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_MAX in parse_content_range().
  • Adds WGINT_MAX - 1, WGINT_MAX, and first_byte/last_byte ERANGE cases to test_parse_range_header(), checking output pointers only when parsing succeeds.
  • Adds tests/Test-c-content-range-overflow.px for 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

Merge request reports

Loading
Loading