fix undefined float-to-integer cast at the numeric limit boundary#1162
Open
aysha-afrah26 wants to merge 1 commit into
Open
fix undefined float-to-integer cast at the numeric limit boundary#1162aysha-afrah26 wants to merge 1 commit into
aysha-afrah26 wants to merge 1 commit into
Conversation
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 safe-cast helpers in convert_impl.hpp and safe_any.hpp gate a floating point to integer conversion with
from > static_cast<From>(numeric_limits<To>::max()), and if that passes they static_cast the value to the integer type. The trouble is that when the target has more significant bits than the source mantissa, like double to int64_t or float to int32_t,static_cast<From>(To::max)rounds up to a value that is one past the real maximum: for int64_t it becomes 2^63, which is not representable in an int64_t. A double that lands exactly on 2^63 therefore slips past the>check and reachesstatic_cast<int64_t>(2^63), which is signed-overflow undefined behavior. I hit it with UBSan while poking at numeric ports, since this path is reachable from a script bitwise operator on a double and from Blackboard::set on a port already locked to int64_t (isCastingSafe/ValidCast). The fix keeps the same limits but switches the upper bound to a half-open comparison when static_cast(To::max) rounds up, decided at compile time from the digit counts, so the boundary value is rejected before the cast instead of after. Smaller targets like int32_t whose max is exactly representable keep the inclusive>and still accept their true maximum. I applied the same change at both sites and added a regression test that trips UBSan before the fix and passes after.