Backport some C89 compatible comments from libretro-common. - #4
Merged
Conversation
rtissera
added a commit
that referenced
this pull request
Sep 3, 2026
The CHD v5 compressed map header carries three bit widths - lengthbits,
selfbits and parentbits - as raw bytes, and they become the width argument to
bitstream_read() for every map entry. Nothing validated them, so a malformed
file could make bitstream_peek() evaluate
bitstream->buffer >> (32 - numbits)
with numbits above 32, shifting by a negative amount. Found by fuzzing:
libchdr_bitstream.c:62:27: runtime error: shift exponent -36 is negative
#0 bitstream_peek
#1 bitstream_read
#2 build_v5_map_checkpoints
#3 decompress_v5_map
#4 chd_open_core_file_callbacks
chdman derives all three from hunkbytes and the hunk count, so they never
legitimately exceed 32; anything larger means the file is corrupt. Reject it as
CHDERR_INVALID_FILE at parse time, before the value reaches the bitstream.
bitstream_remove() is hardened separately: consuming all 32 bits is a legitimate
request that peek() already serves, but the matching `buffer <<= 32` on a
uint32_t was undefined too.
Well-formed files are unaffected - decoded output is byte-identical over the
CHD corpus. 3281 malformed inputs, generated from all 17 corpus seed codecs
across header, map-region, whole-file and truncation mutations, now run clean
under ASan and UBSan with no hangs, on both 64-bit and 32-bit builds.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMYbZzB8mioFmotWGFnAXG
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.
The next batch of C89 compatible comments backported from libretro-common. Again there is more to come, but its easier to not do it all at once...