Skip to content

fix: prevent stream detection from corrupting current_file index#2209

Merged
cfsmp3 merged 1 commit intomasterfrom
fix/stream-detect-file-switch
Mar 18, 2026
Merged

fix: prevent stream detection from corrupting current_file index#2209
cfsmp3 merged 1 commit intomasterfrom
fix/stream-detect-file-switch

Conversation

@cfsmp3
Copy link
Contributor

@cfsmp3 cfsmp3 commented Mar 18, 2026

Summary

  • Root-cause fix for the MKV demuxer crash reported in fix: MKV demuxer crash due to NULL file path #2206
  • detect_stream_type() reads 1 MB via buffered_read_opt() for format detection. For files smaller than 1 MB, hitting EOF triggers switch_to_next_file() (because binary_concat defaults to enabled), which increments current_file past the valid range and closes the file descriptor
  • Format-specific handlers (matroska_loop, processmp4, etc.) then crash accessing inputfile[current_file]
  • Fix: temporarily disable binary_concat around detect_stream_type() so EOF during detection never triggers file switching

Affected code path

ccx_demuxer_open()detect_stream_type()buffered_read_opt() → EOF → switch_to_next_file()current_file++ beyond valid range

Reproduction

Any MKV (or other format) file under 1 MB:

ffmpeg -f lavfi -i "color=c=black:s=320x240:d=0.5" -c:v libx264 -y /tmp/tiny.mkv
ccextractor /tmp/tiny.mkv
# Before fix: "Error: Could not open MKV file: (null)"
# After fix:  processes normally, reports "No captions were found"

Test plan

  • Tiny MKV (2 KB) — previously crashed, now works
  • Large MKV (1.2 GB) — still works, no regression
  • Tiny TS (7 KB) — detected correctly, no crash

🤖 Generated with Claude Code

detect_stream_type() reads up to 1MB (STARTBYTESLENGTH) via
buffered_read_opt() for format detection. For input files smaller
than 1MB, the read hits EOF and—because binary_concat defaults to
enabled—buffered_read_opt() calls switch_to_next_file(). This
increments current_file past the valid range and closes the file
descriptor, leaving format-specific handlers (matroska_loop, MP4,
etc.) to crash when they access inputfile[current_file].

Fix: temporarily disable binary_concat around detect_stream_type()
so that hitting EOF during detection never triggers file switching.

Fixes the root cause of the crash reported in PR #2206 (which
proposed a band-aid of using current_file-1).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@cfsmp3 cfsmp3 merged commit 0b1a967 into master Mar 18, 2026
42 of 44 checks passed
@cfsmp3 cfsmp3 deleted the fix/stream-detect-file-switch branch March 18, 2026 03:12
@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on linux. Below is a summary of the test results, when compared to test for commit 92389cf...:
Report Name Tests Passed
Broken 10/13
CEA-708 1/14
DVB 4/7
DVD 3/3
DVR-MS 2/2
General 27/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 79/86
Teletext 20/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...

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:

  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 8e8229b88b..., Last passed: Never
  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2..., Last passed: Never
  • ccextractor --autoprogram --out=srt --latin1 b22260d065..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 7aad20907e..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65..., Last passed: Never
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b..., Last passed: Never
  • ccextractor --out=spupng c83f765c66..., Last passed: Never
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --xds --latin1 --ucla 85058ad37e..., Last passed: Never
  • ccextractor --autoprogram --out=srt --latin1 --ucla b22260d065..., Last passed: Never

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.

@ccextractor-bot
Copy link
Collaborator

CCExtractor CI platform finished running the test files on windows. Below is a summary of the test results, when compared to test for commit 52b5385...:
Report Name Tests Passed
Broken 10/13
CEA-708 1/14
DVB 3/7
DVD 3/3
DVR-MS 2/2
General 25/27
Hardsubx 1/1
Hauppage 3/3
MP4 3/3
NoCC 10/10
Options 75/86
Teletext 20/21
WTV 13/13
XDS 34/34

Your PR breaks these cases:

  • ccextractor --autoprogram --out=srt --latin1 --quant 0 85271be4d2...
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla dab1c1bd65...
  • ccextractor --out=srt --latin1 --autoprogram 29e5ffd34b...
  • ccextractor --out=spupng c83f765c66...
  • ccextractor --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotbefore 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsnotafter 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatleast 1 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...
  • ccextractor --startcreditsforatmost 2 --startcreditstext "CCextractor Start crdit Testing" c4dd893cb9...

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:

  • ccextractor --autoprogram --out=ttxt --latin1 --ucla --xds 8e8229b88b..., Last passed: Never
  • ccextractor --autoprogram --out=srt --latin1 b22260d065..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --latin1 --ucla 7aad20907e..., Last passed: Never
  • ccextractor --autoprogram --out=ttxt --xds --latin1 --ucla 85058ad37e..., Last passed: Never
  • ccextractor --autoprogram --out=srt --latin1 --ucla b22260d065..., Last passed: Never

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.

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