mirror of
https://github.com/espressif/openthread.git
synced 2026-08-04 09:57:47 +00:00
[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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user