[csl] fix overflow in calculating semiWindow (#6734)

Regarding:
semiWindow = elapsed * (Get<Radio>().GetCslAccuracy() + mCslParentDrift) / 1000000;

Consider that the worst Get<Radio>().GetCslAccuracy() and
mCslParentDrift are both 255, the max of uint32 is 4294967295, so when
elapsed is about 8500000(just 8.5s elapsed since the last CSL sync),
the product of elapsed and (Get<Radio>().GetCslAccuracy() +
mCslParentDrift) will overflow.

This commit changes the type to uint64_t from uint32_t to avoid the
overflow.
This commit is contained in:
Zhangwx
2021-06-17 10:11:37 -07:00
committed by GitHub
parent 649e8e1756
commit dfad64ea14
+5 -4
View File
@@ -1058,18 +1058,19 @@ void SubMac::GetCslWindowEdges(uint32_t &ahead, uint32_t &after)
{
uint32_t semiPeriod = mCslPeriod * kUsPerTenSymbols / 2;
uint64_t curTime = otPlatRadioGetNow(&GetInstance());
uint32_t elapsed, semiWindow;
uint64_t elapsed;
uint32_t semiWindow;
if (mCslLastSync.GetValue() > curTime)
{
elapsed = static_cast<uint32_t>(UINT64_MAX - mCslLastSync.GetValue() + curTime);
elapsed = UINT64_MAX - mCslLastSync.GetValue() + curTime;
}
else
{
elapsed = static_cast<uint32_t>(curTime - mCslLastSync.GetValue());
elapsed = curTime - mCslLastSync.GetValue();
}
semiWindow = elapsed * (Get<Radio>().GetCslAccuracy() + mCslParentDrift) / 1000000;
semiWindow = static_cast<uint32_t>(elapsed * (Get<Radio>().GetCslAccuracy() + mCslParentDrift) / 1000000);
semiWindow += mCslParentUncert * kUsPerUncertUnit;
ahead = (semiWindow + kCslReceiveTimeAhead > semiPeriod) ? semiPeriod : semiWindow + kCslReceiveTimeAhead;