Handle error cases for bit-shift operators more cleanly

This commit is contained in:
Tim Wojtulewicz 2022-07-21 13:03:16 -07:00
parent ac4b7f9f02
commit 8fff3c76b9
3 changed files with 41 additions and 4 deletions

View file

@ -960,11 +960,47 @@ ValPtr BinaryExpr::Fold(Val* v1, Val* v2) const
DO_UINT_FOLD(^);
break;
case EXPR_LSHIFT:
DO_INT_FOLD(<<);
{
if ( is_integral )
{
if ( i2 < 0 )
RuntimeError("left shift by negative value");
else if ( i1 < 1 )
RuntimeError("left shifting a negative number is undefined");
i3 = i1 << i2;
}
else if ( is_unsigned )
{
if ( u2 < 0 )
RuntimeError("left shift by negative value");
u3 = u1 << u2;
}
else
RuntimeErrorWithCallStack("bad type in BinaryExpr::Fold");
break;
}
case EXPR_RSHIFT:
DO_INT_FOLD(>>);
{
if ( is_integral )
{
if ( i2 < 0 )
RuntimeError("right shift by negative value");
i3 = i1 >> i2;
}
else if ( is_unsigned )
{
if ( u2 < 0 )
RuntimeError("right shift by negative value");
u3 = u1 >> u2;
}
else
RuntimeErrorWithCallStack("bad type in BinaryExpr::Fold");
break;
}
case EXPR_AND_AND:
DO_INT_FOLD(&&);

View file

@ -18,6 +18,7 @@ modulus operator (PASS)
division operator (PASS)
assignment operator (PASS)
assignment operator (PASS)
bitwise lshift (PASS)
bitwise rshift (PASS)
max int value = 9223372036854775807 (PASS)
min int value = -9223372036854775808 (PASS)

View file

@ -54,7 +54,8 @@ event zeek_init()
test_case( "assignment operator", i2 == 7 );
i2 -= 2;
test_case( "assignment operator", i2 == 5 );
test_case( "bitwise rshift", i10 >> 1 == -6 );
test_case( "bitwise lshift", i6 << 1 == 24 );
test_case( "bitwise rshift", i6 >> 1 == 6 );
# Max/min value tests
@ -68,4 +69,3 @@ event zeek_init()
test_case( str4, str4 == "min int value = -9223372036854775808" );
}