From e7084d9e16d0b370cab29d1f9ba08f07844b55f5 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 12 Aug 2016 16:50:37 +0200 Subject: Throw on division by zero. --- docs/types.rst | 2 ++ libsolidity/codegen/ExpressionCompiler.cpp | 13 ++++++++++--- test/libsolidity/SolidityEndToEndTest.cpp | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/docs/types.rst b/docs/types.rst index 31f6b53d..d6445ed9 100644 --- a/docs/types.rst +++ b/docs/types.rst @@ -57,6 +57,8 @@ Operators: Division always truncates (it just maps to the DIV opcode of the EVM), but it does not truncate if both operators are :ref:`literals` (or literal expressions). +Division by zero and modulus with zero throws an exception. + .. index:: address, balance, send, call, callcode, delegatecall .. _address: diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 1f93cf8c..4695ff96 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1324,11 +1324,18 @@ void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Ty m_context << Instruction::MUL; break; case Token::Div: - m_context << (c_isSigned ? Instruction::SDIV : Instruction::DIV); - break; case Token::Mod: - m_context << (c_isSigned ? Instruction::SMOD : Instruction::MOD); + { + // Test for division by zero + m_context << Instruction::DUP2 << Instruction::ISZERO; + m_context.appendConditionalJumpTo(m_context.errorTag()); + + if (_operator == Token::Div) + m_context << (c_isSigned ? Instruction::SDIV : Instruction::DIV); + else + m_context << (c_isSigned ? Instruction::SMOD : Instruction::MOD); break; + } case Token::Exp: m_context << Instruction::EXP; break; diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 3920d948..647199ea 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -6208,6 +6208,27 @@ BOOST_AUTO_TEST_CASE(addmod_mulmod) BOOST_CHECK(callContractFunction("test()") == encodeArgs(u256(0))); } +BOOST_AUTO_TEST_CASE(divisiod_by_zero) +{ + char const* sourceCode = R"( + contract C { + function div(uint a, uint b) returns (uint) { + return a / b; + } + function mod(uint a, uint b) returns (uint) { + return a % b; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("div(uint256,uint256)", 7, 2) == encodeArgs(u256(3))); + // throws + BOOST_CHECK(callContractFunction("div(uint256,uint256)", 7, 0) == encodeArgs()); + BOOST_CHECK(callContractFunction("mod(uint256,uint256)", 7, 2) == encodeArgs(u256(1))); + // throws + BOOST_CHECK(callContractFunction("mod(uint256,uint256)", 7, 0) == encodeArgs()); +} + BOOST_AUTO_TEST_CASE(string_allocation_bug) { char const* sourceCode = R"( -- cgit From 34f9a59f18dcf8601d910aef599b2589dc080e2d Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 17 Aug 2016 11:27:08 +0200 Subject: Fix tests. --- test/libsolidity/SolidityExpressionCompiler.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp index 967b2907..e9a05745 100644 --- a/test/libsolidity/SolidityExpressionCompiler.cpp +++ b/test/libsolidity/SolidityExpressionCompiler.cpp @@ -323,7 +323,15 @@ BOOST_AUTO_TEST_CASE(arithmetics) byte(Instruction::OR), byte(Instruction::SUB), byte(Instruction::ADD), + byte(Instruction::DUP2), + byte(Instruction::ISZERO), + byte(Instruction::PUSH1), 0x2, + byte(Instruction::JUMPI), byte(Instruction::MOD), + byte(Instruction::DUP2), + byte(Instruction::ISZERO), + byte(Instruction::PUSH1), 0x2, + byte(Instruction::JUMPI), byte(Instruction::DIV), byte(Instruction::MUL)}); BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end()); -- cgit