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

Loops can be implemented more efficiently #157

Open
code423n4 opened this issue Dec 15, 2021 · 0 comments
Open

Loops can be implemented more efficiently #157

code423n4 opened this issue Dec 15, 2021 · 0 comments
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

0x0x0x

Vulnerability details

Proof of Concept

Example:


for (uint i = 0; i < arr.length; i++) {

//Operations not effecting the length of the array.

}

Loading length of array costs gas. Therefore, the length should be cached, if the length of the array doesn't change inside the loop. Furthermore, there is no need to assign the initial value 0. This costs extra gas.

Recommended implementation:


uint length = arr.length;

for (uint i; i < length; ++i) {

//Operations not effecting the length of the array.

}

By doing so the length is only loaded once rather than loading it as many times as iterations (Therefore, less gas is spent).

Occurences


./CreditLine/CreditLine.sol:484:        for (uint256 _index = 0; _index < _strategyList.length; _index++) {
./CreditLine/CreditLine.sol:662:        for (uint256 _index = 0; _index < _strategyList.length; _index++) {
./CreditLine/CreditLine.sol:738:        for (uint256 _index = 0; _index < _strategyList.length; _index++) {
./CreditLine/CreditLine.sol:892:        for (uint256 index = 0; index < _strategyList.length; index++) {
./CreditLine/CreditLine.sol:959:        for (uint256 index = 0; index < _strategyList.length; index++) {
./SavingsAccount/SavingsAccount.sol:289:        for (uint256 i = 0; i < _strategyList.length; i++) {
./SavingsAccount/SavingsAccount.sol:467:        for (uint256 i = 0; i < _strategyList.length; i++) {

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Dec 15, 2021
code423n4 added a commit that referenced this issue Dec 15, 2021
@ritik99 ritik99 added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Dec 25, 2021
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

2 participants