Skip to content
Open
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
7 changes: 7 additions & 0 deletions eth_abi/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,13 @@ def read_data_from_stream(self, stream):
raise NonEmptyPaddingBytes(
f"Padding bytes were not empty: {repr(padding_bytes)}"
)
else:
# In non-strict mode, be more flexible with truncated data
if len(data) < data_length:
# Not even enough data for the declared content length
raise InsufficientDataBytes(
f"Tried to read {data_length} bytes, only got {len(data)} bytes"
)

return data[:data_length]

Expand Down
39 changes: 39 additions & 0 deletions eth_abi/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,52 @@ def _get_decoder_uncached(self, type_str, strict=True):
decoder = self._get_registration(self._decoders, type_str)

if hasattr(decoder, "is_dynamic") and decoder.is_dynamic:
# Create a copy of the decoder to avoid mutating shared cached instances
# This prevents issues where strict=False calls affect subsequent
# strict=True calls
decoder = copy.deepcopy(decoder)

# Set a transient flag each time a call is made to ``get_decoder()``.
# Only dynamic decoders should be allowed these looser constraints. All
# other decoders should keep the default value of ``True``.
decoder.strict = strict

# Recursively set strict on nested decoders
self._set_strict_on_nested_decoders(decoder, strict)

return decoder

def _set_strict_on_nested_decoders(self, decoder, strict):
"""
Recursively set the strict attribute on nested decoders.
"""
if hasattr(decoder, "item_decoder") and decoder.item_decoder:
if hasattr(decoder.item_decoder, "strict"):
decoder.item_decoder.strict = strict
if (
hasattr(decoder.item_decoder, "tail_decoder")
and decoder.item_decoder.tail_decoder
):
if hasattr(decoder.item_decoder.tail_decoder, "strict"):
decoder.item_decoder.tail_decoder.strict = strict
self._set_strict_on_nested_decoders(
decoder.item_decoder.tail_decoder, strict
)

if hasattr(decoder, "decoders") and decoder.decoders:
for nested_decoder in decoder.decoders:
if hasattr(nested_decoder, "strict"):
nested_decoder.strict = strict
if (
hasattr(nested_decoder, "tail_decoder")
and nested_decoder.tail_decoder
):
if hasattr(nested_decoder.tail_decoder, "strict"):
nested_decoder.tail_decoder.strict = strict
self._set_strict_on_nested_decoders(
nested_decoder.tail_decoder, strict
)

def copy(self):
"""
Copies a registry such that new registrations can be made or existing
Expand Down
50 changes: 50 additions & 0 deletions tests/core/abi_tests/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,56 @@ def test_abi_decode_with_shorter_data_than_32_bytes(types, hex_data, expected):
decode(types, bytes.fromhex(hex_data))


def test_abi_decode_bytes_array_with_truncated_padding():
"""
Test that bytes[] arrays with complete content but missing padding
can be decoded in non-strict mode but raise an error in strict mode.

This is a regression test for the fix that allows decoding ABI-encoded
bytes[] data where content is complete but trailing padding is missing.
"""
# Data from decode_multicall.py - represents bytes[] with truncated padding
data = bytes.fromhex(
"0000000000000000000000000000000000000000000000000000000000000020" # offset to bytes[] array # noqa: E501
"0000000000000000000000000000000000000000000000000000000000000001" # array length (1) # noqa: E501
"0000000000000000000000000000000000000000000000000000000000000020" # offset to first element # noqa: E501
"0000000000000000000000000000000000000000000000000000000000000104" # element length (260 bytes) # noqa: E501
# 260 bytes of content (truncated from original due to missing padding)
"b858183f0000000000000000000000000000000000000000000000000000000000000020"
"0000000000000000000000000000000000000000000000000000000000000080"
"00000000000000000000000078ba4c2b0cc3385ca967d250b2313f187d5002f3"
"000000000000000000000000000000000000000000000000000000000a59d6f2"
"000000000000000000000000000000000000000000000000008f3316b7531da4"
"000000000000000000000000000000000000000000000000000000000000002b"
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000064c02aaa39b223fe8d0a"
"0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000"
# Note: Missing ~26 bytes of padding here that would normally pad to 288 total bytes # noqa: E501
)

with pytest.raises(InsufficientDataBytes, match="Tried to read 288 bytes"):
decode(["bytes[]"], data, strict=True)

result = decode(["bytes[]"], data, strict=False)

# Validate the structure
assert len(result) == 1 # One top-level element (the bytes[] array)
assert len(result[0]) == 1 # One element in the bytes[] array
assert len(result[0][0]) == 260 # The element should be 260 bytes

# Validate the decoded content matches expected hex
expected_hex = (
"b858183f0000000000000000000000000000000000000000000000000000000000000020"
"0000000000000000000000000000000000000000000000000000000000000080"
"00000000000000000000000078ba4c2b0cc3385ca967d250b2313f187d5002f3"
"000000000000000000000000000000000000000000000000000000000a59d6f2"
"000000000000000000000000000000000000000000000000008f3316b7531da4"
"000000000000000000000000000000000000000000000000000000000000002b"
"a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000064c02aaa39b223fe8d0a"
"0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000"
)
assert result[0][0].hex() == expected_hex


@pytest.mark.parametrize(
"typestring,malformed_payload",
(
Expand Down