diff options
author | chriseth <chris@ethereum.org> | 2018-04-16 21:23:36 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-16 21:23:36 +0800 |
commit | 3d04d832972cf56555945fe51f9a7eb2fe9c4ac1 (patch) | |
tree | 67622d6d4ff1a0fc88e66bd930eb875d7e28a127 /libsolidity | |
parent | 533d08517f37496295e213194b489cd6ed15432b (diff) | |
parent | a9c16b8c3976dbd2c386586cdf143150a4266ac0 (diff) | |
download | dexon-solidity-3d04d832972cf56555945fe51f9a7eb2fe9c4ac1.tar.gz dexon-solidity-3d04d832972cf56555945fe51f9a7eb2fe9c4ac1.tar.zst dexon-solidity-3d04d832972cf56555945fe51f9a7eb2fe9c4ac1.zip |
Merge pull request #3868 from ethereum/bytescleanup
Properly force-clean for shortening bytesXX conversions.
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/codegen/CompilerUtils.cpp | 19 | ||||
-rw-r--r-- | libsolidity/codegen/ExpressionCompiler.cpp | 3 |
2 files changed, 10 insertions, 12 deletions
diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index b4550153..4af7d905 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -684,19 +684,17 @@ void CompilerUtils::convertType( // clear for conversion to longer bytes solAssert(targetTypeCategory == Type::Category::FixedBytes, "Invalid type conversion requested."); FixedBytesType const& targetType = dynamic_cast<FixedBytesType const&>(_targetType); - if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded) + if (typeOnStack.numBytes() == 0 || targetType.numBytes() == 0) + m_context << Instruction::POP << u256(0); + else if (targetType.numBytes() > typeOnStack.numBytes() || _cleanupNeeded) { - if (typeOnStack.numBytes() == 0) - m_context << Instruction::POP << u256(0); - else - { - m_context << ((u256(1) << (256 - typeOnStack.numBytes() * 8)) - 1); - m_context << Instruction::NOT << Instruction::AND; - } + int bytes = min(typeOnStack.numBytes(), targetType.numBytes()); + m_context << ((u256(1) << (256 - bytes * 8)) - 1); + m_context << Instruction::NOT << Instruction::AND; } } - } break; + } case Type::Category::Enum: solAssert(_targetType == _typeOnStack || targetTypeCategory == Type::Category::Integer, ""); if (enumOverflowCheckPending) @@ -798,8 +796,9 @@ void CompilerUtils::convertType( bytesConstRef data(value); if (targetTypeCategory == Type::Category::FixedBytes) { + int const numBytes = dynamic_cast<FixedBytesType const&>(_targetType).numBytes(); solAssert(data.size() <= 32, ""); - m_context << h256::Arith(h256(data, h256::AlignLeft)); + m_context << (h256::Arith(h256(data, h256::AlignLeft)) & (~(u256(-1) >> (8 * numBytes)))); } else if (targetTypeCategory == Type::Category::Array) { diff --git a/libsolidity/codegen/ExpressionCompiler.cpp b/libsolidity/codegen/ExpressionCompiler.cpp index ed5af42e..3cf46a9d 100644 --- a/libsolidity/codegen/ExpressionCompiler.cpp +++ b/libsolidity/codegen/ExpressionCompiler.cpp @@ -1023,7 +1023,6 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) solAssert(function.kind() == FunctionType::Kind::ABIEncodeWithSelector, ""); } - // Cleanup actually does not clean on shrinking the type. utils().convertType(*dataOnStack, FixedBytesType(4), true); // stack: <memory pointer> <selector> @@ -1034,7 +1033,7 @@ bool ExpressionCompiler::visit(FunctionCall const& _functionCall) let data_start := add(mem_ptr, 0x20) let data := mload(data_start) let mask := )" + mask + R"( - mstore(data_start, or(and(data, mask), and(selector, not(mask)))) + mstore(data_start, or(and(data, mask), selector)) })", {"mem_ptr", "selector"}); m_context << Instruction::POP; } |