The function Statistics::variance calculates the variance of a dataset. However, the current implementation is prone to a revert error when attempting to compute the difference between each data point and the mean, particularly for values less than the mean. This happens because of underflow in the subtraction operation.
In the current implementation, when calculating variance, the function iterates over each data point in the array and calculates the difference between the data point and the mean. If a value in data is less than the mean, subtracting mean from that value results in a negative number, which causes an underflow in unsigned integer arithmetic, leading to a revert.
Example Scenario:
Input array: [1, 2, 3, 4, 5]
The mean is calculated as 3.
When the first element 1 is processed, the function attempts to compute 1 - 3, which results in an underflow for uint256 and causes the function to revert.
This vulnerability effectively breaks the variance function for any input where one or more data points are less than the mean, making the function unreliable and unusable for many typical datasets.
Manual Review
Make the following changes:
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.