Skip to content

rlp: validate and cache element count in RawList#33840

Merged
fjl merged 6 commits into
ethereum:masterfrom
fjl:rlp-rawlist-verify-len
Feb 13, 2026
Merged

rlp: validate and cache element count in RawList#33840
fjl merged 6 commits into
ethereum:masterfrom
fjl:rlp-rawlist-verify-len

Conversation

@fjl
Copy link
Copy Markdown
Contributor

@fjl fjl commented Feb 13, 2026

This changes RawList to ensure the count of items is always valid. Lists with invalid structure, i.e. ones where an element exceeds the size of the container, are now detected during decoding of the RawList and thus cannot exist.

Also remove RawList.Empty since it is now fully redundant, and Iterator.Count since it returns incorrect results in the presence of invalid input. There are no callers of these methods (yet).

fjl added 2 commits February 13, 2026 20:17
This changes the RawList to ensure the count of items is always valid. Lists with invalid
structure, i.e. ones where an element exceeds the size of the container, are now detected
during decoding of the RawList.
Comment thread rlp/raw.go Outdated
fjl added 2 commits February 13, 2026 21:07
This was introduced together with RawList, but it's slightly unsafe since CountValues
returns zero upon encountering an invalid item. This means the invariant documented on
Count, that it provides an upper bound on the number of visited items, is not true.

Since there are no callers of Count (yet), it's best to just remove it.
Comment thread rlp/raw.go Outdated
Copy link
Copy Markdown
Contributor

@jrhea jrhea left a comment

Choose a reason for hiding this comment

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

lgtm

@fjl fjl added this to the 1.17.0 milestone Feb 13, 2026
@fjl fjl merged commit 4f38a76 into ethereum:master Feb 13, 2026
7 of 8 checks passed
fjl added a commit to fjl/go-ethereum that referenced this pull request Feb 13, 2026
I removed Iterator.Count in ethereum#33840, because it appeared to be unused
and did not provide the documented invariant: the returned Count should
always be an upper bound on the number of iterations allowed by Next().

In order to make Count work, the semantics of CountValues has to change to return the
number of items up and including the invalid one. I have reviewed all callsites of
CountValues to assess if changing this is safe. There aren't that many, and the only call
that doesn't check the error and return is in the trie node parser, trie.decodeNodeUnsafe.
There, we distinguish the node type based on the number of items, and it previously
returned an error for item count zero. In order to avoid any potential issue that could
result from this change, I'm adding an error check in that function.
fjl added a commit that referenced this pull request Feb 13, 2026
I removed `Iterator.Count` in #33840, because it appeared to be unused
and did not provide the documented invariant: the returned count should
always be an upper bound on the number of iterations allowed by `Next`.

In order to make `Count` work, the semantics of `CountValues` has to
change to return the number of items up and including the invalid one. I
have reviewed all callsites of `CountValues` to assess if changing this
is safe. There aren't that many, and the only call that doesn't check
the error and return is in the trie node parser,
`trie.decodeNodeUnsafe`. There, we distinguish the node type based on
the number of items, and it previously returned an error for item count
zero. In order to avoid any potential issue that could result from this
change, I'm adding an error check in that function, though it isn't
necessary.
sduchesneau pushed a commit to streamingfast/go-ethereum that referenced this pull request Apr 7, 2026
This changes `RawList` to ensure the count of items is always valid.
Lists with invalid structure, i.e. ones where an element exceeds the
size of the container, are now detected during decoding of the `RawList`
and thus cannot exist.

Also remove `RawList.Empty` since it is now fully redundant, and
`Iterator.Count` since it returns incorrect results in the presence of
invalid input. There are no callers of these methods (yet).
sduchesneau pushed a commit to streamingfast/go-ethereum that referenced this pull request Apr 7, 2026
I removed `Iterator.Count` in ethereum#33840, because it appeared to be unused
and did not provide the documented invariant: the returned count should
always be an upper bound on the number of iterations allowed by `Next`.

In order to make `Count` work, the semantics of `CountValues` has to
change to return the number of items up and including the invalid one. I
have reviewed all callsites of `CountValues` to assess if changing this
is safe. There aren't that many, and the only call that doesn't check
the error and return is in the trie node parser,
`trie.decodeNodeUnsafe`. There, we distinguish the node type based on
the number of items, and it previously returned an error for item count
zero. In order to avoid any potential issue that could result from this
change, I'm adding an error check in that function, though it isn't
necessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants