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

Increments can be unchecked #43

Open
codehawks-bot opened this issue Aug 7, 2023 · 0 comments
Open

Increments can be unchecked #43

codehawks-bot opened this issue Aug 7, 2023 · 0 comments

Comments

@codehawks-bot
Copy link

Increments can be unchecked

Severity

Gas Optimization

Relevant GitHub Links

ethereum/solidity#10695

Summary

In Solidity 0.8+, there’s a default overflow check on unsigned integers. It’s possible to uncheck this in for-loops and save some gas at each iteration, but at the cost of some code readability, as this uncheck cannot be made inline.

Vulnerability Details

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Lender.sol

Instances included:

Lender.borrow() #232      for (uint256 i = 0; i < borrows.length; i++) {

Lender.repay() #292      for (uint256 i = 0; i < loanIds.length; i++) {

Lender.giveLoan() #355      for (uint256 i = 0; i < loanIds.length; i++) {

Lender.startAuction() #437      for (uint256 i = 0; i < loanIds.length; i++) {

Lender.seizeLoan() #548      for (uint256 i = 0; i < loanIds.length; i++) {

Lender.refinance() #591      for (uint256 i = 0; i < refinances.length; i++) {

Tools Used

Manual

Recommendations

The code would go from:

for (uint256 i = 0; i < [].length; i++) {  
 // ...  
}  

to

for (uint256 i = 0; i < [].length;) {  
 // ...  
 unchecked { i++; }  
}  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants