From 58ffd61dcc2874d07089b325850c670d6f622e14 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Thu, 26 Apr 2018 13:25:04 -0700 Subject: [PATCH] test suite for bitwise operators brief NEWS blurb allow for "counter" operands (does anyone still use these?) for one (but not both) of the bitwise operands --- NEWS | 4 ++++ src/Expr.cc | 11 +++++++---- testing/btest/Baseline/language.count/out | 11 +++++++++++ testing/btest/language/count.bro | 11 +++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index aa43682047..eeeda2a42c 100644 --- a/NEWS +++ b/NEWS @@ -146,6 +146,10 @@ New Functionality - Added new SMB events: smb1_transaction_secondary_request, smb1_transaction2_secondary_request, smb1_transaction_response +- Added support for bitwise operations on "count" values. '&', '|' and + '^' are binary "and", "or" and "xor" operators, and '~' is a unary + ones-complement operator. + Changed Functionality --------------------- diff --git a/src/Expr.cc b/src/Expr.cc index 5f79d08dbe..f5c8e66b50 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -1874,12 +1874,15 @@ BitExpr::BitExpr(BroExprTag arg_tag, Expr* arg_op1, Expr* arg_op2) if ( IsVector(bt2) ) bt2 = op2->Type()->AsVectorType()->YieldType()->Tag(); - if ( bt1 == TYPE_COUNT && bt2 == bt1 ) + if ( (bt1 == TYPE_COUNT || bt1 == TYPE_COUNTER) && + (bt2 == TYPE_COUNT || bt2 == TYPE_COUNTER) ) { - if ( is_vector(op1) || is_vector(op2) ) - SetType(new VectorType(base_type(bt1))); + if ( bt1 == TYPE_COUNTER && bt2 == TYPE_COUNTER ) + ExprError("cannot apply a bitwise operator to two \"counter\" operands"); + else if ( is_vector(op1) || is_vector(op2) ) + SetType(new VectorType(base_type(TYPE_COUNT))); else - SetType(base_type(bt1)); + SetType(base_type(TYPE_COUNT)); } else diff --git a/testing/btest/Baseline/language.count/out b/testing/btest/Baseline/language.count/out index 4ef65b6098..f1e1eef587 100644 --- a/testing/btest/Baseline/language.count/out +++ b/testing/btest/Baseline/language.count/out @@ -14,5 +14,16 @@ modulus operator (PASS) division operator (PASS) assignment operator (PASS) assignment operator (PASS) +bitwise and (PASS) +bitwise and (PASS) +bitwise and (PASS) +bitwise or (PASS) +bitwise or (PASS) +bitwise or (PASS) +bitwise xor (PASS) +bitwise xor (PASS) +bitwise xor (PASS) +bitwise complement (PASS) +bitwise complement (PASS) max count value = 18446744073709551615 (PASS) max count value = 18446744073709551615 (PASS) diff --git a/testing/btest/language/count.bro b/testing/btest/language/count.bro index b0972e29fa..39a3786dfb 100644 --- a/testing/btest/language/count.bro +++ b/testing/btest/language/count.bro @@ -47,6 +47,17 @@ event bro_init() test_case( "assignment operator", c2 == 8 ); c2 -= 2; test_case( "assignment operator", c2 == 6 ); + test_case( "bitwise and", c2 & 0x4 == 0x4 ); + test_case( "bitwise and", c4 & 0x4 == 0x4 ); + test_case( "bitwise and", c8 & 0x4 == 0x0 ); + test_case( "bitwise or", c2 | 0x4 == c2 ); + test_case( "bitwise or", c4 | 0x4 == c4 ); + test_case( "bitwise or", c8 | 0x4 == c7 ); + test_case( "bitwise xor", c7 ^ 0x4 == c8 ); + test_case( "bitwise xor", c4 ^ 0x4 == 251 ); + test_case( "bitwise xor", c8 ^ 0x4 == c7 ); + test_case( "bitwise complement", ~c6 == 0 ); + test_case( "bitwise complement", ~~c4 == c4 ); # Max. value tests