Skip to content

fix: range-check the operand of bitwise not - #1349

Open
Nussu06 wants to merge 1 commit into
google:masterfrom
Nussu06:bitwise-not-range-check
Open

Nussu06 wants to merge 1 commit into
google:masterfrom
Nussu06:bitwise-not-range-check

Conversation

@Nussu06

@Nussu06 Nussu06 commented Oct 27, 2026

Copy link
Copy Markdown

The unary ~ operator in core/vm.cpp converts its operand with a raw (long) cast, while <<, >>, &, ^ and | all go through safeDoubleToInt64. As a result ~1e300 is undefined behaviour (UBSan float-cast-overflow; arm64 saturates and prints -9223372036854775808, x86-64 cvttsd2si gives LONG_MIN so the sign flips), values past 2^53 are accepted silently while 1e300 & 1 raises an error, and on Windows, where long is 32 bits, ~4294967296 is UB even though the spec says bitwise operands are converted to signed 64-bit integers. Fixed by converting the operand with safeDoubleToInt64, so ~ now always uses int64_t and raises the same "numeric value outside safe integer range for bitwise operation" error; results inside the safe range (including the existing safe_integer_conversion cases) are unchanged, and test_suite passes with the new error.integer_bitwise_not case, which fails before the patch.

This branch has not been deployed

No deployments
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