Skip to content

[FIX] Skip corrupt H.264 data instead of exiting - #2492

Merged
cfsmp3 merged 1 commit into
masterfrom
fix/avc-invalid-slice-type
Nov 24, 2026
Merged

cfsmp3 merged 1 commit into
masterfrom
fix/avc-invalid-slice-type

Conversation

@cfsmp3

@cfsmp3 cfsmp3 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

In raising this pull request, I confirm the following (please check boxes):

Reason for this PR:

  • This PR adds new functionality.
  • This PR fixes a bug that I have personally experienced or that a real user has reported and for which a sample exists.
  • This PR is porting code from C to Rust.

Sanity check:

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • If the PR adds new functionality, I've added it to the changelog. If it's just a bug fix, I have NOT added it to the changelog.
  • I am NOT adding new C code unless it's to fix an existing, reproducible bug.

Repro instructions:

ccextractor c4dd893cb9d6….ts addf5e2fc9c2….ts -o out.srt

Both samples are on the Sample Platform (completed/). Each one works on its own, but together, on master:

thread '<unnamed>' panicked at src/avc/nal.rs:642:12:
index out of bounds: the len is 10 but the index is 32

The process exits with code 134 and writes no output. addf5e2f… has no H.264 SPS (Number of NAL_type_7: 0). Concatenated after an H.264 file, its video is fed to the H.264 parser as-is.

Problem

The Rust AVC parser assumes well-formed H.264:

  • slice_type panic. slice_header() uses slice_type as an index into SLICE_TYPES without checking it. The C slice_header() rejects slice_type >= 10 (avc_functions.c), and the Rust port lost that check.
  • Unbounded Exp-Golomb. read_exp_golomb_unsigned() counts leading zeros with no limit. A run of zero bytes then asks read_bits() for hundreds of bits, which is fatal: In next_bits: Argument is greater than the maximum bit number i.e. 64: 471!.
  • Unchecked SPS fields. seq_parameter_set_rbsp() accepts any log2_max_frame_num_minus4 and log2_max_pic_order_cnt_lsb_minus4. slice_header() later uses them as bit counts, with the same fatal error.

Fix

  • Skip a slice whose slice_type is outside SLICE_TYPES, as the C code does.
  • Return a BitstreamError for an Exp-Golomb code with 64 or more leading zeros. Unit tests cover this and valid codes; the new test fails on master.
  • Reject an SPS whose log2 fields exceed 12 (H.264 7.4.2.1.1). The SPS is parsed into a copy, so the previous one stays in effect.

do_nal() errors were already logged and the NAL skipped, so corrupt data now costs only the affected NALs.

Verification

  • Repro: exits 0 with no panic and no fatal error. The 1 caption from the first file is written, then the corrupt slices are logged and skipped. With a third file (7236304c….ts) appended, its captions follow; the first one is merged with a leftover line from the first file.
  • Normal output unchanged: 34 samples with 708 captions plus 15 MP4s (--service all) give byte-identical output and exit codes. None of the new messages appear on them.
  • Rust checks: cargo test passes (448 src/rust, 57 lib_ccxr), and cargo fmt --check and cargo clippy -- -D warnings are clean in both crates.

Not fixed here

addf5e2f…'s own 4 captions are still lost after an H.264 file, because its video keeps going to the H.264 parser. Switching codecs between concatenated inputs is a separate change.

Feeding data that isn't H.264 to the AVC parser (for example a
non-H.264 file concatenated after an H.264 one) made ccextractor
panic or exit:

- slice_header() indexed SLICE_TYPES with an unchecked slice_type
  (panic: "index out of bounds: the len is 10 but the index is 32").
  The C slice_header() rejects values >= 10; the Rust port lost that
  check.
- read_exp_golomb_unsigned() counted leading zeros without a limit,
  so a run of zero bytes asked read_bits() for hundreds of bits,
  which is fatal.
- seq_parameter_set_rbsp() accepted any log2_max_frame_num_minus4 and
  log2_max_pic_order_cnt_lsb_minus4. slice_header() later uses them as
  bit counts, with the same fatal result.

Skip such slices as the C code does, return an error for exp-Golomb
codes with 64 or more leading zeros, and reject an SPS whose log2
fields exceed the H.264 limit of 12, keeping the previous one. do_nal
errors are already logged and the NAL skipped.
@ccextractor-bot

Copy link
Copy Markdown
Collaborator
CCExtractor CI platform finished running the test files on linux. 171/238 tests matched the approved output:
Report Name Tests Passed
Broken 9/13
CEA-708 2/14
DVB 0/7
DVD 3/3
DVR-MS 2/2
General 26/28
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 68/86
Teletext 0/21
WTV 12/13
XDS 32/34

67 tests do not match the approved output. That is the pass/fail verdict. Whether this branch caused it is a separate question, answered below.


Compared with the tip of master — test 10124, commit 034fdbc:

  • 0 pass there and fail here
  • 0 fail there and pass here
  • 0 fail on both, with different output
  • 67 fail on both, byte for byte the same

Compared with the commit this branch was cut from: the same run as the tip of master (test 10124), so the comparison above already covers it.


No test changes behaviour relative to the tip of master: every failure above fails there too, byte for byte. The approved output for those tests is out of date, which is a baseline to review rather than a regression in this branch.

@ccextractor-bot

Copy link
Copy Markdown
Collaborator
CCExtractor CI platform finished running the test files on windows. 171/238 tests matched the approved output:
Report Name Tests Passed
Broken 9/13
CEA-708 2/14
DVB 0/7
DVD 3/3
DVR-MS 2/2
General 26/28
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 68/86
Teletext 0/21
WTV 12/13
XDS 32/34

67 tests do not match the approved output. That is the pass/fail verdict. Whether this branch caused it is a separate question, answered below.


Compared with the tip of master — test 10125, commit 034fdbc:

  • 0 pass there and fail here
  • 0 fail there and pass here
  • 0 fail on both, with different output
  • 67 fail on both, byte for byte the same

Compared with the commit this branch was cut from: the same run as the tip of master (test 10125), so the comparison above already covers it.


No test changes behaviour relative to the tip of master: every failure above fails there too, byte for byte. The approved output for those tests is out of date, which is a baseline to review rather than a regression in this branch.

@cfsmp3
cfsmp3 merged commit a8ebf10 into master Nov 24, 2026
46 of 48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants