From 5b01e0ac7f85091be9b57f3eb399668316ac5be2 Mon Sep 17 00:00:00 2001 From: Yoichi Hirai Date: Tue, 13 Sep 2016 19:04:52 +0200 Subject: [PATCH] Add a test for (minInt) / (-1) The result is not within the expressible range of signed 256 bit integer. --- libsolidity/codegen/ExpressionCompiler.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 96ca429660b7..4f78ceb82e7c 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1337,6 +1337,20 @@ void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Ty m_context << Instruction::DUP2 << Instruction::ISZERO; m_context.appendConditionalJumpTo(m_context.errorTag()); + // Test for (minInt) / (-1) + if (c_isSigned && _operator == Token::Div) + { + m_context << Instruction::DUP1 << Instruction::NOT // ~x + << u256(2) << u256(255) << Instruction::EXP // 2 ** 255 + << Instruction::EQ; + // stack: (x == minInt), x, y + m_context << Instruction::DUP3 << Instruction::NOT // ~y + << Instruction::ISZERO; + // stack: (y == (-1)), (x == minInt), x, y + m_context << Instruction::AND; + m_context.appendConditionalJumpTo(m_context.errorTag()); + } + if (_operator == Token::Div) m_context << (c_isSigned ? Instruction::SDIV : Instruction::DIV); else