fix(hardsubx): Fix heap corruption from Rust/C allocator mismatch

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 <noreply@anthropic.com>
This commit is contained in:
Carlos Fernandez
2025-12-19 05:29:04 +01:00
parent 80a117e643
commit 80957d645b
4 changed files with 18 additions and 7 deletions

View File

@@ -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;

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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);