Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions sjsonnet/src/sjsonnet/Evaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,11 @@ class Evaluator(
val ll = ld.toLong; val rl = rd.toLong
if (ll.toDouble != ld || rl.toDouble != rd) null // not safe integers
else if (rl < 0) null
else if (rl >= 1 && math.abs(ll) >= (1L << (63 - rl))) null
else Val.cachedNum(pos, (ll << rl).toDouble)
else {
val masked = (rl % 64).toInt
if (masked >= 1 && math.abs(ll) >= (1L << (63 - masked))) null
else Val.cachedNum(pos, (ll << masked).toDouble)
}
case Expr.BinaryOp.OP_>> =>
val ll = ld.toLong; val rl = rd.toLong
if (ll.toDouble != ld || rl.toDouble != rd) null
Expand Down Expand Up @@ -1411,10 +1414,11 @@ class Evaluator(
val ll = visitExprAsDouble(e.lhs).toSafeLong(pos)
val rr = visitExprAsDouble(e.rhs).toSafeLong(pos)
if (rr < 0) Error.fail("shift by negative exponent", pos)
if (rr >= 1 && math.abs(ll) >= (1L << (63 - rr)))
val masked = (rr % 64).toInt
if (masked >= 1 && math.abs(ll) >= (1L << (63 - masked)))
Error.fail("numeric value outside safe integer range for bitwise operation", pos)
else
Val.cachedNum(pos, (ll << rr).toDouble)
Val.cachedNum(pos, (ll << masked).toDouble)

case Expr.BinaryOp.OP_>> =>
val ll = visitExprAsDouble(e.lhs).toSafeLong(pos)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3 << 126
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sjsonnet.Error: numeric value outside safe integer range for bitwise operation
at [<root>].(error.leftshift_large_amount_overflow.jsonnet:1:3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
std.assertEqual(1 << 64, 1) &&
std.assertEqual(0 << 64, 0) &&
std.assertEqual(0 << 1000, 0) &&
std.assertEqual(1 << 128, 1) &&
std.assertEqual(1 << 192, 1) &&
std.assertEqual(4.5 << 66, 16) &&
true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
Loading