Skip to content

Return an error for degenerate Range/ContentRange bounds instead of overflowing (#231) - #235

Open
youdie006 wants to merge 1 commit into
hyperium:masterfrom
youdie006:fix/231-range-bytes-checked-arithmetic
Open

youdie006 wants to merge 1 commit into
hyperium:masterfrom
youdie006:fix/231-range-bytes-checked-arithmetic

Conversation

@youdie006

Copy link
Copy Markdown

Fixes #231.

Problem

Range::bytes and ContentRange::bytes do unchecked u64 arithmetic on the bounds:

  • Range::bytes(0u64..0u64) computes end - 1 on the exclusive end → panics in debug (attempt to subtract with overflow), and in release emits bytes=0-18446744073709551615 (a 2^64-byte span). Range::bytes(3u64..3u64) yields bytes=3-2 (start > last).
  • ContentRange::bytes has the same class of bug: Excluded-start s + 1 overflows at u64::MAX, Excluded-end e - 1 underflows at 0, and an Unbounded end with complete_length: Some(0) underflows on max - 1.

Fix

Both constructors already return a Result and already Err on other invalid inputs (e.g. unbounded-start arms), so the contract-consistent fix is to return that same error for degenerate/empty/overflowing bounds using checked arithmetic, rather than panicking or emitting an invalid header.

Test

Added test_bytes_rejects_degenerate_bounds (range.rs) and bytes_rejects_degenerate_bounds (content_range.rs). Red-green verified with cargo test: before the change both panic with attempt to subtract with overflow; after, the degenerate cases return Err and valid ranges still succeed. cargo fmt --check is clean and the change adds no new clippy warnings.


Disclosure: I used AI assistance (Claude) while preparing this change. I reproduced the overflow, ran the tests (red-green), and take responsibility for the contribution.

…verflowing

Range::bytes and ContentRange::bytes did unchecked u64 arithmetic on the
bounds. An exclusive end of 0 (e.g. Range::bytes(0..0)) computed `end - 1`,
which panics in debug and, in release, produces a bogus 2^64-byte or
negative-width range header; ContentRange had the same class of overflow on
Excluded-start (`s + 1`), Excluded-end (`e - 1`), and Unbounded-end with a
complete_length of 0 (`max - 1`).

Both constructors already return a Result and error on other invalid inputs,
so return that same error for degenerate/empty/overflowing bounds via checked
arithmetic. Add regression tests.

Fixes hyperium#231
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant