Skip to content

Backport some C89 compatible comments from libretro-common. - #4

Merged
rtissera merged 1 commit into
rtissera:masterfrom
orbea:c89
Jan 5, 2018
Merged

Backport some C89 compatible comments from libretro-common.#4
rtissera merged 1 commit into
rtissera:masterfrom
orbea:c89

Conversation

@orbea

@orbea orbea commented Jan 5, 2018

Copy link
Copy Markdown
Contributor

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...

@rtissera
rtissera merged commit 01f3bfa into rtissera:master Jan 5, 2018
@orbea
orbea deleted the c89 branch January 5, 2018 17:32
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
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