Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache array length in for loops #106

Open
code423n4 opened this issue Nov 29, 2021 · 1 comment
Open

Cache array length in for loops #106

code423n4 opened this issue Nov 29, 2021 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

pmerkleplant

Vulnerability details

Impact

Caching the array length in a for-loop saves gas as the length does not need
to be read on every iteration.

The following loops could be refactored:

./Malt.sol:34:    for (uint256 i = 0; i < minters.length; i = i + 1) {
./Malt.sol:37:    for (uint256 i = 0; i < burners.length; i = i + 1) {
./TransferService.sol:87:    for (uint i = 0; i < verifierList.length - 1; i = i + 1) {
./Auction.sol:407:    for (uint i = 0; i < epochCommitments.length; ++i) {
./libraries/UniswapV2Library.sol:66:        for (uint i; i < path.length - 1; i++) {
./AuctionParticipant.sol:107:    for (uint256 i = replenishingIndex; i < auctionIds.length; i = i + 1) {
./MiningService.sol:49:    for (uint i = 0; i < mines.length; i = i + 1) {
./MiningService.sol:69:    for (uint i = 0; i < mines.length; i = i + 1) {
./MiningService.sol:86:    for (uint i = 0; i < mines.length; i = i + 1) {
./MiningService.sol:96:    for (uint i = 0; i < mines.length; i = i + 1) {
./MiningService.sol:142:    for (uint i = 0; i < mines.length - 1; i = i + 1) {
./MiningService.sol:166:    for (uint i = 0; i < mines.length; i = i + 1) {
./DexHandlers/UniswapHandler.sol:317:    for (uint i = 0; i < buyers.length - 1; i = i + 1) {

Tools used

grep -rn ".length" .

@GalloDaSballo
Copy link
Collaborator

Finding is valid, see #136 for a pretty exhaustive explanation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants