mirror of
https://github.com/CCExtractor/ccextractor.git
synced 2026-07-08 18:06:30 +00:00
fix(memory): Fix XDS memory leaks in encoder and decoder cleanup
- XDS encoder leak: Free xds_str when skipping subtitles with invalid timestamps - XDS decoder cleanup: Add proper cleanup for leftover XDS strings in dinit_cc_decode() - Remove incorrect free(p) after write_xds_string() - the pointer is stored for later use by the encoder and must not be freed immediately - Remove xds_ctx free from dinit_cc_decode() to avoid double-free These fixes address the 100-byte XDS leak found in Valgrind test 114. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -241,7 +241,21 @@ void dinit_cc_decode(struct lib_cc_decode **ctx)
|
||||
freep(&bitmap->data1);
|
||||
}
|
||||
}
|
||||
/* Free any leftover XDS strings that weren't processed by the encoder */
|
||||
if (lctx->dec_sub.type == CC_608 && lctx->dec_sub.data)
|
||||
{
|
||||
struct eia608_screen *data = (struct eia608_screen *)lctx->dec_sub.data;
|
||||
for (int i = 0; i < lctx->dec_sub.nb_data; i++, data++)
|
||||
{
|
||||
if (data->format == SFORMAT_XDS && data->xds_str)
|
||||
{
|
||||
freep(&data->xds_str);
|
||||
}
|
||||
}
|
||||
}
|
||||
freep(&lctx->dec_sub.data);
|
||||
/* Note: xds_ctx is freed in general_loop.c, mp4.c etc. during normal processing.
|
||||
Don't free it here as it may cause double-free if already freed elsewhere. */
|
||||
freep(ctx);
|
||||
}
|
||||
|
||||
|
||||
@@ -175,6 +175,8 @@ void xdsprint(struct cc_subtitle *sub, struct ccx_decoders_xds_context *ctx, con
|
||||
if (n > -1 && n < size)
|
||||
{
|
||||
write_xds_string(sub, ctx, p, n);
|
||||
/* Note: Don't free(p) here - the pointer is stored in data->xds_str
|
||||
and will be freed by the encoder or decoder cleanup code */
|
||||
return;
|
||||
}
|
||||
/* Else try again with more space. */
|
||||
|
||||
@@ -924,6 +924,11 @@ int encode_sub(struct encoder_ctx *context, struct cc_subtitle *sub)
|
||||
// After adding delay, if start/end time is lower than 0, then continue with the next subtitle
|
||||
if (data->start_time < 0 || data->end_time <= 0)
|
||||
{
|
||||
// Free XDS string if skipping to avoid memory leak
|
||||
if (data->format == SFORMAT_XDS && data->xds_str)
|
||||
{
|
||||
freep(&data->xds_str);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -570,10 +570,8 @@ int write_image(struct pixel_t *buffer, FILE *fp, int width, int height)
|
||||
png_write_end(png_ptr, NULL);
|
||||
|
||||
finalise:
|
||||
if (info_ptr != NULL)
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
if (png_ptr != NULL)
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||
if (row != NULL)
|
||||
free(row);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user