diff --git a/libsolidity/LValue.cpp b/libsolidity/LValue.cpp index 02e6cbca5..68428a0bf 100644 --- a/libsolidity/LValue.cpp +++ b/libsolidity/LValue.cpp @@ -107,6 +107,11 @@ void StorageItem::retrieveValue(SourceLocation const&, bool _remove) const << u256(0x100) << eth::Instruction::EXP << eth::Instruction::SWAP1 << eth::Instruction::DIV; if (m_dataType.getCategory() == Type::Category::FixedBytes) m_context << (u256(0x1) << (256 - 8 * m_dataType.getStorageBytes())) << eth::Instruction::MUL; + else if ( + m_dataType.getCategory() == Type::Category::Integer && + dynamic_cast(m_dataType).isSigned() + ) + m_context << u256(m_dataType.getStorageBytes() - 1) << eth::Instruction::SIGNEXTEND; else m_context << ((u256(0x1) << (8 * m_dataType.getStorageBytes())) - 1) << eth::Instruction::AND; } @@ -148,6 +153,17 @@ void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _loc m_context << (u256(0x1) << (256 - 8 * dynamic_cast(m_dataType).getNumBytes())) << eth::Instruction::SWAP1 << eth::Instruction::DIV; + else if ( + m_dataType.getCategory() == Type::Category::Integer && + dynamic_cast(m_dataType).isSigned() + ) + // remove the higher order bits + m_context + << (u256(1) << (8 * (32 - m_dataType.getStorageBytes()))) + << eth::Instruction::SWAP1 + << eth::Instruction::DUP2 + << eth::Instruction::MUL + << eth::Instruction::DIV; m_context << eth::Instruction::MUL << eth::Instruction::OR; // stack: value storage_ref updated_value m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE; diff --git a/test/SolidityEndToEndTest.cpp b/test/SolidityEndToEndTest.cpp index 0d7a933b0..3764949d9 100644 --- a/test/SolidityEndToEndTest.cpp +++ b/test/SolidityEndToEndTest.cpp @@ -3676,6 +3676,29 @@ BOOST_AUTO_TEST_CASE(packed_storage_structs_with_bytes0) BOOST_CHECK(callContractFunction("test()") == encodeArgs(true)); } +BOOST_AUTO_TEST_CASE(packed_storage_signed) +{ + char const* sourceCode = R"( + contract C { + int8 a; + uint8 b; + int8 c; + uint8 d; + function test() returns (uint x1, uint x2, uint x3, uint x4) { + a = -2; + b = -uint8(a) * 2; + c = a * int8(120) * int8(121); + x1 = uint(a); + x2 = b; + x3 = uint(c); + x4 = d; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK( callContractFunction("test()") == encodeArgs(u256(-2), u256(4), u256(-112), u256(0))); +} + BOOST_AUTO_TEST_SUITE_END() }