BriVault

First Flight #52
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Impact: low
Likelihood: low
Invalid

Missing array length caching in setCountry() loop

The for loop take much gas due to everytime i reads from the storage.

Description

The function iterates through the countries array using countries.length directly inside the loop condition.
However, it does not cache the array length in a local variable (e.g., uint256 len = countries.length;).


function setCountry(string[48] memory countries) public onlyOwner {
@> //uint256 length = countries.length;
for (uint256 i = 0; i < countries.length; ++i) {
teams[i] = countries[i];
}
emit CountriesSet(countries);
}

Risk

Impact:

Each loop iteration re-reads countries.length from memory.

  • Slight increase in gas cost (minor but measurable).

  • Reduces clarity and consistency in loop structure.

Recommended Mitigation

This makes the loop slightly cheaper and improves code clarity.

+ function setCountry(string[48] memory countries) public onlyOwner {
+ Uint256 length = countries.length;
- for (uint256 i = 0; i < countries.length; ++i) {
+ for (uint256 i = 0; i < length; ++i) {
teams[i] = countries[i];
}
emit CountriesSet(countries);
}
Updates

Appeal created

bube Lead Judge 19 days ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Gas optimizations

Gas optimizations are invalid according to the CodeHawks documentation.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!