Skip to content

[FIX] --videoedited: stop the double free and NULL copy on the second input - #2503

Open
cfsmp3 wants to merge 1 commit into
masterfrom
fix/videoedited-multi-ts
Open

cfsmp3 wants to merge 1 commit into
masterfrom
fix/videoedited-multi-ts

Conversation

@cfsmp3

@cfsmp3 cfsmp3 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

In raising this pull request, I confirm the following (please check boxes):

Reason for this PR:

  • This PR adds new functionality.
  • This PR fixes a bug that I have personally experienced or that a real user has reported and for which a sample exists.
  • This PR is porting code from C to Rust.

Sanity check:

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • If the PR adds new functionality, I've added it to the changelog. If it's just a bug fix, I have NOT added it to the changelog.
  • I am NOT adding new C code unless it's to fix an existing, reproducible bug.

Repro instructions:

ccextractor 01509e4d27bd….ts c4dd893cb9d6….ts --videoedited -o out.srt
ccextractor ab9cf8cf….mpg 725a49f8….mpg --videoedited -o out.srt

On master, both abort with free(): double free detected in tcache 2. 0.96.5 (snap) segfaults on the first. Under ASan, passing the same TS file twice shows a second crash: a SEGV in memmove from copy_payload_to_capbuf() (ts_functions.c:715).

Problem

--videoedited runs general_loop() once per input. The decoders and the demuxer's cap_info streams are kept for the next input, but two pieces of per-file cleanup leave stale state behind:

  1. general_loop() frees dec_ctx->xds_ctx without clearing the pointer. The next input decodes 608 with the freed XDS context, then frees it a second time.
  2. cinfo_cremation() frees each cap_info's capbuf but keeps capbufsize (and capbuflen). For the next input, copy_payload_to_capbuf() sees enough capacity, skips the realloc, and copies the first PES payload into a NULL buffer.

The default mode (binary concat) calls both only once, at the very end, which is why it is unaffected.

Fix

  • free() → freep() for xds_ctx. rcwt_loop() already does this, for the same reason. The XDS functions return early on a NULL context.
  • Reset capbufsize and capbuflen after freeing capbuf.

Verification

  • Repro (ASan): none of these crash any more:

    • TS: 01509e4d + c4dd893c, c4dd893c + 01509e4d, c4dd893c ×2, 01509e4d ×2
    • PS: ab9cf8cf ×2, ab9cf8cf + 725a49f8

    The remaining leaks match a master single-file run (32 bytes) for the PS pairs. The TS pairs leak 96–112 bytes, in copy_demuxer_from_rust_to_c() and init_encoder().

  • PS output: ab9cf8cf + 725a49f8 gives 33 cues (10 + 23). The text is identical to the two separate runs.

  • Release sweep over 45 local samples (.ts, .mpg, .vob, .wtv, .dvr-ms): all 81 output files are byte-identical to master.

  • Default concat, without --videoedited: 01509e4d + c4dd893c and ab9cf8cf + 725a49f8 are byte-identical to master.

  • Formatting: clang-format is clean.

Not fixed here

With the crashes gone, --videoedited output still has timing problems. These are separate defects, present wherever master didn't crash:

  • No flush at the end of each input. The first input's last caption isn't closed at its end of file. It stays open until the second input's first caption: ab9cf8cf cue 10 ends at 1:00.725 instead of 0:29.928. On TS the 608 screen also carries over, so c4dd893c ×2 gives 3 cues instead of 2.
  • The second input's timestamps are about 29.5 s late. fts_global correctly advances by the first input's length (29929 ms). But the timing context keeps the first input's PTS state, so the second input's PTS restart is treated as a jump and also added to fts_offset.
  • The second input has no XDS in general_loop() formats, because the context is freed after the first. rcwt_loop() re-creates it; general_loop() doesn't.
  • --out=report with MPEG-2 video also needs [FIX] Accept a NULL encoder in the MPEG-2 ES parser (report mode) #2502 in debug builds.

--videoedited runs general_loop() once per input and keeps the
decoders and cap_info streams between inputs. Two pieces of per-file
cleanup left stale state behind for the next input:

- general_loop() freed dec_ctx->xds_ctx without clearing it. The next
  input used the freed context and freed it again (glibc aborts with
  "double free detected").
- cinfo_cremation() freed each cap_info's capbuf but kept capbufsize.
  The next input skipped the realloc and copied its first PES payload
  to a NULL buffer.

Clear the pointer and reset the buffer sizes. Any two TS or PS inputs
crashed with --videoedited; 0.96.5 segfaults the same way.
@ccextractor-bot

Copy link
Copy Markdown
Collaborator
CCExtractor CI platform finished running the test files on linux. 171/238 tests matched the approved output:
Report Name Tests Passed
Broken 9/13
CEA-708 2/14
DVB 0/7
DVD 3/3
DVR-MS 2/2
General 26/28
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 68/86
Teletext 0/21
WTV 12/13
XDS 32/34

67 tests do not match the approved output. That is the pass/fail verdict. Whether this branch caused it is a separate question, answered below.


Compared with the tip of master — test 10168, commit 94615d4:

  • 0 pass there and fail here
  • 0 fail there and pass here
  • 0 fail on both, with different output
  • 67 fail on both, byte for byte the same

Compared with the commit this branch was cut from: the same run as the tip of master (test 10168), so the comparison above already covers it.


No test changes behaviour relative to the tip of master: every failure above fails there too, byte for byte. The approved output for those tests is out of date, which is a baseline to review rather than a regression in this branch.

@ccextractor-bot

Copy link
Copy Markdown
Collaborator
CCExtractor CI platform finished running the test files on windows. 171/238 tests matched the approved output:
Report Name Tests Passed
Broken 9/13
CEA-708 2/14
DVB 0/7
DVD 3/3
DVR-MS 2/2
General 26/28
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 68/86
Teletext 0/21
WTV 12/13
XDS 32/34

67 tests do not match the approved output. That is the pass/fail verdict. Whether this branch caused it is a separate question, answered below.


Compared with the tip of master — test 10177, commit e708d72:

  • 0 pass there and fail here
  • 1 fail there and pass here
  • 0 fail on both, with different output
  • 67 fail on both, byte for byte the same

Fail there, pass here:

  • ccextractor --autoprogram --out=ttxt --xds --latin1 --ucla e274a73653...

Compared with the commit this branch was cut from: the same run as the tip of master (test 10177), so the comparison above already covers it.


No test changes behaviour relative to the tip of master: every failure above fails there too, byte for byte. The approved output for those tests is out of date, which is a baseline to review rather than a regression in this branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants