fix: resolve dangling pointer in switch_output_file#2286
Conversation
Free old filename/fh only after new file is successfully opened. Uses prepare-then-swap pattern to avoid partial state where encoder has no valid output. 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. |
|
Thanks for the PR — this was reviewed in depth, including building it and testing against a dual-AVC-track MP4 (outputs byte-identical to master on the reachable path, valgrind clean). Of the three PRs filed for #2273, yours had the best approach: prepare-then-swap is the right structure, it never leaves a transient bad state, and it degrades without losing data. However, we're closing all PRs for #2273: the bug is only reachable on malloc failure. For future reference, two style points from the review: the project requires braces on all |
Free old filename/fh only after new file is successfully opened. Uses prepare-then-swap pattern to avoid partial state where encoder has no valid output. Fixes #2273
In raising this pull request, I confirm the following (please check boxes):
Reason for this PR:
Sanity check:
Repro instructions:
This is essential. We will not merge ANY PR that doesn't come with detailed instructions, including a sample. We don't want
"fixes" for theoretical issues that an AI agent found, without context. If you can't reproduce the bug, don't send a PR.
Creating PRs with AI is very quick, but we still have humans (even if AI assisted) going over each.
Be mindful of reviewers' time.
Fixes #2273
Problem:
In switch_output_file, enc_ctx->out->filename was freed before confirming a new file could be successfully opened. If get_basename() or open() failed, the function would proceed with a dangling pointer being passed to write_subtitle_file_header(), causing undefined behavior.
Fix:
Uses a prepare-then-swap approach — the new filename and file descriptor are fully prepared first. The old values are only freed and replaced after the new file is confirmed open. If any step fails, the function returns early leaving the encoder context in its original valid state.
Testing:
Tested on multiprogram_spain.ts (multiprogram transport stream with 7 programs) — processed all programs without crashing and produced expected output.