Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix termination condition in zero_pad #1611

Merged
merged 4 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion vyper/parser/events.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -899,19 +899,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