diff options
author | chriseth <c@ethdev.com> | 2015-11-17 21:46:19 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-11-17 21:46:19 +0800 |
commit | f4de369bad278936f7e3456ee71a77a264a1bedb (patch) | |
tree | 6c4a0761bf5ed194d569fa87dbc3660e31c1f356 | |
parent | 28c3f1839b6de71cd1339e076d0128da498a38c9 (diff) | |
parent | 0c900f9b8afe4094ca52030f55fcf5e84980cc12 (diff) | |
download | dexon-solidity-f4de369bad278936f7e3456ee71a77a264a1bedb.tar.gz dexon-solidity-f4de369bad278936f7e3456ee71a77a264a1bedb.tar.zst dexon-solidity-f4de369bad278936f7e3456ee71a77a264a1bedb.zip |
Merge pull request #218 from chriseth/fix_overwriteMemory
Fix memory overwrite problem for arrays.
-rw-r--r-- | libsolidity/codegen/LValue.cpp | 16 | ||||
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 15 |
2 files changed, 28 insertions, 3 deletions
diff --git a/libsolidity/codegen/LValue.cpp b/libsolidity/codegen/LValue.cpp index 574d42f8..864f28d0 100644 --- a/libsolidity/codegen/LValue.cpp +++ b/libsolidity/codegen/LValue.cpp @@ -103,10 +103,20 @@ void MemoryItem::storeValue(Type const& _sourceType, SourceLocation const&, bool if (!_move) { utils.moveToStackTop(m_dataType->sizeOnStack()); - utils.copyToStackTop(2, m_dataType->sizeOnStack()); + utils.copyToStackTop(1 + m_dataType->sizeOnStack(), m_dataType->sizeOnStack()); + } + if (!m_padded) + { + solAssert(m_dataType->calldataEncodedSize(false) == 1, "Invalid non-padded type."); + if (m_dataType->category() == Type::Category::FixedBytes) + m_context << u256(0) << eth::Instruction::BYTE; + m_context << eth::Instruction::SWAP1 << eth::Instruction::MSTORE8; + } + else + { + utils.storeInMemoryDynamic(*m_dataType, m_padded); + m_context << eth::Instruction::POP; } - utils.storeInMemoryDynamic(*m_dataType, m_padded); - m_context << eth::Instruction::POP; } else { diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 166786b9..81543356 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -5816,6 +5816,21 @@ BOOST_AUTO_TEST_CASE(lone_struct_array_type) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(3))); } +BOOST_AUTO_TEST_CASE(memory_overwrite) +{ + char const* sourceCode = R"( + contract C { + function f() returns (bytes x) { + x = "12345"; + x[3] = 0x61; + x[0] = 0x62; + } + } + )"; + compileAndRun(sourceCode); + BOOST_CHECK(callContractFunction("f()") == encodeDyn(string("b23a5"))); +} + BOOST_AUTO_TEST_SUITE_END() } |