Skip to content

fix(dyn-abi): validate event topic count before decoding body - #1122

Merged
DaniPopes merged 3 commits into
alloy-rs:mainfrom
satyakwok:fix/dyn-abi-event-topic-count-before-body
Jun 10, 2026
Merged

DaniPopes merged 3 commits into
alloy-rs:mainfrom
satyakwok:fix/dyn-abi-event-topic-count-before-body

Conversation

@satyakwok

Copy link
Copy Markdown
Contributor

Fixes #1123.

Problem

DynSolEvent::decode_log_parts decoded the body data before fully validating the topic count. The up-front size_hint check only fires for exact-size iterators, so a log whose topic count does not match the event — when the topics are supplied via a non-exact iterator (filter/map/streaming) — reached the body decode and failed with a confusing Overrun instead of TopicLengthMismatch.

This shows up in practice because ERC-20 and ERC-721 Transfer share the same topic0 (keccak("Transfer(address,address,uint256)")), so a topic-only filter matches both. Decoding an ERC-721 transfer log (4 topics, empty data) against the ERC-20 Transfer ABI displaced the body words and overran:

exact   iterator -> TopicLengthMismatch { expected: 3, actual: 4 }   // ok
inexact iterator -> SolTypes(Overrun)                                // the bug

Fix

Decode the indexed topics, validate the topic count, then decode the body. Running out of topics now also reports TopicLengthMismatch rather than silently producing a short indexed list. Valid logs are unaffected; the early size_hint fast-path is kept.

No public API or error-variant changes (TopicLengthMismatch already exists).

Tests

  • topic_count_is_validated_before_body — over-count via both exact and non-exact iterators (the reported bug).
  • too_few_topics_is_a_length_mismatch — under-count via a non-exact iterator.
  • Valid-log decoding is covered by the existing it_decodes_logs_with_indexed_params.

cargo test -p alloy-dyn-abi → 160 passed; cargo fmt --check clean; clippy --all-features with -D warnings clean.

DynSolEvent::decode_log_parts decoded the body data before fully
validating the topic count. The up-front size_hint check only fires for
exact-size iterators, so a log with a mismatched topic count supplied via
a non-exact iterator (filter/map/streaming) reached the body decode and
failed with a confusing Overrun instead of TopicLengthMismatch.

This happens in practice because ERC-20 and ERC-721 Transfer share the
same topic0: an ERC-721 transfer log (4 topics, empty data) decoded
against the ERC-20 Transfer ABI displaced the body words and overran.

Decode the indexed topics, then check the topic count, then decode the
body. Too-few topics now also report TopicLengthMismatch rather than
silently producing a short indexed list.

Closes alloy-rs/alloy#2243
@satyakwok
satyakwok force-pushed the fix/dyn-abi-event-topic-count-before-body branch from bc0dabf to ff7bc47 Compare June 10, 2026 11:50
@github-project-automation github-project-automation Bot moved this to Reviewed in Alloy Jun 10, 2026
@DaniPopes
DaniPopes enabled auto-merge (squash) June 10, 2026 20:19
@DaniPopes
DaniPopes disabled auto-merge June 10, 2026 20:22
@satyakwok

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the clippy fix — should've run clippy with --all-targets to catch that one. Appreciated!

@satyakwok
satyakwok requested a review from DaniPopes June 10, 2026 21:17
@DaniPopes
DaniPopes merged commit fb67f91 into alloy-rs:main Jun 10, 2026
31 checks passed
@github-project-automation github-project-automation Bot moved this from Reviewed to Done in Alloy Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Bug] Overrun on log decoding

2 participants