From bc96f3b01f7d80a31f25e6008707117e0a8a1bb9 Mon Sep 17 00:00:00 2001 From: Justin King Date: Fri, 19 Jul 2024 12:23:21 -0700 Subject: [PATCH] Fix math.bitShiftRight for negative int (#983) * Fix math.bitShiftRight for negative int Signed-off-by: Justin King * Update ext/math_test.go Signed-off-by: Justin King --------- Signed-off-by: Justin King --- WORKSPACE | 2 +- ext/math.go | 2 +- ext/math_test.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index 19ca3e15..da3c8bd9 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -119,7 +119,7 @@ go_repository( # CEL Spec deps go_repository( name = "com_google_cel_spec", - commit = "8958834564cfc6a4764f76238d03cdbe772665bb", + commit = "5299974f1c69103e4bb4eec48f7d9b24413ca3c7", importpath = "github.com/google/cel-spec", ) diff --git a/ext/math.go b/ext/math.go index 695c707c..893654e7 100644 --- a/ext/math.go +++ b/ext/math.go @@ -755,7 +755,7 @@ func bitShiftRightIntInt(value, bits ref.Val) ref.Val { if bs < types.IntZero { return types.NewErr("math.bitShiftRight() negative offset: %d", bs) } - return v >> bs + return types.Int(types.Uint(v) >> bs) } func bitShiftRightUintInt(value, bits ref.Val) ref.Val { diff --git a/ext/math_test.go b/ext/math_test.go index fc5be8d3..91252259 100644 --- a/ext/math_test.go +++ b/ext/math_test.go @@ -129,8 +129,8 @@ func TestMath(t *testing.T) { {expr: "math.bitShiftLeft(-1, 200) == 0"}, {expr: "math.bitShiftRight(1024, 2) == 256"}, {expr: "math.bitShiftRight(1024, 64) == 0"}, - {expr: "math.bitShiftRight(-1024, 3) == -128"}, - {expr: "math.bitShiftRight(-1024, 64) == -1"}, + {expr: "math.bitShiftRight(-1024, 3) == 2305843009213693824"}, + {expr: "math.bitShiftRight(-1024, 64) == 0"}, // Unsigned bitwise ops {expr: "math.bitAnd(1u, 2u) == 0u"}, {expr: "math.bitAnd(1u, 3u) == 1u"},