Skip to content

[FIX] CEA-708: port the C decoder's window/pen guards to the Rust decoder (two panics) - #2516

Open
fourthed wants to merge 1 commit into
CCExtractor:masterfrom
fourthed:fix/cea708-window-bounds
Open

fourthed wants to merge 1 commit into
CCExtractor:masterfrom
fourthed:fix/cea708-window-bounds

Conversation

@fourthed

Copy link
Copy Markdown

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.

A note first: I have only a little Rust experience. I ran into these crashes on my own over-the-air recordings and worked on the fix with an AI assistant (Claude). I've read through the change and tested it as described below, but I'd be grateful for corrections on anything that isn't idiomatic, or on how you'd rather this be handled.

The problem

Two panics in the Rust CEA-708 decoder abort the whole run (panic in a function that cannot unwind, exit 134). The 708 decoders run by default, so this also kills a plain 608 extraction such as ccextractor -in=ts file.ts -out=ass, which is how I found it. They reproduce on 0.96.5 and on current master (2c1c1c6).

  1. Extended character with no current window

    panicked at src/decoder/service_decoder.rs:1142:27:
    index out of bounds: the len is 8 but the index is 18446744073709551615
    

    process_character() indexes windows[current_window as usize] before it checks current_window == -1. The G0, G1 and P16 handlers check first, but handle_extended_char() calls process_character() directly. In my samples an EXT1 + G2 character (10 7D, 10 20) arrives after RST, or after DLW at an edit point.

  2. Set Pen Location to row 15

    panicked at src/decoder/service_decoder.rs:1168:21:
    index out of bounds: the len is 15 but the index is 15
    

    SPL takes a 4-bit row (0-15) without checking it, but rows[] holds CCX_DTVCC_MAX_ROWS (15). One broadcast sends SPL row 15 inside a 4-row window, followed by text.

The C decoder already rejects both inputs, since the fix for #1961 (82109e6); the Rust decoder didn't get the same guards.

The change

All in src/rust/src/decoder/service_decoder.rs, following the C decoder's checks from the #1961 fix:

  • process_character() checks current_window before indexing, and drops a character whose pen is outside the CCX_DTVCC_MAX_ROWS x CCX_DTVCC_MAX_COLUMNS grid.
  • handle_set_pen_location() rejects a pen location outside the current window (the pen stays where it was), as dtvcc_handle_SPL_SetPenLocation does.
  • handle_define_windows() rejects a window larger than CCX_DTVCC_MAX_ROWS x CCX_DTVCC_MAX_COLUMNS, as dtvcc_handle_DFx_DefineWindow does. This also stops a 16-row window from reading past rows[] in copy_to_screen and rollup.
  • Four unit tests, one per guard. test_handle_set_pen_location now defines a 15x64 window so its row 14 / column 63 are inside it.

Repro instructions

Samples: ccextractor_cea708_panic_samples.zip (Google Drive, 2.6 MB). It holds three clips of about 3 seconds each, cut from US ATSC 1.0 MPEG-2 recordings as plain byte ranges of whole TS packets (no remux), plus a README and the full logs below.

Clip Video What the captions do
cea708_spl_row15_crossing_jordan.ts 704x480i, 29.97 fps 4-row window, then SPL row 15, then text
cea708_no_window_after_rst_librarians.ts 720x480i, 29.97 fps RST, then 10 7D
cea708_no_window_after_edit_quarantine.ts 1280x720p, 59.94 fps DLW FF, then 10 20 after a byte-level edit splice

On each clip, either of these commands reproduces it:

ccextractor -in=ts <clip>.ts -out=ass -o out.ass
ccextractor --service all --out=srt <clip>.ts
Clip master 2c1c1c6 with this PR
crossing_jordan exit 134, panic at 1168:21 exit 0
librarians exit 134, panic at 1142:27 exit 0
quarantine exit 134, panic at 1142:27 exit 0

logs/ in the zip has the complete output of both commands on each clip, for both builds.

Testing

  • cargo test in src/rust and src/rust/lib_ccxr passes (452, 57 and 46 tests), on the pinned 1.99.1 toolchain under Linux.
  • cargo fmt --check is clean. cargo clippy --all-targets shows only the warning that is already on master (service_decoder.rs:1530, byte_char_slices).
  • The four new tests fail when added to master's service_decoder.rs unchanged. Two of them panic at the same two lines as above.
  • On the full recordings, master aborts on all three (Crossing Jordan at 19% and Quarantine at 26%, the same points as 0.96.5), and this branch runs all three to the end.
  • On the full Crossing Jordan recording, this branch's 608 .ass output is line-for-line identical to what 0.96.5 produced from a copy where I'd manually changed the one SPL byte, so 608 output is unaffected.

Not included

  • Claude found one more possible path to the same kind of panic while reviewing this patch. The review checked every other place where the pen position indexes rows[], to see whether the panic could still be reached some other way. process_cr() with BOTTOM_TOP print direction sets pen_row = row_count, one past the window (RIGHT_LEFT does the same with pen_column). With a 15-row window, a following HCR would call clear_row(15) and overrun rows[]. The C decoder has the same pattern. I don't have any recordings that trigger it, so I haven't worked on a fix.
  • chore: bump Rust toolchain to 1.100.0 and fix its new warnings #2505 also edits service_decoder.rs. If it merges first, I'll rebase.

I've used CCExtractor for over a decade and greatly appreciate the work of all the maintainers and contributors. Hopefully this patch can help someone else.

🤖 Generated with Claude Code

…oder

Two panics in the Rust 708 decoder abort the whole run ("panic in a
function that cannot unwind") on US OTA MPEG-2 broadcasts. Both reproduce
on 0.96.5 and on current master; the C decoder already rejects the same
input since CCExtractor#1961.

1. EXT1 + G2/G3 character with no current window
   panicked at src/decoder/service_decoder.rs:1142:27:
   index out of bounds: the len is 8 but the index is 18446744073709551615
   process_character() indexed windows[current_window as usize] before
   its own `current_window == -1` check. G0/G1/P16 check in their
   handlers, but handle_extended_char() calls process_character()
   directly. Seen after RST, and after DLW at an edit point.
   Fix: check current_window before indexing.

2. Set Pen Location to row 15
   panicked at src/decoder/service_decoder.rs:1168:21:
   index out of bounds: the len is 15 but the index is 15
   SPL takes a 4-bit row (0-15) unchecked, but rows[] holds
   CCX_DTVCC_MAX_ROWS (15). One broadcast sends SPL row 15 in a 4-row
   window, then text.
   Fix, as in the C decoder (CCExtractor#1961):
   - SPL rejects a pen location outside the current window (pen unchanged)
   - DefineWindow rejects a window larger than CCX_DTVCC_MAX_ROWS x
     CCX_DTVCC_MAX_COLUMNS (the row field allows 16)
   - process_character drops a character whose pen is outside the grid

The DefineWindow check also stops a 16-row window from overrunning rows[]
in copy_to_screen and rollup.

Not changed, noted for a follow-up: process_cr with BOTTOM_TOP print
direction sets pen_row = row_count (and RIGHT_LEFT sets pen_column =
col_count), one past the window. With a 15-row window, a following HCR
calls clear_row(15) and overruns rows[]. The C decoder has the same
pattern. No sample hits it.

Tests: SPL rejection, no-window extended characters, out-of-grid pen,
oversized DefineWindow. The four fail on master (two at the panic sites
above). test_handle_set_pen_location now defines a 15x64 window so
row 14 / col 63 are valid, and sets the reserved high bits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 10180, 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 10180), 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