From 80957d645bf581721b4e151bd31f33774f4766d9 Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Date: Fri, 19 Dec 2025 05:29:04 +0100 Subject: [PATCH] fix(hardsubx): Fix heap corruption from Rust/C allocator mismatch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hardsubx code was using C's free() on strings allocated by Rust's CString::into_raw(). Since Rust and C use different memory allocators, this caused heap corruption that manifested as garbage OCR output after processing ~27 subtitle frames. Changes: - Export free_rust_c_string() from Rust as extern "C" function - Declare free_rust_c_string() in hardsubx.h for C code - Replace free(subtitle_text) with free_rust_c_string(subtitle_text) in hardsubx_decoder.c for Rust-allocated strings - Fix memory leaks in process_hardsubx_linear_frames_and_normal_subs() where subtitle_text_hard and prev_subtitle_text_hard were not freed - Remove dummy CI trigger file (no longer needed) Testing: - AddressSanitizer: No memory errors detected - Valgrind: 0 bytes definitely lost, 0 bytes indirectly lost - Manual testing: OCR output now correct for entire video duration 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/ci_trigger_dummy.c | 3 --- src/lib_ccx/hardsubx.h | 4 ++++ src/lib_ccx/hardsubx_decoder.c | 15 ++++++++++++--- src/rust/src/utils.rs | 3 ++- 4 files changed, 18 insertions(+), 7 deletions(-) delete mode 100644 src/ci_trigger_dummy.c diff --git a/src/ci_trigger_dummy.c b/src/ci_trigger_dummy.c deleted file mode 100644 index 15ee19ef..00000000 --- a/src/ci_trigger_dummy.c +++ /dev/null @@ -1,3 +0,0 @@ -// Dummy file to trigger Sample Platform CI -// This file is not compiled or linked - safe to delete after triage complete -static int ci_trigger_dummy_unused = 0; diff --git a/src/lib_ccx/hardsubx.h b/src/lib_ccx/hardsubx.h index 45642743..c5da3b49 100644 --- a/src/lib_ccx/hardsubx.h +++ b/src/lib_ccx/hardsubx.h @@ -116,6 +116,10 @@ int64_t convert_pts_to_s(int64_t pts, AVRational time_base); int is_valid_trailing_char(char c); char *prune_string(char *s); +// Rust memory management - strings returned from Rust must be freed with this function +// DO NOT use free() on strings returned from _process_frame_white_basic, _process_frame_color_basic, etc. +void free_rust_c_string(char *ptr); + #endif #endif diff --git a/src/lib_ccx/hardsubx_decoder.c b/src/lib_ccx/hardsubx_decoder.c index 75e94772..ddfa6991 100644 --- a/src/lib_ccx/hardsubx_decoder.c +++ b/src/lib_ccx/hardsubx_decoder.c @@ -210,7 +210,7 @@ void hardsubx_process_frames_linear(struct lib_hardsubx_ctx *ctx, struct encoder if (dist < (0.2 * MIN(strlen(subtitle_text), strlen(prev_subtitle_text)))) { dist = -1; - free(subtitle_text); + free_rust_c_string(subtitle_text); subtitle_text = NULL; prev_end_time = convert_pts_to_ms(ctx->packet.pts, ctx->format_ctx->streams[ctx->video_stream_id]->time_base); } @@ -255,8 +255,8 @@ void hardsubx_process_frames_linear(struct lib_hardsubx_ctx *ctx, struct encoder } prev_packet_pts = ctx->packet.pts; - // Free subtitle_text from this iteration (was allocated by _process_frame_*_basic) - free(subtitle_text); + // Free subtitle_text from this iteration (was allocated by Rust in _process_frame_*_basic) + free_rust_c_string(subtitle_text); subtitle_text = NULL; } } @@ -497,6 +497,7 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har if (dist < (0.2 * MIN(strlen(subtitle_text_hard), strlen(prev_subtitle_text_hard)))) { dist = -1; + free_rust_c_string(subtitle_text_hard); subtitle_text_hard = NULL; prev_end_time_hard = convert_pts_to_ms(hard_ctx->packet.pts, hard_ctx->format_ctx->streams[hard_ctx->video_stream_id]->time_base); } @@ -506,6 +507,7 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har add_cc_sub_text(hard_ctx->dec_sub, prev_subtitle_text_hard, prev_begin_time_hard, prev_end_time_hard, "", "BURN", CCX_ENC_UTF_8); encode_sub(enc_ctx, hard_ctx->dec_sub); prev_begin_time_hard = prev_end_time_hard + 1; + free(prev_subtitle_text_hard); prev_subtitle_text_hard = NULL; prev_sub_encoded_hard = 1; prev_end_time_hard = convert_pts_to_ms(hard_ctx->packet.pts, hard_ctx->format_ctx->streams[hard_ctx->video_stream_id]->time_base); @@ -526,6 +528,10 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har prev_sub_encoded_hard = 0; } prev_packet_pts_hard = hard_ctx->packet.pts; + + // Free subtitle_text_hard from this iteration (allocated by Rust) + free_rust_c_string(subtitle_text_hard); + subtitle_text_hard = NULL; } } } @@ -538,6 +544,9 @@ void process_hardsubx_linear_frames_and_normal_subs(struct lib_hardsubx_ctx *har encode_sub(enc_ctx, hard_ctx->dec_sub); prev_sub_encoded_hard = 1; } + + // Cleanup + free(prev_subtitle_text_hard); activity_progress(100, cur_sec / 60, cur_sec % 60); } diff --git a/src/rust/src/utils.rs b/src/rust/src/utils.rs index 4b531e74..2947612f 100644 --- a/src/rust/src/utils.rs +++ b/src/rust/src/utils.rs @@ -38,7 +38,8 @@ pub fn string_to_c_char(a: &str) -> *mut ::std::os::raw::c_char { /// # Safety /// The pointer must have been allocated by `string_to_c_char` (i.e., `CString::into_raw`) /// or be null. Passing a pointer allocated by C's malloc will cause undefined behavior. -pub unsafe fn free_rust_c_string(ptr: *mut ::std::os::raw::c_char) { +#[no_mangle] +pub unsafe extern "C" fn free_rust_c_string(ptr: *mut ::std::os::raw::c_char) { if !ptr.is_null() { // Reclaim ownership and drop the CString, which frees the memory let _ = ffi::CString::from_raw(ptr);