From d2077b8962a084651b0f40fde79c91e0bc9923a4 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 19 Mar 2015 18:15:16 +0100 Subject: [PATCH] Byte size checked for zero; coding style. --- libsolidity/ArrayUtils.cpp | 27 ++++++++++++++------------- libsolidity/ArrayUtils.h | 2 +- libsolidity/ExpressionCompiler.cpp | 1 + 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/libsolidity/ArrayUtils.cpp b/libsolidity/ArrayUtils.cpp index a46dea3c8..bbf7f985a 100644 --- a/libsolidity/ArrayUtils.cpp +++ b/libsolidity/ArrayUtils.cpp @@ -440,9 +440,10 @@ void ArrayUtils::retrieveLength(ArrayType const& _arrayType) const } } -void ArrayUtils::incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosition, unsigned storageOffsetPosition) const +void ArrayUtils::incrementByteOffset(unsigned _byteSize, unsigned _byteOffsetPosition, unsigned _storageOffsetPosition) const { - solAssert(byteSize < 32, ""); + solAssert(_byteSize < 32, ""); + solAssert(_byteSize != 0, ""); // We do the following, but avoiding jumps: // byteOffset += byteSize // if (byteOffset + byteSize > 32) @@ -450,28 +451,28 @@ void ArrayUtils::incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosit // storageOffset++; // byteOffset = 0; // } - if (byteOffsetPosition > 1) - m_context << eth::swapInstruction(byteOffsetPosition - 1); - m_context << u256(byteSize) << eth::Instruction::ADD; - if (byteOffsetPosition > 1) - m_context << eth::swapInstruction(byteOffsetPosition - 1); + if (_byteOffsetPosition > 1) + m_context << eth::swapInstruction(_byteOffsetPosition - 1); + m_context << u256(_byteSize) << eth::Instruction::ADD; + if (_byteOffsetPosition > 1) + m_context << eth::swapInstruction(_byteOffsetPosition - 1); // compute, X := (byteOffset + byteSize - 1) / 32, should be 1 iff byteOffset + bytesize > 32 m_context - << u256(32) << eth::dupInstruction(1 + byteOffsetPosition) << u256(byteSize - 1) + << u256(32) << eth::dupInstruction(1 + _byteOffsetPosition) << u256(_byteSize - 1) << eth::Instruction::ADD << eth::Instruction::DIV; // increment storage offset if X == 1 (just add X to it) // stack: X m_context - << eth::swapInstruction(storageOffsetPosition) << eth::dupInstruction(storageOffsetPosition + 1) - << eth::Instruction::ADD << eth::swapInstruction(storageOffsetPosition); + << eth::swapInstruction(_storageOffsetPosition) << eth::dupInstruction(_storageOffsetPosition + 1) + << eth::Instruction::ADD << eth::swapInstruction(_storageOffsetPosition); // stack: X // set source_byte_offset to zero if X == 1 (using source_byte_offset *= 1 - X) m_context << u256(1) << eth::Instruction::SUB; // stack: 1 - X - if (byteOffsetPosition == 1) + if (_byteOffsetPosition == 1) m_context << eth::Instruction::MUL; else m_context - << eth::dupInstruction(byteOffsetPosition + 1) << eth::Instruction::MUL - << eth::swapInstruction(byteOffsetPosition) << eth::Instruction::POP; + << eth::dupInstruction(_byteOffsetPosition + 1) << eth::Instruction::MUL + << eth::swapInstruction(_byteOffsetPosition) << eth::Instruction::POP; } diff --git a/libsolidity/ArrayUtils.h b/libsolidity/ArrayUtils.h index 6e4ceec1c..22c0646a5 100644 --- a/libsolidity/ArrayUtils.h +++ b/libsolidity/ArrayUtils.h @@ -76,7 +76,7 @@ private: /// the storage offset if adding this number again would increase the counter over 32. /// @param byteOffsetPosition the stack offset of the storage byte offset /// @param storageOffsetPosition the stack offset of the storage slot offset - void incrementByteOffset(unsigned byteSize, unsigned byteOffsetPosition, unsigned storageOffsetPosition) const; + void incrementByteOffset(unsigned _byteSize, unsigned _byteOffsetPosition, unsigned _storageOffsetPosition) const; CompilerContext& m_context; }; diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index 144827d44..da762147a 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -839,6 +839,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) // goal: // = <(index % itemsPerSlot) * byteSize> unsigned byteSize = arrayType.getBaseType()->getStorageBytes(); + solAssert(byteSize != 0, ""); unsigned itemsPerSlot = 32 / byteSize; m_context << u256(itemsPerSlot) << eth::Instruction::SWAP2; // stack: itemsPerSlot index data_ref