Repository navigation
Fix segfaults and use-after-frees - #2476
Merged
Merged
Conversation
- `demux()`/`decode()` resumed after the container closed read from the freed format context; check the container is open on every iteration - A Python file whose `read(n)` returned more than `n` bytes overflowed FFmpeg's I/O buffer; raise `ValueError` instead - `Packet(bytearray)` referenced the bytearray without a buffer export, so resizing it left the packet reading freed memory; only take the no-copy path for immutable `bytes` and `str` - `Dictionary` iteration held an entry pointer that inserts invalidated; snapshot the keys first - `VideoBlockParams` didn't keep its `VideoEncParams` alive - `HWConfig` and the side data wrappers checked their sentinel in `__init__`, so `__new__` left a NULL pointer; check in `__cinit__` Also type `Packet(input)` and `Dictionary(...)` to match what they accept.
`ByteSource` took `len()` of a str, which counts characters, so
`Packet("né")` and `CodecContext.extradata = "né"` dropped the last
byte of any non-ASCII text. Get the pointer and byte length together
from `PyUnicode_AsUTF8AndSize`; the str owns the buffer, and for ASCII
it is the str's own data, so nothing is copied.
WyattBlue
force-pushed
the
segfault-fixes
branch
from
October 25, 2026 02:11
baeba9c to
71f0798
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found by fuzzing the public API in a crash-isolating harness (objects in closed, freed, empty, and
__new__states; ~112k malformed inputs) and auditingmemcpy/pointer sites.Segfaults
demux()/decode()after the container closed calledav_read_frame(NULL, …). Easy to hit: create the generator insidewith av.open(...)and iterate after the block. Now raisesAssertionError: Container is not open.read(n)returned more thannbytes overflowed FFmpeg's I/O buffer. Now raisesValueError.av.dictionary.Dictionarywhile iterating it left the iterator with a dangling entry pointer. Iteration now snapshots the keys.Cls.__new__(Cls)left a NULL pointer onHWConfigand the side data wrappers, because their sentinel check lived in__init__. Moved to__cinit__, so it raisesTypeErrorlike the other wrappers.Use-after-frees
Packet(bytearray)referenced the bytearray without a buffer export, so resizing it left the packet reading freed memory (also a race inCodecContext.parse).bytearraynow goes throughPyObject_GetBuffer; resizing it while the packet lives raisesBufferError, as it already did formemoryviewinput.VideoBlockParamsdidn't keep itsVideoEncParamsalive.Also
strinput toPacket,extradata, andsubtitle_headerdropped bytes of non-ASCII text (len()counted characters, not UTF-8 bytes). Separate commit.Packet(input)acceptsint | str | Buffer | None;Dictionary(...)accepts mappings and keywords, matching runtime.Each regression test fails on the old code (two of them by segfaulting/hanging).