From 0b6eea0c557d0c987baa2d560fe3871ba3bb4a58 Mon Sep 17 00:00:00 2001 From: Leonardo Alt Date: Fri, 27 Apr 2018 11:35:58 +0200 Subject: [PATCH 1/2] Bool variables should not allow arithmetic comparison --- libsolidity/ast/Types.cpp | 2 +- libsolidity/formal/SMTChecker.cpp | 6 +----- test/libsolidity/SMTChecker.cpp | 29 ----------------------------- 3 files changed, 2 insertions(+), 35 deletions(-) diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index 60e3183c3..066868c97 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -1358,7 +1358,7 @@ TypePointer BoolType::binaryOperatorResult(Token::Value _operator, TypePointer c { if (category() != _other->category()) return TypePointer(); - if (Token::isCompareOp(_operator) || _operator == Token::And || _operator == Token::Or) + if (_operator == Token::Equal || _operator == Token::NotEqual || _operator == Token::And || _operator == Token::Or) return _other; else return TypePointer(); diff --git a/libsolidity/formal/SMTChecker.cpp b/libsolidity/formal/SMTChecker.cpp index 425c5c1e3..8639317ba 100644 --- a/libsolidity/formal/SMTChecker.cpp +++ b/libsolidity/formal/SMTChecker.cpp @@ -485,11 +485,7 @@ void SMTChecker::compareOperation(BinaryOperation const& _op) solUnimplementedAssert(SSAVariable::isBool(_op.annotation().commonType->category()), "Operation not yet supported"); value = make_shared( op == Token::Equal ? (left == right) : - op == Token::NotEqual ? (left != right) : - op == Token::LessThan ? (!left && right) : - op == Token::LessThanOrEqual ? (!left || right) : - op == Token::GreaterThan ? (left && !right) : - /*op == Token::GreaterThanOrEqual*/ (left || !right) + /*op == Token::NotEqual*/ (left != right) ); } // TODO: check that other values for op are not possible. diff --git a/test/libsolidity/SMTChecker.cpp b/test/libsolidity/SMTChecker.cpp index 71fdb906f..ec23f4521 100644 --- a/test/libsolidity/SMTChecker.cpp +++ b/test/libsolidity/SMTChecker.cpp @@ -388,35 +388,6 @@ BOOST_AUTO_TEST_CASE(bool_simple) } )"; CHECK_SUCCESS_NO_WARNINGS(text); - text = R"( - contract C { - function f(bool x) public pure { - bool y; - assert(x <= y); - } - } - )"; - CHECK_WARNING(text, "Assertion violation happens here"); - text = R"( - contract C { - function f(bool x) public pure { - bool y; - assert(x >= y); - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); - text = R"( - contract C { - function f(bool x) public pure { - require(x); - bool y; - assert(x > y); - assert(y < x); - } - } - )"; - CHECK_SUCCESS_NO_WARNINGS(text); } BOOST_AUTO_TEST_CASE(bool_int_mixed) From a19b516b2b0b56e2dc0f769ee96ffc871b7acee2 Mon Sep 17 00:00:00 2001 From: Leonardo Alt Date: Fri, 27 Apr 2018 14:13:18 +0200 Subject: [PATCH 2/2] Add syntax tests and Changelog entry --- Changelog.md | 4 +- .../syntaxTests/types/bool_ops.sol | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/libsolidity/syntaxTests/types/bool_ops.sol diff --git a/Changelog.md b/Changelog.md index 38c66d395..e047f3366 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,7 +1,9 @@ ### 0.5.0 (unreleased) -Features: +Breaking Changes: + * Type Checker: Disallow arithmetic operations for Boolean variables. +Features: Bugfixes: diff --git a/test/libsolidity/syntaxTests/types/bool_ops.sol b/test/libsolidity/syntaxTests/types/bool_ops.sol new file mode 100644 index 000000000..910339064 --- /dev/null +++ b/test/libsolidity/syntaxTests/types/bool_ops.sol @@ -0,0 +1,53 @@ +contract C { + function f(bool a, bool b) public pure { + bool c; + // OK + c = !a; + c = !b; + c = a == b; + c = a != b; + c = a || b; + c = a && b; + + // Not OK + c = a > b; + c = a < b; + c = a >= b; + c = a <= b; + c = a & b; + c = a | b; + c = a ^ b; + c = ~a; + c = ~b; + c = a + b; + c = a - b; + c = -a; + c = -b; + c = a * b; + c = a / b; + c = a ** b; + c = a % b; + c = a << b; + c = a >> b; + } +} +// ---- +// TypeError: (231-236): Operator > not compatible with types bool and bool +// TypeError: (250-255): Operator < not compatible with types bool and bool +// TypeError: (269-275): Operator >= not compatible with types bool and bool +// TypeError: (289-295): Operator <= not compatible with types bool and bool +// TypeError: (309-314): Operator & not compatible with types bool and bool +// TypeError: (328-333): Operator | not compatible with types bool and bool +// TypeError: (347-352): Operator ^ not compatible with types bool and bool +// TypeError: (366-368): Unary operator ~ cannot be applied to type bool +// TypeError: (382-384): Unary operator ~ cannot be applied to type bool +// TypeError: (398-403): Operator + not compatible with types bool and bool +// TypeError: (417-422): Operator - not compatible with types bool and bool +// TypeError: (436-438): Unary operator - cannot be applied to type bool +// TypeError: (452-454): Unary operator - cannot be applied to type bool +// TypeError: (468-473): Operator * not compatible with types bool and bool +// TypeError: (487-492): Operator / not compatible with types bool and bool +// TypeError: (506-512): Operator ** not compatible with types bool and bool +// TypeError: (526-531): Operator % not compatible with types bool and bool +// TypeError: (545-551): Operator << not compatible with types bool and bool +// TypeError: (565-571): Operator >> not compatible with types bool and bool