From 5327255b59ee0a40bc61e634777a304c3648a35b Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Mon, 26 Oct 2026 19:14:19 +0530 Subject: [PATCH] fix: range-check the operand of bitwise not --- core/vm.cpp | 3 ++- test_suite/error.integer_bitwise_not.jsonnet | 3 +++ test_suite/error.integer_bitwise_not.jsonnet.golden | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 test_suite/error.integer_bitwise_not.jsonnet create mode 100644 test_suite/error.integer_bitwise_not.jsonnet.golden diff --git a/core/vm.cpp b/core/vm.cpp index 667f9e1c..b90ed8d4 100644 --- a/core/vm.cpp +++ b/core/vm.cpp @@ -3246,7 +3246,8 @@ class Interpreter { case UOP_MINUS: scratch = makeNumber(-scratch.v.d); break; case UOP_BITWISE_NOT: - scratch = makeNumber(~(long)(scratch.v.d)); + scratch = makeNumber( + ~safeDoubleToInt64(scratch.v.d, ast.location)); break; default: diff --git a/test_suite/error.integer_bitwise_not.jsonnet b/test_suite/error.integer_bitwise_not.jsonnet new file mode 100644 index 00000000..b433aaf4 --- /dev/null +++ b/test_suite/error.integer_bitwise_not.jsonnet @@ -0,0 +1,3 @@ +// Value just beyond MAX_SAFE_INTEGER (2^53 - 1) +local beyond_max = 9007199254740992; // 2^53 +~beyond_max // Should throw error "numeric value outside safe integer range for bitwise operation" diff --git a/test_suite/error.integer_bitwise_not.jsonnet.golden b/test_suite/error.integer_bitwise_not.jsonnet.golden new file mode 100644 index 00000000..69b96c76 --- /dev/null +++ b/test_suite/error.integer_bitwise_not.jsonnet.golden @@ -0,0 +1,2 @@ +RUNTIME ERROR: numeric value outside safe integer range for bitwise operation. + error.integer_bitwise_not.jsonnet:3:1-12