Skip to content

Commit

Permalink
Merge pull request #1611 from charles-cooper/zero_pad
Browse files Browse the repository at this point in the history
Fix termination condition in zero_pad
  • Loading branch information
charles-cooper committed Sep 24, 2019
2 parents aa7a49e + 9a46479 commit 32ce6c9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion vyper/parser/events.py
Expand Up @@ -108,9 +108,10 @@ def pack_args_by_32(holder, maxlen, arg, typ, context, placeholder,
typ=typ, location='memory', annotation="pack_args_by_32:dest_placeholder")
copier = make_byte_array_copier(dest_placeholder, source_lll, pos=pos)
holder.append(copier)
item_maxlen = source_lll.typ.maxlen
# Add zero padding.
holder.append(
zero_pad(dest_placeholder, maxlen, zero_pad_i=zero_pad_i)
zero_pad(dest_placeholder, item_maxlen, zero_pad_i=zero_pad_i)
)

# Increment offset counter.
Expand Down
14 changes: 11 additions & 3 deletions vyper/parser/parser_utils.py
Expand Up @@ -889,19 +889,27 @@ def annotate_ast(
RewriteUnarySubVisitor().visit(parsed_ast)


# zero pad a bytearray according to the ABI spec. The last word
# of the byte array needs to be right-padded with zeroes.
def zero_pad(bytez_placeholder, maxlen, context=None, zero_pad_i=None):
zero_padder = LLLnode.from_list(['pass'])
if maxlen > 0:
# Iterator used to zero pad memory.
if zero_pad_i is None:
zero_pad_i = context.new_placeholder(BaseType('uint256'))
zero_padder = LLLnode.from_list([
'repeat', zero_pad_i, ['mload', bytez_placeholder], ceil32(maxlen), [
# the runtime length of the data rounded up to nearest 32
# from spec:
# the actual value of X as a byte sequence,
# followed by the *minimum* number of zero-bytes
# such that len(enc(X)) is a multiple of 32.
'with', '_ceil32_end', ['ceil32', ['mload', bytez_placeholder]],
['repeat', zero_pad_i, ['mload', bytez_placeholder], ceil32(maxlen), [
'seq',
# stay within allocated bounds
['if', ['gt', ['mload', zero_pad_i], ceil32(maxlen)], 'break'],
['if', ['ge', ['mload', zero_pad_i], '_ceil32_end'], 'break'],
['mstore8', ['add', ['add', 32, bytez_placeholder], ['mload', zero_pad_i]], 0]
]],
]]],
annotation="Zero pad",
)
return zero_padder
Expand Down

0 comments on commit 32ce6c9

Please sign in to comment.