Browse Source

Do not allow boolean operators for integers.

Fixes #2496
cl-refactor
chriseth 10 years ago
parent
commit
888909bee7
  1. 1
      libsolidity/Token.h
  2. 2
      libsolidity/Types.cpp
  3. 16
      test/libsolidity/SolidityNameAndTypeResolution.cpp

1
libsolidity/Token.h

@ -366,6 +366,7 @@ public:
}
static bool isBitOp(Value op) { return (BitOr <= op && op <= SHR) || op == BitNot; }
static bool isBooleanOp(Value op) { return (Or <= op && op <= And) || op == Not; }
static bool isUnaryOp(Value op) { return (Not <= op && op <= Delete) || op == Add || op == Sub || op == After; }
static bool isCountOp(Value op) { return op == Inc || op == Dec; }
static bool isShiftOp(Value op) { return (SHL <= op) && (op <= SHR); }

2
libsolidity/Types.cpp

@ -311,6 +311,8 @@ TypePointer IntegerType::binaryOperatorResult(Token::Value _operator, TypePointe
// All integer types can be compared
if (Token::isCompareOp(_operator))
return commonType;
if (Token::isBooleanOp(_operator))
return TypePointer();
// Nothing else can be done with addresses
if (commonType->isAddress())
return TypePointer();

16
test/libsolidity/SolidityNameAndTypeResolution.cpp

@ -1883,6 +1883,22 @@ BOOST_AUTO_TEST_CASE(positive_integers_to_unsigned_out_of_bound)
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode), TypeError);
}
BOOST_AUTO_TEST_CASE(integer_boolean_operators)
{
char const* sourceCode1 = R"(
contract test { function() { uint x = 1; uint y = 2; x || y; } }
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode1), TypeError);
char const* sourceCode2 = R"(
contract test { function() { uint x = 1; uint y = 2; x && y; } }
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode2), TypeError);
char const* sourceCode3 = R"(
contract test { function() { uint x = 1; !x; } }
)";
BOOST_CHECK_THROW(parseTextAndResolveNames(sourceCode3), TypeError);
}
BOOST_AUTO_TEST_CASE(overwrite_memory_location_external)
{
char const* sourceCode = R"(

Loading…
Cancel
Save