Skip to content

Fix segfaults on streams without a codec context#2346

Merged
WyattBlue merged 1 commit into
mainfrom
patch
Jul 21, 2026
Merged

Fix segfaults on streams without a codec context#2346
WyattBlue merged 1 commit into
mainfrom
patch

Conversation

@WyattBlue

Copy link
Copy Markdown
Member

Stream.codec_context is cdef-typed, so with Cython's default nonecheck=False, calling into it while None causes a segfault. A stream has no codec context when avcodec_find_decoder comes up empty on demux, or when it was created by add_mux_stream.

Decoding and encoding such a stream now raise DecoderNotFoundError and EncoderNotFoundError through a shared guard. BitStreamFilterContext instead skips the dereference entirely: it only mirrored codecpar into the context, and avcodec_parameters_copy has already given the muxer what it needs, so add_mux_stream with h264_mp4toannexb now works rather than crashing.

Closes #2344.

`Stream.codec_context` is cdef-typed, so with Cython's default
nonecheck=False, calling into it while None causes a segfault. A stream
has no codec context when avcodec_find_decoder comes up empty on demux,
or when it was created by `add_mux_stream`.

Decoding and encoding such a stream now raise DecoderNotFoundError and
EncoderNotFoundError through a shared guard. `BitStreamFilterContext`
instead skips the dereference entirely: it only mirrored codecpar into
the context, and avcodec_parameters_copy has already given the muxer
what it needs, so `add_mux_stream` with h264_mp4toannexb now works
rather than crashing.

Closes #2344.
@WyattBlue
WyattBlue merged commit 97e964e into main Jul 21, 2026
8 checks passed
@WyattBlue
WyattBlue deleted the patch branch July 21, 2026 05:02
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.

Decoding a stream with no codec context (truncated bitstream) crashes the process (SIGSEGV/SIGBUS) instead of raising

1 participant