Repository navigation
Avoid signed overflow in bs_read_se() - #2071
Merged
cgutman merged 1 commit intoOct 24, 2026
Merged
Conversation
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>
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.
bs_read_ue()can return values aboveINT32_MAX.bs_read_se()stored the result in anint32_tand computed(r+1)/2, which is undefined behavior for code0x7FFFFFFFand gives wrong values aboveINT32_MAX. The SPS parsed here comes from the host during the H.264 SPS fixup. No security impact is known: the affectedse()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 most0xFFFFFFFE, 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=undefinedover 11 codes, including0x7FFFFFFF,0x80000000,0xFFFFFFFDand0xFFFFFFFE, against the H.264 9.1.1 mapping. Before: UBSan abort atbs.h:184. After: all pass. The full app builds.🤖 Generated with Claude Code