Skip to content

Conversation

@THE-Amrit-mahto-05
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.

Summary

fixes multiple memory leaks and unsafe pointer overwrites in the Rust–C FFI demuxer synchronization logic.

The issues occur when Rust demuxer state is copied into C structures repeatedly (e.g. during resets), causing existing C-owned pointers to be overwritten without being freed.


Problem

  1. In copy_demuxer_from_rust_to_c, existing pointers in PID_buffers and PIDs_programs were overwritten without freeing the previous allocations, leading to cumulative memory leaks during demuxing.

  2. PSIBuffer::default() initialized buffer with a dummy heap allocation instead of NULL, which:

    • Creates a small but persistent leak
    • Makes ownership and validity ambiguous on the C side

Both issues become more visible when the demuxer is reset or reused multiple times.


Fixes

  • demuxer.rs

    • Free existing C pointers before overwriting them
    • Add NULL checks before calling Box::from_raw to avoid undefined behavior
  • common_types.rs

    • Initialize PSIBuffer::buffer with std::ptr::null_mut() instead of a 1-byte allocation
    • Clarify Rust ↔ C ownership semantics

Impact

  • Prevents repeated memory leaks during long-running demuxing
  • Makes FFI ownership rules explicit and safer
  • No functional behavior change expected

Verification

  • Built CCExtractor successfully with the changes
  • cargo test passes in src/rust
  • Manual demuxing on sample input shows no regression

@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 cfb10d4...:
Report Name Tests Passed
Broken 13/13
CEA-708 14/14
DVB 6/7
DVD 3/3
DVR-MS 2/2
General 27/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 86/86
Teletext 21/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2...

Congratulations: Merging this PR would fix the following tests:

  • 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 cfb10d4...:
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 86/86
Teletext 21/21
WTV 13/13
XDS 34/34

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 --out=spupng c83f765c66..., 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

All tests passed completely.

Check the result page for more info.

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.

Review: ✅ Approved

Fixes multiple memory leaks in Rust-C FFI demuxer synchronization.

Changes:

  1. demuxer.rs: Frees existing C pointers before overwriting them in copy_demuxer_from_rust_to_c()
  2. common_types.rs: Initializes PSIBuffer::buffer with null_mut() instead of dummy heap allocation

Analysis:
The fix is correct. When syncing state from Rust to C repeatedly, the old code would overwrite C-side pointers without freeing them first, causing cumulative memory leaks. The new code properly frees existing allocations before creating new ones.

The POISON_PTR_PATTERN check (0xcdcdcdcd) is a defensive measure against uninitialized memory in debug builds.

Testing:

  • ✅ Builds successfully
  • ✅ Tested with sample - works correctly

Verdict: Ready to merge.

@cfsmp3 cfsmp3 merged commit d837c36 into CCExtractor:master Jan 9, 2026
22 of 24 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.

3 participants