Skip to content

Fix/dtvcc allocation panic#2050

Open
pranavshar223 wants to merge 1 commit intoCCExtractor:masterfrom
pranavshar223:fix/dtvcc-allocation-panic
Open

Fix/dtvcc allocation panic#2050
pranavshar223 wants to merge 1 commit intoCCExtractor:masterfrom
pranavshar223:fix/dtvcc-allocation-panic

Conversation

@pranavshar223
Copy link
Contributor

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

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.
  • I have mentioned this change in the changelog.

My familiarity with the project is as follows (check one):

  • I have never used CCExtractor.
  • I have used CCExtractor just a couple of times.
  • I absolutely love CCExtractor, but have not contributed previously.
  • I am an active contributor to CCExtractor.

This PR removes panics from the CEA-708 decoder initialization path.

Previously, allocation failures for dtvcc_tv_screen or
dtvcc_service_decoder would cause a hard panic, crashing CCExtractor.

Instead, allocation failures are now handled gracefully by logging
a debug message and disabling the affected service decoder while
allowing processing to continue.

This improves robustness and aligns Rust behavior with the legacy C
implementation.

Copy link
Contributor

@cfsmp3 cfsmp3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on improving the robustness of the CEA-708 decoder initialization!

The concept is good - handling allocation failures gracefully instead of panicking. However, there are several issues that need to be fixed before this can be merged:

1. CRLF Line Endings Breaking Linux Builds

All files in this PR have Windows-style CRLF line endings instead of Unix LF:

decoder/mod.rs: ASCII text, with CRLF line terminators
pre-build.sh: ASCII text, with CRLF line terminators

This breaks the Linux build - shell scripts with CRLF don't work on Unix systems. This is why the CI is failing:

  • build_shell - FAILURE
  • build_autoconf - FAILURE
  • format_rust - FAILURE

Fix: Configure your git to handle line endings properly:

git config --global core.autocrlf input

Then convert the files:

dos2unix src/rust/src/decoder/mod.rs linux/pre-build.sh

Or rebase your branch on a fresh master checkout.

2. Unrelated Changes from PR #2045

This PR includes changes from #2045 (userdata.rs documentation and debug logging). These should be in a separate PR - #2045 is already pending review.

Please remove src/rust/src/es/userdata.rs from this PR.

3. Messy Commit History

The commits include "retry the test" and "restore all changes" which don't describe the actual changes. Consider squashing into a clean commit with a descriptive message.

4. Minor: Trailing Whitespace

                    continue;
                }       // <-- extra spaces here

The Actual Fix Looks Good

Once the above issues are fixed, the core change is reasonable:

// Before: panic on allocation failure
panic!("Failed to allocate dtvcc_tv_screen");

// After: log and gracefully skip the service
debug!(msg_type = DebugMessageFlag::DECODER_708;
    "DTVCC: Failed to allocate tv_screen for service {}, disabling service", i + 1);
continue;

The cleanup of the already-allocated tv_screen when decoder allocation fails is also correct:

unsafe {
    std::alloc::dealloc(tv_ptr as *mut u8, tv_layout);
}

Summary

Please:

  1. Fix line endings (dos2unix or fresh checkout)
  2. Remove userdata.rs changes (belongs in #2045)
  3. Squash commits into clean history
  4. Push updated branch

Looking forward to the updated PR!

@pranavshar223 pranavshar223 force-pushed the fix/dtvcc-allocation-panic branch from 9b1126b to 6b99a30 Compare February 13, 2026 08:18
Copy link
Contributor

@cfsmp3 cfsmp3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the cleanup — the branch is much better now (single commit, CRLF fixed, unrelated changes removed).

The concept is correct: replacing panic!() with graceful degradation is the right approach. However, there's a double-free bug in the second allocation failure path that needs to be fixed.

The bug

When the decoder_ptr allocation fails (line 306), your code does this:

// Line 297: Box takes ownership of tv_ptr's memory
let mut tv_screen = unsafe { Box::from_raw(tv_ptr) };
tv_screen.cc_count = 0;
tv_screen.service_number = i as i32 + 1;

// ... decoder_ptr alloc fails ...

// Line 312-314: You manually free tv_ptr's memory
unsafe {
    std::alloc::dealloc(tv_ptr as *mut u8, tv_layout);
}
// Line 315: continue causes tv_screen to go out of scope
continue;
// ^^^ Box::drop runs here, calling dealloc on the SAME pointer again → double-free!

The problem is that Box::from_raw(tv_ptr) on line 297 already transferred ownership of that memory to tv_screen. When continue executes, tv_screen goes out of scope, and its Drop implementation will call dealloc on the same pointer you already freed manually. This is undefined behavior (double-free).

The fix

Simply remove the manual dealloc block (lines 312-314). You don't need it — the continue will drop tv_screen, which will correctly free the memory through the Box destructor:

if decoder_ptr.is_null() {
    debug!(
        msg_type = DebugMessageFlag::DECODER_708;
        "DTVCC: Failed to allocate service decoder {}, disabling service",
        i + 1
    );
    continue; // tv_screen drops here, Box::drop frees the memory correctly
}

That's the only change needed. Everything else in this PR looks good.

@pranavshar223 pranavshar223 force-pushed the fix/dtvcc-allocation-panic branch from 6b99a30 to 983735f Compare February 16, 2026 08:38
@pranavshar223 pranavshar223 force-pushed the fix/dtvcc-allocation-panic branch from 983735f to 925193b Compare February 16, 2026 08:44
@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on linux. Below is a summary of the test results, when compared to test for commit ba873ed...:
Report Name Tests Passed
Broken 13/13
CEA-708 14/14
DVB 7/7
DVD 3/3
DVR-MS 2/2
General 27/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 85/86
Teletext 21/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

Congratulations: Merging this PR would fix the following tests:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65..., Last passed: Never
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b..., Last passed: Never
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never

It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you).

Check the result page for more info.

@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on windows. Below is a summary of the test results, when compared to test for commit ba873ed...:
Report Name Tests Passed
Broken 13/13
CEA-708 14/14
DVB 6/7
DVD 3/3
DVR-MS 2/2
General 25/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 81/86
Teletext 21/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65...
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b...
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...

Congratulations: Merging this PR would fix the following tests:


It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you).

Check the result page for more info.

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.

3 participants