Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

fix: off by one error during log topic decoding #296

Merged
merged 1 commit into from
May 24, 2021

Conversation

mattsse
Copy link
Collaborator

@mattsse mattsse commented May 22, 2021

Motivation

Fixes a bug, that is essentially an off by one error, that resulted in errors when decoding events with no indexed entries.

Solution

Remove the bad is_empty requirement and test against real log.

@@ -432,7 +432,7 @@ fn derive_decode_from_log_impl(
let flat_topics = topics.iter().skip(1).flat_map(|t| t.as_ref().to_vec()).collect::<Vec<u8>>();
},
quote! {
if topic_tokens.is_empty() || topic_tokens.len() != topics.len() - 1 {
if topic_tokens.len() != topics.len() - 1 {
Copy link
Collaborator

@prestwich prestwich May 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this subtraction underflow if the LOG0 opcode is used? I'm not super familiar with these codepaths 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro generates different code for anonymous events:

let (signature_check, flat_topics_init, topic_tokens_len_check) = if event.anonymous {
(
quote! {},
quote! {
let flat_topics = topics.iter().flat_map(|t| t.as_ref().to_vec()).collect::<Vec<u8>>();
},
quote! {
if topic_tokens.len() != topics.len() {
return Err(ethers_core::abi::Error::InvalidData);
}
},
)
} else {
(
quote! {
let event_signature = topics.get(0).ok_or(ethers_core::abi::Error::InvalidData)?;
if event_signature != &Self::signature() {
return Err(ethers_core::abi::Error::InvalidData);
}
},
quote! {
let flat_topics = topics.iter().skip(1).flat_map(|t| t.as_ref().to_vec()).collect::<Vec<u8>>();
},
quote! {
if topic_tokens.len() != topics.len() - 1 {
return Err(ethers_core::abi::Error::InvalidData);
}
},
)
};

only for non anonymous it makes sure that the signature is the first entry in topics

let event_signature = topics.get(0).ok_or(ethers_core::abi::Error::InvalidData)?;

so topics.len() - 1 can't underflow.

Most of this is adapted from:
https://github.com/rust-ethereum/ethabi/blob/970d47018e0f457440851752a7b46a4dbf793a0b/ethabi/src/event.rs#L122-L173

@gakonst gakonst merged commit ac6d4d7 into gakonst:master May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants