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

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

Original Pull Request: https://github.com/CCExtractor/ccextractor/pull/1840

State: closed
Merged: Yes


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

**Original Pull Request:** https://github.com/CCExtractor/ccextractor/pull/1840 **State:** closed **Merged:** Yes --- ## 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)
claunia added the pull-request label 2026-01-29 17:23:01 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/ccextractor#2603