[PR #1999] [MERGED] fix prevent FFI memory leaks in demuxer sync #2801

Closed
opened 2026-01-29 17:23:58 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/CCExtractor/ccextractor/pull/1999
Author: @THE-Amrit-mahto-05
Created: 1/8/2026
Status: Merged
Merged: 1/9/2026
Merged by: @cfsmp3

Base: masterHead: fix/demuxer-ffi-memory-leak


📝 Commits (1)

  • 1911068 fix(rust): prevent FFI memory leaks in demuxer sync

📊 Changes

2 files changed (+59 additions, -43 deletions)

View changed files

📝 src/rust/src/demuxer/common_types.rs (+3 -1)
📝 src/rust/src/libccxr_exports/demuxer.rs (+56 -42)

📄 Description

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

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/CCExtractor/ccextractor/pull/1999 **Author:** [@THE-Amrit-mahto-05](https://github.com/THE-Amrit-mahto-05) **Created:** 1/8/2026 **Status:** ✅ Merged **Merged:** 1/9/2026 **Merged by:** [@cfsmp3](https://github.com/cfsmp3) **Base:** `master` ← **Head:** `fix/demuxer-ffi-memory-leak` --- ### 📝 Commits (1) - [`1911068`](https://github.com/CCExtractor/ccextractor/commit/1911068e9211c40de681ea851d4ef248e5833456) fix(rust): prevent FFI memory leaks in demuxer sync ### 📊 Changes **2 files changed** (+59 additions, -43 deletions) <details> <summary>View changed files</summary> 📝 `src/rust/src/demuxer/common_types.rs` (+3 -1) 📝 `src/rust/src/libccxr_exports/demuxer.rs` (+56 -42) </details> ### 📄 Description <!-- Please prefix your pull request with one of the following: **[FEATURE]** **[FIX]** **[IMPROVEMENT]**. --> **In raising this pull request, I confirm the following (please check boxes):** - [x] I have read and understood the [contributors guide](https://github.com/CCExtractor/ccextractor/blob/master/.github/CONTRIBUTING.md). - [x] I have checked that another pull request for this purpose does not exist. - [x] I have considered, and confirmed that this submission will be valuable to others. - [x] I accept that this submission may not be used, and the pull request closed at the will of the maintainer. - [x] I give this submission freely, and claim no ownership to its content. - [x] **I have mentioned this change in the [changelog](https://github.com/CCExtractor/ccextractor/blob/master/docs/CHANGES.TXT).** **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. - [x] 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 17:23:58 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/ccextractor#2801