Statistics::sqrt function reverts when the input is type(uint256).max due to an integer overflow.
When the input to the Statistics::sqrt function is type(uint256).max, the following line in the function causes an integer overflow:
https://github.com/Cyfrin/2024-10-swan-dria/blob/main/contracts/libraries/Statistics.sol#L40-L47
x + 1 overflows when x is the maximum uint256 value.
Steps to reproduce:
Inside the 2024-10-swan-dria directory:
Run npm i --save-dev @nomicfoundation/hardhat-foundry to install the hardhat-foundry plugin.
Add require("@nomicfoundation/hardhat-foundry"); to the top of the hardhat.config.js file.
Run npx hardhat init-foundry in the terminal. This will generate a foundry.toml file based on the Hardhat project’s existing configuration, and will install the forge-std library.
Create a file ("StatisticsTest.t.sol") in the ./test/ directory.
Copy the provided test case into the file.
Run forge test --mt testFuzzUniSqrt in the terminal
Test logs:
The Statistics::sqrt function reverts when the input is type(uint256).max.
Statistics::stddev function calls the sqrt function. If the sqrt function overflows, stddev will revert.
Foundry Fuzz Test
Consider using the UniSwap's sqrt function as a reference to update the Statistics::sqrt function to handle the edge case when the input is type(uint256).max, or using Solmate FixedPointMathLib::sqrt implementation.
https://github.com/transmissions11/solmate/blob/main/src/utils/FixedPointMathLib.sol#L164
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.