BriVault

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

Gas Optimization in setCountry() Loop

Caching Array Length Before Loop in setCountry() Saves Gas

Description

  • The setCountry() function iterates over the input array countries. Reading the length of an array is an SSTORE operation in memory, and doing this on every iteration (countries.length) adds unnecessary overhead.

    The gas cost can be reduced by caching the length of the array in a local variable before the loop starts.

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

Risk

Likelihood:

  • Reason 1: The gas cost is incurred every time the function is called because it reads from state.



Impact:

Minor (Gas)

Gas Overspending: Leads to higher transaction costs for the owner when setting up the tournament, especially given the large array size (48 elements).

Proof of Concept

Recommended Mitigation

Cache the array length outside the loop using a local variable.

function setCountry(string[48] memory countries) public onlyOwner {
+ uint256 countriesLength = countries.length;
- for (uint256 i = 0; i < countries.length; ++i) {
+ for (uint256 i = 0; i < countriesLength; ++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!