diff options
| -rw-r--r-- | docs/frequently-asked-questions.rst | 17 | ||||
| -rw-r--r-- | libsolidity/codegen/ExpressionCompiler.cpp | 2 | ||||
| -rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 16 |
3 files changed, 35 insertions, 0 deletions
diff --git a/docs/frequently-asked-questions.rst b/docs/frequently-asked-questions.rst index 6ac5a9e9..fc7d7b7f 100644 --- a/docs/frequently-asked-questions.rst +++ b/docs/frequently-asked-questions.rst @@ -666,6 +666,23 @@ gas and return your 20 Wei). In the above example, the low-level function `call` is used to invoke another contract with `p.data` as payload and `p.amount` Wei is sent with that call. +What happens to a struct's mapping when copying over a struct? +============================================================== + +This is a very interesting question. Suppose that we have a contract field set up like such:: + + struct user{ + mapping(string => address) usedContracts; + } + function somefunction{ + user user1; + user1.usedContracts["Hello"] = "World"; + user user2 = user1; + } + +In this case, the mapping of the struct being copied over into the userList is ignored as there is no "list of mapped keys". +Therefore it is not possible to find out which values should be copied over. + How do I initialize a contract with only a specific amount of wei? ================================================================== diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 64eb6710..a17ec2f6 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1069,6 +1069,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) solAssert(_indexAccess.indexExpression(), "Index expression expected."); _indexAccess.indexExpression()->accept(*this); + utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType(256), true); // stack layout: <base_ref> [<length>] <index> ArrayUtils(m_context).accessIndex(arrayType); switch (arrayType.location()) @@ -1104,6 +1105,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) solAssert(_indexAccess.indexExpression(), "Index expression expected."); _indexAccess.indexExpression()->accept(*this); + utils().convertType(*_indexAccess.indexExpression()->annotation().type, IntegerType(256), true); // stack layout: <value> <index> // check out-of-bounds access m_context << u256(fixedBytesType.numBytes()); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 663493c9..c872f011 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -6575,6 +6575,22 @@ BOOST_AUTO_TEST_CASE(inline_assembly_jumps) BOOST_CHECK(callContractFunction("f()", u256(7)) == encodeArgs(u256(34))); } +BOOST_AUTO_TEST_CASE(index_access_with_type_conversion) +{ + // Test for a bug where higher order bits cleanup was not done for array index access. + char const* sourceCode = R"( + contract C { + function f(uint x) returns (uint[256] r){ + r[uint8(x)] = 2; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + // neither of the two should throw due to out-of-bounds access + BOOST_CHECK(callContractFunction("f(uint256)", u256(0x01)).size() == 256 * 32); + BOOST_CHECK(callContractFunction("f(uint256)", u256(0x101)).size() == 256 * 32); +} + BOOST_AUTO_TEST_SUITE_END() } |
