The Statistics library's avg()
function performs division using array length without validating empty arrays:
The function is used for calculating averages of numerical data, but fails abruptly when given empty arrays instead of handling this edge case gracefully.
The avg()
function fails to check if the input array is empty before performing division:
https://github.com/Cyfrin/2024-10-swan-dria/blob/main/contracts/libraries/Statistics.sol#L8
When an empty array is passed:
data.length = 0
Loop is skipped (sum stays 0)
Final division becomes 0 / 0
Transaction reverts due to division by zero error
This creates unexpected transaction failures when input validation is missing, particularly problematic in contract interactions where empty arrays might be valid business cases.
A proper input validation check would prevent this runtime error.
Simple, explicit check ensures function fails early with clear error message instead of runtime division error. Protocols can handle this error case appropriately.
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.