code-423n4 / 2022-01-trader-joe-findings

2 stars 0 forks source link

Cache storage variables #297

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

Czar102

Vulnerability details

Impact

Storage variables are often read more than once, despite no change in their value.

Example

    function deposit(uint256 _amount) external {
        UserInfo storage user = userInfo[msg.sender];

        updatePool();

        if (user.amount > 0) {
            uint256 pending = (user.amount * accRJoePerShare) /
                PRECISION -
                user.rewardDebt;
            _safeRJoeTransfer(msg.sender, pending);
        }
        user.amount = user.amount + _amount;
        user.rewardDebt = (user.amount * accRJoePerShare) / PRECISION;

        joe.safeTransferFrom(address(msg.sender), address(this), _amount);
        emit Deposit(msg.sender, _amount);
    }

In the above example, user.amount is read 4 times, while it could have been read only once.

Tools Used

Manual analysis

Recommended Mitigation Steps

Cache unchanging storage variables on the stack.

cryptofish7 commented 2 years ago

Duplicate of #128