[BUG] read_exp_golomb_unsigned panics/aborts on 64 or more leading zero bits (malformed SPS)

Open
#2,470 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Souravrajvi0 is already working on this.

Since Nov 18, 2026.

  • #2471 by @Souravrajvi0 — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, rust
Domain
backend

Research direction

Start in src/rust/lib_ccxr/src/common/bitstream.rs with read_exp_golomb_unsigned, then trace the C forwarding function in cc_bitstream.c and its use from seq_parameter_set_rbsp() in avc_functions.c. Add a unit test decoding 64 zero bits and check that malformed input returns a BitstreamError rather than panicking or terminating the process; verify the SPS path can skip the NAL.

Written by the indexing model from the issue text.

Description

Summary
BitStreamRust::read_exp_golomb_unsigned (src/rust/lib_ccxr/src/common/bitstream.rs) does not limit the number of leading zero bits, so a short malformed H.264 SPS can panic (debug builds), return a wrong value (release builds) or terminate the process.

pub fn read_exp_golomb_unsigned(&mut self) -> Result<u64, BitstreamError> {
    let mut zeros = 0;
    while self.read_bits(1)? == 0 && self.bits_left >= 0 {
        zeros += 1;
    }
    let remaining_bits = self.read_bits(zeros)?;
    let res = ((1u64 << zeros) - 1) + remaining_bits;
    Ok(res)
}
  • Exactly 64 leading zeros: 1u64 << 64 overflows. Debug build: panicked at ... attempt to shift left with overflow. Release build: the shift count is masked, so the result is silently wrong (1u64 << 0).
  • More than 64 leading zeros: read_bits rejects bnum > 64 with fatal!, which exits the process.

Reachability
cc_bitstream.c forwards read_exp_golomb_unsigned() to this function, and seq_parameter_set_rbsp() in avc_functions.c calls it for seq_parameter_set_id. An SPS made of the 3-byte header followed by 8-9 zero bytes reaches it from do_NAL().

Reproduction
A lib_ccxr unit test that decodes 64 zero bits panics in a debug build. 9 zero bytes in an SPS makes the process exit through fatal!.

Expected
A BitstreamError that lets the caller skip the NAL.

Suggested fix
Return Err when zeros >= 64 (and have read_bits return Err instead of calling fatal! for bnum > 64). A related spot is slice_header(), which does 1 << log2_max_frame_num in C; H.264 only allows 4..16 there.

Dominant language
C
Stars
919
Forks
604
Avg merge
9d 5h
Merged PRs (30d)
26

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from CCExtractor/ccextractor

All issues in CCExtractor/ccextractor

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.