The _abs function in the PerpDepository.sol is useless since there are no negative numbers in Solidity
Summary
In solidity we have only unsigned integers which means we can't have negative numbers, there is no reason to convert a number to it's "Absolute" version, since it could either be 0 or greater than 0, it can't be negative (-).
Vulnerability Detail
The _abs function in PerpDepository.sol is supposed to return the "absolute" value of a number. It's useless and doesn't make sense because we don't have negative numbers in solidity.
In case the input number is 0 it returns uint256(-1 * value), which will revert since you can't use negative numbers in solidity.
_abs is used in multiple places in the contract, which wastes a lot of gas, in addition if the value that will be sent to the function will be 0, it will be reverted due to the usage of -1.
_abs(0) ==> always reverts.
Impact
Dos for the contract when _abs(0), and a lot of wasted gas anytime we call this function.
JohnnyTime
medium
The
_abs
function in thePerpDepository.sol
is useless since there are no negative numbers in SoliditySummary
In solidity we have only unsigned integers which means we can't have negative numbers, there is no reason to convert a number to it's "Absolute" version, since it could either be 0 or greater than 0, it can't be negative (-).
Vulnerability Detail
The
_abs
function inPerpDepository.sol
is supposed to return the "absolute" value of a number. It's useless and doesn't make sense because we don't have negative numbers in solidity. In case the input number is 0 it returns uint256(-1 * value), which will revert since you can't use negative numbers in solidity._abs
is used in multiple places in the contract, which wastes a lot of gas, in addition if the value that will be sent to the function will be 0, it will be reverted due to the usage of -1._abs(0)
==> always reverts.Impact
Dos for the contract when
_abs(0)
, and a lot of wasted gas anytime we call this function.Code Snippet
https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L821-L823 https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L521 https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L684 https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L712 https://github.com/sherlock-audit/2023-01-uxd/blob/main/contracts/integrations/perp/PerpDepository.sol#L781
Tool used
Manual Review
Recommendation
Get rid of the
_abs
function.