Skip to content

scuffle-expgolomb: read_exp_golomb underflows with 64+ leading zeros #654

Open

Description

@kixelated

BitReaderExpGolombExt::read_exp_golomb in scuffle-expgolomb 0.1.5 does not bound leading_zeros. With 64 or more leading zeros, result <<= 1 shifts the implicit leading 1 out of the u64, and Ok(result - 1) at src/lib.rs:88 then underflows. Debug builds panic with "attempt to subtract with overflow". Release builds return Ok(u64::MAX) (or another wrapped value), and callers range-check that as if it were real.

use scuffle_bytes_util::BitReader;
use scuffle_expgolomb::BitReaderExpGolombExt;

// 64 zero bits, the marker 1, then 64 zero suffix bits.
let mut data = vec![0u8; 8];
data.push(0x80);
data.extend([0u8; 8]);
let mut r = BitReader::new(std::io::Cursor::new(data));
// debug: panics at lib.rs:88; release: Ok(18446744073709551615)
let _ = r.read_exp_golomb();

The largest value a u64 ue(v) can hold needs 63 leading zeros, so returning io::ErrorKind::InvalidData once leading_zeros reaches 64 would fix both builds. H.264 and H.265 cap ue(v) at 2^32 - 2 (31 leading zeros), if you want the tighter bound. read_signed_exp_golomb inherits the fix.

Found by fuzzing scuffle-h265's SPS parser in moq-dev/moq.

(Written by Claude Opus 5.5)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions