[PR #1840] [MERGED] fix(memory): Fix uninitialized memory and memory leaks found by Valgrind #2598

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

📋 Pull Request Information

Original PR: https://github.com/CCExtractor/ccextractor/pull/1840
Author: @cfsmp3
Created: 12/17/2025
Status: Merged
Merged: 12/18/2025
Merged by: @cfsmp3

Base: masterHead: fix/valgrind-memory-issues


📝 Commits (9)

  • 89849d3 fix(memory): Fix uninitialized memory and memory leaks found by Valgrind
  • 683468e fix(memory): Fix use-after-free and memory leaks in Rust FFI
  • 3d5d8e2 fix(memory): Fix major memory leaks in Rust FFI demuxer and decoder
  • 7b1a169 fix(memory): Fix use-after-free in Teletext and uninitialized variables
  • 627e085 fix(memory): Fix 608 decoder memory leak in dec_sub.data
  • 4a30434 fix(memory): Fix XDS memory leaks in encoder and decoder cleanup
  • d72646a fix(memory): Fix XDS memory leak in rcwt_loop path
  • 4429067 fix(rust): Fix Drop compatibility and formatting issues
  • 859741a fix(rust): Remove unused import free_rust_c_string_array

📊 Changes

15 files changed (+302 additions, -64 deletions)

View changed files

📝 src/lib_ccx/ccx_decoders_common.c (+25 -0)
📝 src/lib_ccx/ccx_decoders_xds.c (+2 -0)
📝 src/lib_ccx/ccx_demuxer.c (+1 -1)
📝 src/lib_ccx/ccx_encoders_common.c (+5 -0)
📝 src/lib_ccx/ccx_encoders_spupng.c (+1 -3)
📝 src/lib_ccx/general_loop.c (+33 -22)
📝 src/lib_ccx/lib_ccx.c (+2 -1)
📝 src/lib_ccx/telxcc.c (+3 -1)
📝 src/lib_ccx/ts_info.c (+15 -0)
📝 src/rust/src/common.rs (+74 -18)
📝 src/rust/src/decoder/mod.rs (+7 -4)
📝 src/rust/src/demuxer/common_types.rs (+28 -0)
📝 src/rust/src/demuxer/demux.rs (+42 -6)
📝 src/rust/src/libccxr_exports/demuxer.rs (+19 -8)
📝 src/rust/src/utils.rs (+45 -0)

📄 Description

Summary

Comprehensive memory fixes found via Valgrind testing. This PR eliminates memory leaks, use-after-free bugs, and uninitialized memory issues across both C and Rust code.

Commits

  1. Fix uninitialized memory and memory leaks - Use calloc() instead of malloc() in demuxer/decoder init
  2. Fix use-after-free and memory leaks in Rust FFI - Prevent dangling pointers when copy_from_rust() is called multiple times
  3. Fix major memory leaks in Rust FFI demuxer/decoder - Fix 55MB-331MB leaks in Dtvcc encoder allocation
  4. Fix use-after-free in Teletext - Properly null out dec_ctx->private_data after Teletext context is freed
  5. Fix 608 decoder memory leak - Free DVB bitmap data in dinit_cc_decode()
  6. Fix XDS encoder/decoder leaks - Free xds_str when skipping invalid timestamps
  7. Fix XDS rcwt_loop leak - Add cleanup for --in=bin and --in=raw formats
  8. Fix Rust CI - Update test code for Drop compatibility + formatting

Valgrind Test Results (245 tests)

Test Range Perfect Memory Leaks Timeouts
Tests 1-99 TBD TBD TBD
Tests 100-150 33 0 17
Tests 151-200 48 0 0
Tests 200-245 37 0 4

All tested ranges show 0 bytes definitely lost.

Key Fixes Verified

  • Test 114: 0 bytes (was 100 bytes) - XDS encoder leak
  • Tests 217/218: 0 bytes (was 880 bytes) - rcwt_loop XDS leak
  • Tests 24, 26-28: 0 bytes (was 55MB-331MB) - Rust FFI leaks

Technical Details

C Code Fixes

  • ccx_demuxer.c: calloc() in init_demuxer()
  • lib_ccx.c: calloc() in init_decoder_setting()
  • telxcc.c: calloc() in telxcc_init()
  • general_loop.c: Initialize variables, add rcwt_loop cleanup
  • ccx_decoders_common.c: Free DVB bitmap and XDS strings in cleanup
  • ccx_encoders_common.c: Free XDS strings on invalid timestamp skip

Rust FFI Fixes

  • utils.rs: Add replace_rust_c_string(), free_rust_c_string_array()
  • common.rs: Only set inputfile/enc_cfg on first call to prevent leaks
  • demuxer.rs: Direct C struct manipulation in ccxr_demuxer_close()
  • decoder/mod.rs: Only allocate encoder when ctx.encoder is null
  • common_types.rs: Add Drop for CcxDemuxer to free FFI allocations

Known Pre-existing Issues

  • Test 19: 5536 bytes leaked from teletext with --datapid (complex interaction, pre-existing)
  • Uninitialized value warnings in some tests (not memory leaks)

Test plan

  • Build successfully on Linux
  • Cargo fmt passes
  • Cargo test passes (269 tests)
  • Valgrind tests 100-245: 0 memory leaks
  • Basic functionality verified with sample files

🤖 Generated with Claude Code


🔄 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/1840 **Author:** [@cfsmp3](https://github.com/cfsmp3) **Created:** 12/17/2025 **Status:** ✅ Merged **Merged:** 12/18/2025 **Merged by:** [@cfsmp3](https://github.com/cfsmp3) **Base:** `master` ← **Head:** `fix/valgrind-memory-issues` --- ### 📝 Commits (9) - [`89849d3`](https://github.com/CCExtractor/ccextractor/commit/89849d321f95a92c5698d3f451b22a15a6261eff) fix(memory): Fix uninitialized memory and memory leaks found by Valgrind - [`683468e`](https://github.com/CCExtractor/ccextractor/commit/683468e233f5f81f16d7a5941eb5ba494dc6ea39) fix(memory): Fix use-after-free and memory leaks in Rust FFI - [`3d5d8e2`](https://github.com/CCExtractor/ccextractor/commit/3d5d8e2a0a3e6e45d3bdcfb434edc7a190f8f0dc) fix(memory): Fix major memory leaks in Rust FFI demuxer and decoder - [`7b1a169`](https://github.com/CCExtractor/ccextractor/commit/7b1a169b8fcb2ec9ba082e1a05f5a36c34cd0ef7) fix(memory): Fix use-after-free in Teletext and uninitialized variables - [`627e085`](https://github.com/CCExtractor/ccextractor/commit/627e0855ceb121240c42b2e8e25e89fa83f58fc0) fix(memory): Fix 608 decoder memory leak in dec_sub.data - [`4a30434`](https://github.com/CCExtractor/ccextractor/commit/4a304346c9b3df6ffd6755bc8304b8b60be5cb0a) fix(memory): Fix XDS memory leaks in encoder and decoder cleanup - [`d72646a`](https://github.com/CCExtractor/ccextractor/commit/d72646ac8503a4d11b09339c8ef0bd871c4a004c) fix(memory): Fix XDS memory leak in rcwt_loop path - [`4429067`](https://github.com/CCExtractor/ccextractor/commit/44290679657e13776af9bf06f029e9a6e198f5a5) fix(rust): Fix Drop compatibility and formatting issues - [`859741a`](https://github.com/CCExtractor/ccextractor/commit/859741a22c982e41fb8526e29fdd48acadc54dce) fix(rust): Remove unused import free_rust_c_string_array ### 📊 Changes **15 files changed** (+302 additions, -64 deletions) <details> <summary>View changed files</summary> 📝 `src/lib_ccx/ccx_decoders_common.c` (+25 -0) 📝 `src/lib_ccx/ccx_decoders_xds.c` (+2 -0) 📝 `src/lib_ccx/ccx_demuxer.c` (+1 -1) 📝 `src/lib_ccx/ccx_encoders_common.c` (+5 -0) 📝 `src/lib_ccx/ccx_encoders_spupng.c` (+1 -3) 📝 `src/lib_ccx/general_loop.c` (+33 -22) 📝 `src/lib_ccx/lib_ccx.c` (+2 -1) 📝 `src/lib_ccx/telxcc.c` (+3 -1) 📝 `src/lib_ccx/ts_info.c` (+15 -0) 📝 `src/rust/src/common.rs` (+74 -18) 📝 `src/rust/src/decoder/mod.rs` (+7 -4) 📝 `src/rust/src/demuxer/common_types.rs` (+28 -0) 📝 `src/rust/src/demuxer/demux.rs` (+42 -6) 📝 `src/rust/src/libccxr_exports/demuxer.rs` (+19 -8) 📝 `src/rust/src/utils.rs` (+45 -0) </details> ### 📄 Description ## Summary Comprehensive memory fixes found via Valgrind testing. This PR eliminates memory leaks, use-after-free bugs, and uninitialized memory issues across both C and Rust code. ## Commits 1. **Fix uninitialized memory and memory leaks** - Use `calloc()` instead of `malloc()` in demuxer/decoder init 2. **Fix use-after-free and memory leaks in Rust FFI** - Prevent dangling pointers when `copy_from_rust()` is called multiple times 3. **Fix major memory leaks in Rust FFI demuxer/decoder** - Fix 55MB-331MB leaks in Dtvcc encoder allocation 4. **Fix use-after-free in Teletext** - Properly null out `dec_ctx->private_data` after Teletext context is freed 5. **Fix 608 decoder memory leak** - Free DVB bitmap data in `dinit_cc_decode()` 6. **Fix XDS encoder/decoder leaks** - Free `xds_str` when skipping invalid timestamps 7. **Fix XDS rcwt_loop leak** - Add cleanup for `--in=bin` and `--in=raw` formats 8. **Fix Rust CI** - Update test code for Drop compatibility + formatting ## Valgrind Test Results (245 tests) | Test Range | Perfect | Memory Leaks | Timeouts | |------------|---------|--------------|----------| | Tests 1-99 | TBD | TBD | TBD | | Tests 100-150 | 33 | **0** | 17 | | Tests 151-200 | 48 | **0** | 0 | | Tests 200-245 | 37 | **0** | 4 | **All tested ranges show 0 bytes definitely lost.** ### Key Fixes Verified - Test 114: 0 bytes (was 100 bytes) - XDS encoder leak - Tests 217/218: 0 bytes (was 880 bytes) - rcwt_loop XDS leak - Tests 24, 26-28: 0 bytes (was 55MB-331MB) - Rust FFI leaks ## Technical Details ### C Code Fixes - `ccx_demuxer.c`: `calloc()` in `init_demuxer()` - `lib_ccx.c`: `calloc()` in `init_decoder_setting()` - `telxcc.c`: `calloc()` in `telxcc_init()` - `general_loop.c`: Initialize variables, add rcwt_loop cleanup - `ccx_decoders_common.c`: Free DVB bitmap and XDS strings in cleanup - `ccx_encoders_common.c`: Free XDS strings on invalid timestamp skip ### Rust FFI Fixes - `utils.rs`: Add `replace_rust_c_string()`, `free_rust_c_string_array()` - `common.rs`: Only set inputfile/enc_cfg on first call to prevent leaks - `demuxer.rs`: Direct C struct manipulation in `ccxr_demuxer_close()` - `decoder/mod.rs`: Only allocate encoder when `ctx.encoder` is null - `common_types.rs`: Add `Drop` for `CcxDemuxer` to free FFI allocations ## Known Pre-existing Issues - **Test 19**: 5536 bytes leaked from teletext with `--datapid` (complex interaction, pre-existing) - Uninitialized value warnings in some tests (not memory leaks) ## Test plan - [x] Build successfully on Linux - [x] Cargo fmt passes - [x] Cargo test passes (269 tests) - [x] Valgrind tests 100-245: 0 memory leaks - [x] Basic functionality verified with sample files 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <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:00 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/ccextractor#2598