[link-quality] prevent overflow in LqiAverager calculation (#9876)

This commit updates `LqiAverager::Add()` to explicitly use a `uint16_t`
local variable for calculating the new running average. This prevents
potential overflows.
This commit is contained in:
Abtin Keshavarzian
2024-02-26 18:37:46 -08:00
committed by GitHub
parent ac417bdee7
commit aff159f40c
+6 -2
View File
@@ -117,7 +117,8 @@ exit:
void LqiAverager::Add(uint8_t aLqi)
{
uint8_t count;
uint8_t count;
uint16_t newAverage;
if (mCount < NumericLimits<uint8_t>::kMax)
{
@@ -126,7 +127,10 @@ void LqiAverager::Add(uint8_t aLqi)
count = Min(static_cast<uint8_t>(1 << kCoeffBitShift), mCount);
mAverage = static_cast<uint8_t>(((mAverage * (count - 1)) + aLqi) / count);
newAverage = mAverage;
newAverage = (newAverage * (count - 1) + aLqi) / count;
mAverage = static_cast<uint8_t>(newAverage);
}
void LinkQualityInfo::Clear(void)