From 98b686c67207c2c3ced0110dced5ed6934716386 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 31 Mar 2016 01:17:48 +0200 Subject: [PATCH 1/2] Test to check we correctly clean higher order bits for index access. --- test/libsolidity/SolidityEndToEndTest.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 663493c9f6d4..c872f011922c 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() } From 2c29492227553e3861089a0dc944b60fafcb7055 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 31 Mar 2016 01:18:09 +0200 Subject: [PATCH 2/2] Correctly clean higher order bits for index access. --- libsolidity/codegen/ExpressionCompiler.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index 64eb671039d2..a17ec2f6af26 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: [] 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: // check out-of-bounds access m_context << u256(fixedBytesType.numBytes());