From c8ac8618324d8d167c79a926880b2b5ab853f7f3 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 4 Jul 2018 11:23:51 +0200 Subject: Fix allocation of byte arrays. --- Changelog.md | 1 + libsolidity/codegen/CompilerUtils.cpp | 2 +- test/libsolidity/SolidityEndToEndTest.cpp | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index 64207c56..eae12c2c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -47,6 +47,7 @@ Compiler Features: Bugfixes: * Tests: Fix chain parameters to make ipc tests work with newer versions of cpp-ethereum. + * Code Generator: Fix allocation of byte arrays (zeroed out too much memory). ### 0.4.24 (2018-05-16) diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index a5e96335..2f45765a 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -510,7 +510,7 @@ void CompilerUtils::zeroInitialiseMemoryArray(ArrayType const& _type) codecopy(memptr, codesize(), size) memptr := add(memptr, size) })"); - templ("element_size", to_string(_type.baseType()->memoryHeadSize())); + templ("element_size", to_string(_type.isByteArray() ? 1 : _type.baseType()->memoryHeadSize())); m_context.appendInlineAssembly(templ.render(), {"length", "memptr"}); } else diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 4b1cb99b..bc613868 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -8060,7 +8060,7 @@ BOOST_AUTO_TEST_CASE(create_memory_array_allocation_size) // multiple of 32 char const* sourceCode = R"( contract C { - function f() public pure returns (uint d1, uint d2, uint d3) { + function f() public pure returns (uint d1, uint d2, uint d3, uint memsize) { bytes memory b1 = new bytes(31); bytes memory b2 = new bytes(32); bytes memory b3 = new bytes(256); @@ -8069,12 +8069,13 @@ BOOST_AUTO_TEST_CASE(create_memory_array_allocation_size) d1 := sub(b2, b1) d2 := sub(b3, b2) d3 := sub(b4, b3) + memsize := msize() } } } )"; compileAndRun(sourceCode); - ABI_CHECK(callContractFunction("f()"), encodeArgs(0x40, 0x40, 0x20 + 256)); + ABI_CHECK(callContractFunction("f()"), encodeArgs(0x40, 0x40, 0x20 + 256, 0x260)); } BOOST_AUTO_TEST_CASE(memory_arrays_of_various_sizes) -- cgit v1.2.3