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

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

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

State: closed
Merged: Yes


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

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

No dependencies set.

Reference: starred/ccextractor#2585