code-423n4 / 2021-09-defiprotocol-findings

1 stars 0 forks source link

Rearranging state variable declaration order will allow slot packing to save gas #148

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

0xRajeev

Vulnerability details

Impact

Rearranging the declaration order of state variables in Auction.sol by moving all 3 bool's next to each other will pack them in one slot because bool’s are represented as uint8 internally. This will save slots and gas if they're used together in functions by allowing optimizer to combine their SLOADs/SSTOREs. If they are not used together, this may increase gas costs due to the required masking of shared slot variables.

Proof of Concept

https://github.com/code-423n4/2021-09-defiProtocol/blob/52b74824c42acbcd64248f68c40128fe3a82caf6/contracts/contracts/Auction.sol#L16-L21

Tools Used

Manual Analysis

Recommended Mitigation Steps

Rearrange state variable declaration order, by considering their usage in functions, to allow slot packing and save gas.

GalloDaSballo commented 2 years ago

Duplicate of #109