Skip to content

Commit

Permalink
Add a test for (minInt) / (-1)
Browse files Browse the repository at this point in the history
The result is not within the expressible range of signed 256 bit integer.
  • Loading branch information
pirapira committed Sep 13, 2016
1 parent 596f8ac commit 5b01e0a
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libsolidity/codegen/ExpressionCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b01e0a

Please sign in to comment.