There are division-by-zero vulnerability in the Statistics
library when calculating the mean and variance of an array. Specifically, if an empty array is passed to either avg
or variance
functions, a division by zero occurs, leading to a contract revert. This vulnerability affects the stability of any contracts utilizing the library to perform statistical calculations on potentially empty datasets.
The vulnerability exists in two functions, avg
and variance
, due to the lack of checks for an empty array. Both functions divide by the length of the array, assuming it is non-zero. When the input array is empty, data.length
equals zero, resulting in a division by zero that causes a revert.
The avg
function calculates the average by dividing the sum of the elements by data.length
:
https://github.com/Cyfrin/2024-10-swan-dria/blob/c8686b199daadcef3161980022e12b66a5304f8e/contracts/libraries/Statistics.sol#L8-L14
Similarly, in the variance
function, the calculation divides by data.length
, causing a division by zero when data
is empty.
We can create a test on Hardhat:
Output:
A division-by-zero vulnerability results in a contract revert, disrupting the functionality of any contract that depends on the Statistics
library for calculations. It also introduces the risk of denial-of-service, where an empty array input inadvertently prevents further execution of the contract.
Manual review.
To address this issue, add a check in each affected function to ensure that data.length > 0
before performing any division. If the array is empty, handle the condition accordingly (e.g., revert with a custom error message).
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.