Repository navigation
fix undefined double to int64 cast in the script complement operator - #1239
Open
aysha-afrah26 wants to merge 1 commit into
Open
aysha-afrah26 wants to merge 1 commit into
aysha-afrah26 wants to merge 1 commit into
Conversation
The ~ operator in ExprUnaryArithmetic::evaluate guarded the cast of its double operand to int64_t with a hand-written range comparison. NaN passes it (every comparison is false) and so does exactly 2^63, because double(INT64_MAX) rounds up to 2^63; the following static_cast is then undefined behavior (UBSan float-cast-overflow). Fractional operands were also truncated silently, while the binary bitwise operators reject them. Use the checked Any::cast<int64_t> the binary bitwise operators already use, so these operands raise the existing RuntimeError instead.
This branch has not been deployed
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 scripting operator
~is evaluated inExprUnaryArithmetic::evaluate(operators.hpp) by casting the operand todoubleand then toint64_tbehind a hand-written range check,rv > double(INT64_MAX) || rv < double(INT64_MIN). That check lets NaN through, since every comparison with NaN is false, and it lets exactly 2^63 through, sincedouble(INT64_MAX)rounds up to 2^63 and 2^63 is not greater than itself; both reachstatic_cast<int64_t>, which is undefined for values outside the range, and it also truncates fractional operands silently, so~1.5evaluates as~1. The binary bitwise operators a few lines below go through the checkedAny::cast<int64_t>instead and reject a non-integer, so the complement is the one remaining hand-rolled cast in the evaluator (the shared helpers it now relies on were tightened at the boundary in #1162). The operand is untrusted script text fromScriptnodes and_skipIf/_while/_onSuccessattributes, or a blackboard double:~(0.0 / 0.0)and~9223372036854775808.0both trigger UBSanfloat-cast-overflowat operators.hpp:137 in the ASan+UBSan configuration the CI runs. The fix routes the complement through the same checked cast the binary operators use, so NaN, infinities, out-of-range and fractional values raise the existingRuntimeErrorand integral values, including bothint64_tboundaries, evaluate as before. Header-only, no API or ABI change. The new testParserTest.BitwiseComplementRejectsNonIntegersaborts under UBSan on master and passes with the change.