DeFiFoundry
50,000 USDC
View results
Submission Details
Severity: low
Invalid

Unchecked Math in _absDiff()

Summary

The _absDiff function in KeeperProxy can be optimized using unchecked math since the conditional logic already prevents underflows, leading to gas savings without compromising safety.

Technical Analysis

Current implementation:

function _absDiff(uint256 a, uint256 b) internal pure returns (uint256) {
if (a > b) return a - b;
else return b - a;
}

The function is safe by design because:

  1. Conditional check ensures larger number is always subtracted from

  2. Both inputs are uint256, preventing overflow scenarios

  3. Function is used only in price validation contexts

Impact

Severity: Gas Optimization (Low)

Effects:

  • Gas savings: ~40-100 per operation

  • No security implications

  • Cumulative savings with frequent price checks

Test Cases

contract KeeperProxyTest {
function testAbsDiffGasOptimization() public {
uint256 a = 1000;
uint256 b = 500;
// Test original
uint256 gasStart = gasleft();
uint256 result1 = keeper._absDiff(a, b);
uint256 gasUsed1 = gasStart - gasleft();
// Test optimized
gasStart = gasleft();
uint256 result2 = keeper._absDiffOptimized(a, b);
uint256 gasUsed2 = gasStart - gasleft();
assertEq(result1, result2);
assertLt(gasUsed2, gasUsed1);
}
}

Recommendation

function _absDiff(uint256 a, uint256 b) internal pure returns (uint256) {
unchecked {
return a > b ? a - b : b - a;
}
}

Gas Savings Breakdown

Operation Before After Savings
Single Call ~150 gas ~50 gas ~100 gas
Price Check (3 tokens) ~450 gas ~150 gas ~300 gas
Updates

Lead Judging Commences

n0kto Lead Judge 5 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Informational or Gas

Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelihood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.