fix: improve leaf index validation#227
Closed
henriquemarlon wants to merge 2 commits intocartesi:mainfrom
Closed
Conversation
ZzzzHui
reviewed
Dec 11, 2025
guidanoli
reviewed
Dec 11, 2025
Collaborator
There was a problem hiding this comment.
These changes seem to pertain more to your other PR #226.
My suggestion is, now that you have write permissions to the repo:
-
Address all suggested changes.
-
Rebase
fix/integer-overflowon top offix/dockerized-env-tests
git checkout fix/integer-overflow
git rebase -i fix/dockerized-env-tests # Might require conflicts to be resolved- Push your branches from your fork to the upstream
daverepo and make these PRs point to them.
git remote add upstream https://github.com/cartesi/dave.git
git fetch upstream
git push upstream fix/dockerized-env-tests
git push upstream fix/integer-overflow-
Make your PRs point to the new branches on the
daverepo (not to your fork branches) -
Make this PR point have
fix/dockerized-env-testsas its base branch
| apt-get update && apt-get install -y --no-install-recommends curl ca-certificates git | ||
| curl -L https://foundry.paradigm.xyz | bash | ||
| ~/.foundry/bin/foundryup --install stable | ||
| ~/.foundry/bin/foundryup --install v1.5.0 |
Collaborator
There was a problem hiding this comment.
When bumping Foundry, we should make sure to:
- update the Foundry version on the CI config.
- run the Forge formatter on all Forge projects.
472446b to
05b80e8
Compare
Collaborator
|
Superseded by #236. :-) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With the stronger fuzzer introduced in Foundry v1.5.0, a new failing fuzz case was detected in:
Merkle.getHashOfLeafAtIndex(bytes calldata data, uint256 leafIndex)The issue is caused by this line:
uint256 start = leafIndex << MerkleConstants.LOG2_LEAF_SIZE;For very large leafIndex values, this shift can silently wrap around modulo
2^256, making start small again. This may cause the condition start < data.length to incorrectly pass, leading to an invalid hash being returned instead of the pristine leaf.Although this does not affect deployed contracts (where leafIndex is safely bounded), it breaks fuzz tests on Foundry v1.5.0.
This PR adds a reversible shift check to prevent wrap-around:
if (start < data.length && (start >> MerkleConstants.LOG2_LEAF_SIZE) == leafIndex)This guarantees that the shift did not overflow and restores correct behavior under fuzzing.