Skip to content

fix undefined float-to-integer cast at the numeric limit boundary#1162

Open
aysha-afrah26 wants to merge 1 commit into
BehaviorTree:masterfrom
aysha-afrah26:double-to-int-boundary-ub
Open

fix undefined float-to-integer cast at the numeric limit boundary#1162
aysha-afrah26 wants to merge 1 commit into
BehaviorTree:masterfrom
aysha-afrah26:double-to-int-boundary-ub

Conversation

@aysha-afrah26

Copy link
Copy Markdown

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

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.

1 participant