Skip to content

Avoid signed overflow in bs_read_se() - #2071

Merged
cgutman merged 1 commit into
moonlight-stream:masterfrom
ccowan93:fix/h264bitstream-se-overflow
Oct 24, 2026
Merged

cgutman merged 1 commit into
moonlight-stream:masterfrom
ccowan93:fix/h264bitstream-se-overflow

Conversation

@ccowan93

Copy link
Copy Markdown
Contributor

bs_read_ue() can return values above INT32_MAX. bs_read_se() stored the result in an int32_t and computed (r+1)/2, which is undefined behavior for code 0x7FFFFFFF and gives wrong values above INT32_MAX. The SPS parsed here comes from the host during the H.264 SPS fixup. No security impact is known: the affected se() fields are only re-serialized. This is hardening next to the recent h264bitstream clamps.

The conversion is now done in unsigned arithmetic. bs_read_ue() returns at most 0xFFFFFFFE, so both branches produce in-range values, and every previously well-defined code decodes to the same value.

Tested: a unit test of bs_read_se() built with -fsanitize=undefined over 11 codes, including 0x7FFFFFFF, 0x80000000, 0xFFFFFFFD and 0xFFFFFFFE, against the H.264 9.1.1 mapping. Before: UBSan abort at bs.h:184. After: all pass. The full app builds.

🤖 Generated with Claude Code

bs_read_ue() can return values above INT32_MAX. bs_read_se() stored the
result in an int32_t and computed (r+1)/2, which is undefined behavior
when the code is 0x7FFFFFFF and gives wrong values for codes above
INT32_MAX. The SPS being parsed comes from the host during the H.264
SPS fixup.

Do the conversion in unsigned arithmetic. bs_read_ue() returns at most
0xFFFFFFFE, so both branches produce in-range int32_t values, and every
code that was previously well-defined decodes to the same value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cgutman
cgutman merged commit c8474c5 into moonlight-stream:master Oct 24, 2026
5 checks passed
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