sherlock-audit/2024-04-teller-finance-judging

Edge Case for abs function in LenderCommitmentGroup_Smart

Closed this issue · 0 comments

Edge Case for abs function in LenderCommitmentGroup_Smart

Low/Info issue submitted by BengalCatBalu

Summary

Edge case in abs function

Vulnerability Detail

Abs function will revert if x = type(int256).min, так как abs(int256.min) = abs(int256.max) + 1

Impact

I have not found that this case is achievable with the protocol work
Low.

Code Snippet

function abs(int x) private pure returns (uint) {
        return x >= 0 ? uint(x) : uint(-x);
    }

Tool used

Manual Review

Recommendation

Use

function abs(int256 n) internal pure returns (uint256) {
        unchecked {
            // Formula from the "Bit Twiddling Hacks" by Sean Eron Anderson.
            // Since `n` is a signed integer, the generated bytecode will use the SAR opcode to perform the right shift,
            // taking advantage of the most significant (or "sign" bit) in two's complement representation.
            // This opcode adds new most significant bits set to the value of the previous most significant bit. As a result,
            // the mask will either be `bytes(0)` (if n is positive) or `~bytes32(0)` (if n is negative).
            int256 mask = n >> 255;

            // A `bytes(0)` mask leaves the input unchanged, while a `~bytes32(0)` mask complements it.
            return uint256((n + mask) ^ mask);
        }
    }