[PR #1828] [MERGED] fix: Comprehensive bug fixes - Phases 2-4 (Memory, Buffer, Rust FFI) #2580

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

📋 Pull Request Information

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

Base: masterHead: fix/memory-issues-batch-2.1


📝 Commits (10+)

  • 95f6f09 fix(memory): Fix memory leaks in ocr.c and ts_tables_epg.c
  • 390c96f fix(memory): Fix memory leaks and unsafe realloc patterns in multiple files
  • 3b0a63d fix(memory): Fix memory leaks and unsafe realloc patterns in lib_ccx, utility, avc_functions
  • ea4f884 fix(memory): Fix unsafe realloc patterns in asf_functions, telxcc, and ccx_encoders_srt
  • 5b286c5 fix(memory): Fix potential memory leaks in encoder files
  • 494b14b fix(memory): Fix memory issues in helpers, splitbysentence, and output
  • 90519e2 fix(memory): Fix memory issues in final batch of files (Batch 2.7)
  • af3ab5a fix(buffer): Replace unsafe string functions with safe alternatives
  • d8048bc fix(rust): Complete Phase 4 - FFI safety and documentation
  • d202a66 style(rust): Apply cargo fmt formatting

📊 Changes

43 files changed (+608 additions, -275 deletions)

View changed files

📝 src/lib_ccx/asf_functions.c (+12 -4)
📝 src/lib_ccx/avc_functions.c (+12 -4)
📝 src/lib_ccx/ccx_decoders_608.c (+6 -4)
📝 src/lib_ccx/ccx_encoders_helpers.c (+9 -1)
📝 src/lib_ccx/ccx_encoders_smptett.c (+7 -3)
📝 src/lib_ccx/ccx_encoders_splitbysentence.c (+22 -5)
📝 src/lib_ccx/ccx_encoders_spupng.c (+33 -14)
📝 src/lib_ccx/ccx_encoders_srt.c (+7 -2)
📝 src/lib_ccx/ccx_encoders_ssa.c (+7 -2)
📝 src/lib_ccx/ccx_encoders_webvtt.c (+12 -4)
📝 src/lib_ccx/ccx_gxf.c (+3 -2)
📝 src/lib_ccx/dvb_subtitle_decoder.c (+3 -0)
📝 src/lib_ccx/dvd_subtitle_decoder.c (+30 -1)
📝 src/lib_ccx/ffmpeg_intgr.c (+29 -6)
📝 src/lib_ccx/general_loop.c (+7 -2)
📝 src/lib_ccx/hardsubx.c (+19 -0)
📝 src/lib_ccx/lib_ccx.c (+7 -0)
📝 src/lib_ccx/myth.c (+7 -3)
📝 src/lib_ccx/ocr.c (+48 -16)
📝 src/lib_ccx/output.c (+7 -2)

...and 23 more files

📄 Description

Summary

This PR contains comprehensive bug fixes from Phases 2-4 of the bug analysis cycle, addressing memory issues, buffer overruns, and Rust FFI safety across the entire codebase.


Phase 2: Memory Issues (78 fixes)

Memory Leak Fixes

  • ocr.c, ts_tables_epg.c: Fixed memory leaks in OCR and EPG table handling
  • lib_ccx files: Fixed leaks in utility, avc_functions, and core library functions
  • Encoder files: Fixed potential memory leaks in encoder subsystem
  • helpers, splitbysentence, output: Fixed various memory management issues

Unsafe Realloc Pattern Fixes

  • asf_functions.c, telxcc.c, ccx_encoders_srt.c: Replaced unsafe ptr = realloc(ptr, ...) patterns
  • Multiple files: Added proper NULL checks after realloc to prevent memory corruption

Files Modified (Phase 2)

  • src/lib_ccx/ocr.c
  • src/lib_ccx/ts_tables_epg.c
  • src/lib_ccx/utility.c
  • src/lib_ccx/avc_functions.c
  • src/lib_ccx/lib_ccx.c
  • src/lib_ccx/asf_functions.c
  • src/lib_ccx/telxcc.c
  • src/lib_ccx/ccx_encoders_srt.c
  • src/lib_ccx/ccx_encoders_helpers.c
  • src/lib_ccx/ccx_encoders_splitbysentence.c
  • src/lib_ccx/output.c
  • And more...

Phase 3: Buffer Overruns (29 fixes)

Unsafe String Function Replacements

Unsafe Safe Replacement Count
sprintf snprintf with size limits 18
strcpy strncpy or memcpy 5
strcat strncat with bounds 6

Files Modified (Phase 3)

  • src/lib_ccx/ccx_encoders_common.c
  • src/lib_ccx/ccx_encoders_sami.c
  • src/lib_ccx/ccx_encoders_smptett.c
  • src/lib_ccx/ccx_encoders_webvtt.c
  • src/lib_ccx/networking.c
  • src/lib_ccx/params.c
  • And more...

Phase 4: Rust FFI Safety (89 fixes)

Safety Documentation

  • Added # Safety docs to all 83 production FFI functions
  • Documented pointer requirements, lifetime constraints, and caller responsibilities

Panic Prevention (FFI function bodies)

  • hardsubx/decoder.rs: Replaced 8 .try_into().unwrap() with safe as casts
  • libccxr_exports/net.rs: Replaced expect() with safe error handling
  • libccxr_exports/mod.rs: Removed panic!/expect(), use defaults
  • libccxr_exports/time.rs: Replaced unwrap() with unwrap_or()

Clippy Fixes

  • Fixed 72 Clippy warnings across the Rust codebase
  • Replaced assert!(false) with unreachable!()
  • Added #[allow] attributes for acceptable test code patterns

Files Modified (Phase 4)

  • src/rust/src/lib.rs
  • src/rust/src/decoder/encoding.rs
  • src/rust/src/decoder/service_decoder.rs
  • src/rust/src/hardsubx/decoder.rs
  • src/rust/src/hardsubx/imgops.rs
  • src/rust/src/hardsubx/utility.rs
  • src/rust/src/libccxr_exports/*.rs
  • And more...

Test Plan

  • All 269 Rust tests pass
  • Clippy reports 0 warnings
  • C code compiles without warnings
  • No unwrap()/expect() calls in FFI function bodies
  • All FFI functions have safety documentation

Statistics

Phase Issues Found Issues Fixed
Phase 1 (Analysis) 1317 N/A
Phase 2 (Memory) 78 78
Phase 3 (Buffer) 29 29
Phase 4 (Rust FFI) 89 89
Total 196 196

This completes Phases 2-4 of the comprehensive bug analysis cycle:

  • Phase 1: Static Analysis (completed - identified issues)
  • Phase 2: Memory Issues (this PR)
  • Phase 3: Buffer Overruns (this PR)
  • Phase 4: Rust FFI Safety (this PR)
  • Phase 5: Runtime Validation (pending)

🤖 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/1828 **Author:** [@cfsmp3](https://github.com/cfsmp3) **Created:** 12/15/2025 **Status:** ✅ Merged **Merged:** 12/15/2025 **Merged by:** [@cfsmp3](https://github.com/cfsmp3) **Base:** `master` ← **Head:** `fix/memory-issues-batch-2.1` --- ### 📝 Commits (10+) - [`95f6f09`](https://github.com/CCExtractor/ccextractor/commit/95f6f096598edd3dc40d687269f29d5574d4d881) fix(memory): Fix memory leaks in ocr.c and ts_tables_epg.c - [`390c96f`](https://github.com/CCExtractor/ccextractor/commit/390c96f00d2edc44f4c250be067af96ce9fd42c9) fix(memory): Fix memory leaks and unsafe realloc patterns in multiple files - [`3b0a63d`](https://github.com/CCExtractor/ccextractor/commit/3b0a63d9c6c489c85757a3d9137b1b51fee8115a) fix(memory): Fix memory leaks and unsafe realloc patterns in lib_ccx, utility, avc_functions - [`ea4f884`](https://github.com/CCExtractor/ccextractor/commit/ea4f884b9d405d2d610fad70d2d056eb94b44159) fix(memory): Fix unsafe realloc patterns in asf_functions, telxcc, and ccx_encoders_srt - [`5b286c5`](https://github.com/CCExtractor/ccextractor/commit/5b286c5b8db150493fce57a907c8eb16dc1cef74) fix(memory): Fix potential memory leaks in encoder files - [`494b14b`](https://github.com/CCExtractor/ccextractor/commit/494b14b6515b4d2cfa3b8c58abccebfbc151686a) fix(memory): Fix memory issues in helpers, splitbysentence, and output - [`90519e2`](https://github.com/CCExtractor/ccextractor/commit/90519e22965b3c908c2e1452d39611313aa6cdc8) fix(memory): Fix memory issues in final batch of files (Batch 2.7) - [`af3ab5a`](https://github.com/CCExtractor/ccextractor/commit/af3ab5acd4ed7db147548312825387d143a5a4dd) fix(buffer): Replace unsafe string functions with safe alternatives - [`d8048bc`](https://github.com/CCExtractor/ccextractor/commit/d8048bc95a2b620fc7ad2ca2f97a0c750ebc9ecc) fix(rust): Complete Phase 4 - FFI safety and documentation - [`d202a66`](https://github.com/CCExtractor/ccextractor/commit/d202a66fd018367c75e8277749ee512589ebdef9) style(rust): Apply cargo fmt formatting ### 📊 Changes **43 files changed** (+608 additions, -275 deletions) <details> <summary>View changed files</summary> 📝 `src/lib_ccx/asf_functions.c` (+12 -4) 📝 `src/lib_ccx/avc_functions.c` (+12 -4) 📝 `src/lib_ccx/ccx_decoders_608.c` (+6 -4) 📝 `src/lib_ccx/ccx_encoders_helpers.c` (+9 -1) 📝 `src/lib_ccx/ccx_encoders_smptett.c` (+7 -3) 📝 `src/lib_ccx/ccx_encoders_splitbysentence.c` (+22 -5) 📝 `src/lib_ccx/ccx_encoders_spupng.c` (+33 -14) 📝 `src/lib_ccx/ccx_encoders_srt.c` (+7 -2) 📝 `src/lib_ccx/ccx_encoders_ssa.c` (+7 -2) 📝 `src/lib_ccx/ccx_encoders_webvtt.c` (+12 -4) 📝 `src/lib_ccx/ccx_gxf.c` (+3 -2) 📝 `src/lib_ccx/dvb_subtitle_decoder.c` (+3 -0) 📝 `src/lib_ccx/dvd_subtitle_decoder.c` (+30 -1) 📝 `src/lib_ccx/ffmpeg_intgr.c` (+29 -6) 📝 `src/lib_ccx/general_loop.c` (+7 -2) 📝 `src/lib_ccx/hardsubx.c` (+19 -0) 📝 `src/lib_ccx/lib_ccx.c` (+7 -0) 📝 `src/lib_ccx/myth.c` (+7 -3) 📝 `src/lib_ccx/ocr.c` (+48 -16) 📝 `src/lib_ccx/output.c` (+7 -2) _...and 23 more files_ </details> ### 📄 Description ## Summary This PR contains comprehensive bug fixes from Phases 2-4 of the bug analysis cycle, addressing memory issues, buffer overruns, and Rust FFI safety across the entire codebase. --- ## Phase 2: Memory Issues (78 fixes) ### Memory Leak Fixes - **ocr.c, ts_tables_epg.c**: Fixed memory leaks in OCR and EPG table handling - **lib_ccx files**: Fixed leaks in utility, avc_functions, and core library functions - **Encoder files**: Fixed potential memory leaks in encoder subsystem - **helpers, splitbysentence, output**: Fixed various memory management issues ### Unsafe Realloc Pattern Fixes - **asf_functions.c, telxcc.c, ccx_encoders_srt.c**: Replaced unsafe `ptr = realloc(ptr, ...)` patterns - **Multiple files**: Added proper NULL checks after realloc to prevent memory corruption ### Files Modified (Phase 2) - `src/lib_ccx/ocr.c` - `src/lib_ccx/ts_tables_epg.c` - `src/lib_ccx/utility.c` - `src/lib_ccx/avc_functions.c` - `src/lib_ccx/lib_ccx.c` - `src/lib_ccx/asf_functions.c` - `src/lib_ccx/telxcc.c` - `src/lib_ccx/ccx_encoders_srt.c` - `src/lib_ccx/ccx_encoders_helpers.c` - `src/lib_ccx/ccx_encoders_splitbysentence.c` - `src/lib_ccx/output.c` - And more... --- ## Phase 3: Buffer Overruns (29 fixes) ### Unsafe String Function Replacements | Unsafe | Safe Replacement | Count | |--------|------------------|-------| | `sprintf` | `snprintf` with size limits | 18 | | `strcpy` | `strncpy` or `memcpy` | 5 | | `strcat` | `strncat` with bounds | 6 | ### Files Modified (Phase 3) - `src/lib_ccx/ccx_encoders_common.c` - `src/lib_ccx/ccx_encoders_sami.c` - `src/lib_ccx/ccx_encoders_smptett.c` - `src/lib_ccx/ccx_encoders_webvtt.c` - `src/lib_ccx/networking.c` - `src/lib_ccx/params.c` - And more... --- ## Phase 4: Rust FFI Safety (89 fixes) ### Safety Documentation - Added `# Safety` docs to all **83 production FFI functions** - Documented pointer requirements, lifetime constraints, and caller responsibilities ### Panic Prevention (FFI function bodies) - **hardsubx/decoder.rs**: Replaced 8 `.try_into().unwrap()` with safe `as` casts - **libccxr_exports/net.rs**: Replaced `expect()` with safe error handling - **libccxr_exports/mod.rs**: Removed `panic!/expect()`, use defaults - **libccxr_exports/time.rs**: Replaced `unwrap()` with `unwrap_or()` ### Clippy Fixes - Fixed 72 Clippy warnings across the Rust codebase - Replaced `assert!(false)` with `unreachable!()` - Added `#[allow]` attributes for acceptable test code patterns ### Files Modified (Phase 4) - `src/rust/src/lib.rs` - `src/rust/src/decoder/encoding.rs` - `src/rust/src/decoder/service_decoder.rs` - `src/rust/src/hardsubx/decoder.rs` - `src/rust/src/hardsubx/imgops.rs` - `src/rust/src/hardsubx/utility.rs` - `src/rust/src/libccxr_exports/*.rs` - And more... --- ## Test Plan - [x] All 269 Rust tests pass - [x] Clippy reports 0 warnings - [x] C code compiles without warnings - [x] No `unwrap()`/`expect()` calls in FFI function bodies - [x] All FFI functions have safety documentation --- ## Statistics | Phase | Issues Found | Issues Fixed | |-------|--------------|--------------| | Phase 1 (Analysis) | 1317 | N/A | | Phase 2 (Memory) | 78 | 78 | | Phase 3 (Buffer) | 29 | 29 | | Phase 4 (Rust FFI) | 89 | 89 | | **Total** | **196** | **196** | --- ## Related This completes Phases 2-4 of the comprehensive bug analysis cycle: - Phase 1: Static Analysis (completed - identified issues) - Phase 2: Memory Issues (this PR) - Phase 3: Buffer Overruns (this PR) - Phase 4: Rust FFI Safety (this PR) - Phase 5: Runtime Validation (pending) 🤖 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:22:54 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/ccextractor#2580