Fix signed integer overflow UB in BSNumber and BitHacks for INT_MIN#122
Open
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
Open
Fix signed integer overflow UB in BSNumber and BitHacks for INT_MIN#122CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
CodeReclaimers wants to merge 1 commit intodavideberly:masterfrom
Conversation
…alues BSNumber(int32_t) and BSNumber(int64_t) negated negative inputs via `number = -number`, which is undefined behavior for INT32_MIN/INT64_MIN. Fix: use unsigned arithmetic for the magnitude. BitHacks::GetTrailingBit(int32_t) used `value & -value` which also overflows for INT32_MIN, and the uint32_t overload delegated to it. Fix: reverse delegation so uint32_t does the work using ~value + 1u. Co-Authored-By: Claude Opus 4.6 <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.
BSNumber(int32_t)andBSNumber(int64_t)negate negative inputs vianumber = -number, which is undefined behavior forINT32_MIN/INT64_MIN. Fix: compute the magnitude in unsigned arithmetic.BitHacks::GetTrailingBit(int32_t)has the same issue withvalue & -valueforINT32_MIN, and theuint32_toverload delegated to it. Fix: reverse the delegation souint32_tdoes the work using~value + 1u, andint32_tdelegates touint32_t.Test: build with
-fsanitize=undefinedand constructBSNumberfromINT32_MINandINT64_MIN.Test (CMakeLists.txt)
Test (test_issue_8_2.cpp)