Assert the avifROStream offset invariant - #3349
Closed
WAM41 wants to merge 1 commit into
Closed
Conversation
include/avif/internal.h documents that functions operating on an avifROStream must maintain stream->offset <= stream->raw->size. avifROStreamHasBytesLeft() and avifROStreamRemainingBytes() both rely on that invariant to compute a size_t difference that would otherwise wrap. Assert it at those two places so a violation aborts under assertions instead of becoming an out-of-bounds read. Release builds are unaffected. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
include/avif/internal.hdocuments that functions operating on anavifROStreammust maintainstream->offset <= stream->raw->size.Two functions rely on that invariant to compute a
size_tdifference:If the invariant were broken, both subtractions would wrap around, so the comparison in
avifROStreamHasBytesLeft()would succeed for byte counts that must be rejected, and thememcpy()inavifROStreamRead()would read past the end of the buffer.The invariant does hold today, and every path that moves an
avifROStreamoffset preserves it:avifROStreamSetOffset()clamps toraw->size;avifROStreamSkip()andavifROStreamRead()advance only afteravifROStreamHasBytesLeft()has succeeded;avifROStreamReadString()advances by at most the number of remaining bytes, because it first proves a null terminator exists within them; and the bit readers go throughavifROStreamSkip()rather than touchingoffset. Nothing outsidesrc/stream.cwrites anavifROStreamoffset.But nothing checks the invariant, so a future change that broke any of those paths would surface as an out-of-bounds read rather than as a diagnosable failure. This adds an
assert()at the two places that depend on it, consistent with the existing use ofassert()elsewhere insrc/stream.c. Release builds are unaffected, sinceassert()is compiled out underNDEBUG.Tested with dav1d 1.4.1 and libaom 3.8.2 on Ubuntu 24.04, GCC 13.3,
CMAKE_BUILD_TYPE=Debug: all 51 ctest targets pass. Inverting the condition inavifROStreamHasBytesLeft()instead aborts 70 test subprocesses, and inverting it inavifROStreamRemainingBytes()aborts 62, so both functions are reached by the suite and the assertions are compiled in.🤖 Generated with Claude Code