sherlock-audit / 2024-05-pooltogether-judging

10 stars 6 forks source link

Arithmetics #165

Closed sherlock-admin4 closed 4 months ago

sherlock-admin4 commented 4 months ago

Arithmetics

Low/Info issue submitted by 0x1ns4n3

Summary

Vulnerability Detail

++i costs less gas compared to i++ or i += 1 ++i costs less gas compared to i++ or i += 1 for unsigned integer, as pre-increment is cheaper (about 5 gas iteration). This statement is true even with the optimizer enabled. i++ increments i and retuns the initial value of i. Which means:

uint i = 1;
i++;  // == 1 but i == 2

But ++i returns the actual incremented value:

uint i = 1;
++i; // == 2 and i == 2 too, so no need for a temporary variable

In the first case, the compiler has to create a temporary variable (when used) for returning 1 instead of 2

Impact

More Gase is used than necessary. On ethereum chains, it is around $1 worth of gas per instance per transaction

Code Snippet

https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L132 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L158 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L160 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L164 https://github.com/sherlock-audit/2024-05-pooltogether/blob/main/pt-v5-claimer/src/Claimer.sol#L230

Tool used

Manual Review

Recommendation

I suggest using ++i instead of i++ to increment the value of a uint variable.