Skip to content

[FIX] Accept a NULL encoder in the MPEG-2 ES parser (report mode) - #2502

Open
cfsmp3 wants to merge 1 commit into
masterfrom
fix/es-report-null-encoder
Open

cfsmp3 wants to merge 1 commit into
masterfrom
fix/es-report-null-encoder

Conversation

@cfsmp3

@cfsmp3 cfsmp3 commented Nov 24, 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 (debug build, ./build -debug):

ccextractor 01509e4d27bd….ts --out=report

On master it aborts:

thread '<unnamed>' panicked at src/es/mod.rs:60:32:
null reference produced

Any MPEG-2 video with a GOP header does this. With ASan, 26 of the 28 MPEG-2 samples in our local set abort in report mode.

Problem

--out=report creates no encoder, so C passes enc_ctx = NULL to ccxr_read_gop_info(), ccxr_read_pic_info() and ccxr_read_eau_info(). The wrappers turn it into &mut *enc_ctx, which is undefined behaviour. Debug builds catch it and abort. Release builds happen to work, because the ES parser never reads through the pointer. It only hands it back to C (store_hdcc(), process_hdcc()), which accepts NULL.

Fix

Keep enc_ctx as *mut encoder_ctx through the ES parser (gop, pic, eau, userdata). read_eau_info() and extension_and_user_data() become unsafe fn like their siblings, since they now forward a raw pointer.

The AVC path took another route for the same NULL (fecd24d): it skips parsing when there is no encoder. That route isn't taken here, because it loses caption detection: the report would say EIA-608: No for every MPEG-2 file.

Verification

  • Test first: es::tests::test_null_enc_ctx_is_accepted calls the three wrappers with a NULL enc_ctx. On master it fails with the same "null reference produced" panic; with the fix it passes. The full cargo test passes (449 tests), as does cargo clippy -- -D warnings.
  • Repro: the debug build now completes and reports EIA-608: Yes / CEA-708: Yes. That's byte-identical to the master release build's report.
  • ASan, report mode: of 28 MPEG-2 samples, master aborts on 26 and the fix on none.
  • Release sweep over 45 local samples (.ts, .mpg, .vob, .wtv, .dvr-ms): the SRT output is byte-identical to master on all 45, and --out=report completes on all of them.

Not fixed here

--videoedited with two or more TS inputs crashes on master, with or without --out=report. general_loop() frees dec_ctx->xds_ctx at the end of each file without clearing it (a double free), and the second file then hits a NULL memmove in copy_payload_to_capbuf(). That's a separate PR.

With --out=report no encoder is created, so C passes a NULL enc_ctx
to ccxr_read_gop_info(), ccxr_read_pic_info() and
ccxr_read_eau_info(). The wrappers turned it into a &mut reference,
which is undefined behaviour. Debug builds abort with "null reference
produced" on any MPEG-2 video; release builds happen to work because
the pointer is only handed back to C (store_hdcc, process_hdcc),
which accepts NULL.

Keep enc_ctx as a raw pointer through the ES parser instead. Unlike
the AVC path, which skips parsing without an encoder, this keeps the
report's caption detection working.
@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 10181, commit 2c1c1c6:

  • 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 10181), 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 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 10178, commit f56427e:

  • 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 10178), 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.

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