Skip to content

Commit

Permalink
Fix math.bitShiftRight for negative int (#983)
Browse files Browse the repository at this point in the history
* Fix math.bitShiftRight for negative int

Signed-off-by: Justin King <[email protected]>

* Update ext/math_test.go

Signed-off-by: Justin King <[email protected]>

---------

Signed-off-by: Justin King <[email protected]>
  • Loading branch information
jcking authored Jul 19, 2024
1 parent 21976cf commit bc96f3b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand Down
2 changes: 1 addition & 1 deletion ext/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions ext/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down

0 comments on commit bc96f3b

Please sign in to comment.