Fix use-after-free in switch_output_file on basename failure#2287
Fix use-after-free in switch_output_file on basename failure#2287amitmishra11 wants to merge 1 commit into
Conversation
If get_basename(enc_ctx->out->original_filename) returns NULL, the function left enc_ctx->out->filename pointing at memory it had just freed a few lines above, then unconditionally passed enc_ctx->out to write_subtitle_file_header(), which dereferences filename downstream (write_subtitle_file_header -> write_spumux_header -> spunpg_init). NULL the pointer immediately after freeing it, and return early (with a warning) when no basename can be derived, instead of falling through to use a context with no valid filename/handle. Fixes CCExtractor#2273.
CCExtractor CI platform finished running the test files on linux. Below is a summary of the test results, when compared to test for commit 2feb09a...:
Your PR breaks these cases:
NOTE: The following tests have been failing on the master branch as well as the PR:
Congratulations: Merging this PR would fix the following tests:
It seems that not all tests were passed completely. This is an indication that the output of some files is not as expected (but might be according to you). Check the result page for more info. |
|
Closing in favor of #2286, which was filed first (2026-06-26) and fixes the same root cause in |
Why
switch_output_file()insrc/lib_ccx/ccx_encoders_common.cfrees the previousenc_ctx->out->filenameunconditionally, then only reassigns it insideif (basename != NULL):If
get_basename()returnsNULL(allocation failure, or aNULL/unusualoriginal_filename),enc_ctx->out->filenameis left pointing at memory that was just freed, andwrite_subtitle_file_header()is called regardless — its call chain (write_subtitle_file_header -> write_spumux_header -> spunpg_init) dereferencesout->filename(strlen(out->filename),memcpy(... out->filename ...)), a use-after-free.Fix
enc_ctx->out->filename = NULL;immediately after freeing it, so the pointer is never left dangling even momentarily.mprintwarning, matching the existing logging convention in this file) whenbasenameisNULL, instead of falling through to use a context with no valid filename/handle — there's nothing sensible to write a header for at that point anyway.Testing
I couldn't get a full build of this project running in my sandbox (no
cmake, and the available MinGW toolchain doesn't have the FFmpeg/GPAC/etc. dev libraries this project links against), so I verified the logic two ways instead of guessing:mastersource matches the bug exactly as described (readswitch_output_fileand tracedwrite_subtitle_file_header -> write_spumux_header -> spunpg_init's use ofout->filename).filename == NULLand never reads the dangling pointer.utility.h) into a standalone file and compiled/ran it through both the normal and basename-failure paths to confirm no syntax issues and unchanged behavior on the happy path across repeated calls.Happy to add this as a real unit test if there's an existing harness/fixture I should hook into - I didn't see one already covering
switch_output_fileorget_basenamefailure paths.Fixes #2273.