From dfad64ea14d02d1cda030d849e0eec7528694a37 Mon Sep 17 00:00:00 2001 From: Zhangwx Date: Fri, 18 Jun 2021 01:11:37 +0800 Subject: [PATCH] [csl] fix overflow in calculating semiWindow (#6734) Regarding: semiWindow = elapsed * (Get().GetCslAccuracy() + mCslParentDrift) / 1000000; Consider that the worst Get().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().GetCslAccuracy() + mCslParentDrift) will overflow. This commit changes the type to uint64_t from uint32_t to avoid the overflow. --- src/core/mac/sub_mac.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/core/mac/sub_mac.cpp b/src/core/mac/sub_mac.cpp index 750c8d5fe..1c788de59 100644 --- a/src/core/mac/sub_mac.cpp +++ b/src/core/mac/sub_mac.cpp @@ -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(UINT64_MAX - mCslLastSync.GetValue() + curTime); + elapsed = UINT64_MAX - mCslLastSync.GetValue() + curTime; } else { - elapsed = static_cast(curTime - mCslLastSync.GetValue()); + elapsed = curTime - mCslLastSync.GetValue(); } - semiWindow = elapsed * (Get().GetCslAccuracy() + mCslParentDrift) / 1000000; + semiWindow = static_cast(elapsed * (Get().GetCslAccuracy() + mCslParentDrift) / 1000000); semiWindow += mCslParentUncert * kUsPerUncertUnit; ahead = (semiWindow + kCslReceiveTimeAhead > semiPeriod) ? semiPeriod : semiWindow + kCslReceiveTimeAhead;