From c8ccc84b3b518b0a2bdac28bc56f75a7572f096a Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Tue, 30 Sep 2025 16:59:39 -0700 Subject: [PATCH] [num-utils] add `SetToUintMax()` to prevent type-mismatch bugs (#11969) Introduces a new template helper function `SetToUintMax()` in `common/num_utils.hpp`. This function sets a given unsigned integer variable to its maximum possible value. The `SetToUintMax()` function infers the type of the variable, ensuring that the correct `NumericLimits<...>::kMax` is used. This prevents potential bugs where a variable could be assigned the max value of a wrong `uint` type. Existing code across different modules is updated to use this new helper function, improving code safety and robustness. --- src/core/border_router/br_tracker.cpp | 2 +- src/core/border_router/dhcp6_pd_client.cpp | 2 +- src/core/common/message.cpp | 4 ++-- src/core/common/num_utils.hpp | 14 ++++++++++++++ src/core/net/mdns.cpp | 4 +++- src/core/radio/trel_peer.cpp | 8 ++++++-- src/core/thread/mle_ftd.cpp | 2 +- src/core/utils/ping_sender.hpp | 4 ++-- 8 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/core/border_router/br_tracker.cpp b/src/core/border_router/br_tracker.cpp index 1a3ed3557..c4b9f6636 100644 --- a/src/core/border_router/br_tracker.cpp +++ b/src/core/border_router/br_tracker.cpp @@ -65,7 +65,7 @@ uint16_t NetDataBrTracker::CountBrs(Filter aFilter, uint32_t &aMinAge) const uint32_t uptime = Get().GetUptimeInSeconds(); uint16_t count = 0; - aMinAge = NumericLimits::kMax; + SetToUintMax(aMinAge); for (const BorderRouter &entry : mBorderRouters) { diff --git a/src/core/border_router/dhcp6_pd_client.cpp b/src/core/border_router/dhcp6_pd_client.cpp index 5059da69c..a69e7e900 100644 --- a/src/core/border_router/dhcp6_pd_client.cpp +++ b/src/core/border_router/dhcp6_pd_client.cpp @@ -1337,7 +1337,7 @@ uint16_t Dhcp6PdClient::RetxTracker::DetermineElapsedTime(void) if (mLongElapsedTime) { - elapsed = NumericLimits::kMax; + SetToUintMax(elapsed); ExitNow(); } diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 3c5320de9..b65aca7af 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -163,7 +163,7 @@ uint16_t MessagePool::GetFreeBufferCount(void) const #if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE rval = static_cast(Instance::GetHeap().GetFreeSize() / sizeof(Buffer)); #else - rval = NumericLimits::kMax; + SetToUintMax(rval); #endif #elif OPENTHREAD_CONFIG_PLATFORM_MESSAGE_MANAGEMENT rval = otPlatMessagePoolNumFreeBuffers(&GetInstance()); @@ -182,7 +182,7 @@ uint16_t MessagePool::GetTotalBufferCount(void) const #if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE rval = static_cast(Instance::GetHeap().GetCapacity() / sizeof(Buffer)); #else - rval = NumericLimits::kMax; + SetToUintMax(rval); #endif #else rval = OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS; diff --git a/src/core/common/num_utils.hpp b/src/core/common/num_utils.hpp index f2500290e..99104388e 100644 --- a/src/core/common/num_utils.hpp +++ b/src/core/common/num_utils.hpp @@ -144,6 +144,20 @@ template int8_t ClampToInt8(IntType aValue) static_cast(NumericLimits::kMax))); } +/** + * Sets a given unsigned integer variable to its maximum possible value. + * + * @tparam UintType The unsigned integer type. + * + * @param[out] aVariable A reference to the variable to set to its max possible value. + */ +template void SetToUintMax(UintType &aVariable) +{ + static_assert(TypeTraits::IsUint::kValue, "UintType must be an unsigned int (8, 16, 32, or 64 bit len)"); + + aVariable = NumericLimits::kMax; +} + /** * Indicates whether or not the addition of two unsigned integers will result in an overflow. * diff --git a/src/core/net/mdns.cpp b/src/core/net/mdns.cpp index f83e9ba74..bbece3317 100644 --- a/src/core/net/mdns.cpp +++ b/src/core/net/mdns.cpp @@ -1051,7 +1051,9 @@ exit: uint32_t Core::RecordInfo::GetDurationSinceLastMulticast(TimeMilli aTime) const { - uint32_t duration = NumericLimits::kMax; + uint32_t duration; + + SetToUintMax(duration); VerifyOrExit(mIsPresent && mIsLastMulticastValid); VerifyOrExit(aTime > mLastMulticastTime, duration = 0); diff --git a/src/core/radio/trel_peer.cpp b/src/core/radio/trel_peer.cpp index 0e76f04d9..c99e319c9 100644 --- a/src/core/radio/trel_peer.cpp +++ b/src/core/radio/trel_peer.cpp @@ -134,9 +134,11 @@ uint32_t Peer::DetermineExpirationDelay(uint32_t aUptimeNow) const // `uint32_t` max value, indicating that the peer is not // considered as expired. - uint32_t delay = NumericLimits::kMax; + uint32_t delay; uint32_t expireTime; + SetToUintMax(delay); + VerifyOrExit(mDnssdState == kDnssdRemoved); expireTime = mLastInteractionTime + kExpirationDelay; @@ -462,7 +464,9 @@ exit: void PeerTable::HandleTimer(void) { uint32_t uptimeNow = Get().GetUptimeInSeconds(); - uint32_t delay = NumericLimits::kMax; + uint32_t delay; + + SetToUintMax(delay); RemoveAndFreeAllMatching(Peer::ExpireChecker(uptimeNow)); diff --git a/src/core/thread/mle_ftd.cpp b/src/core/thread/mle_ftd.cpp index 7b08cf1be..7d39ae365 100644 --- a/src/core/thread/mle_ftd.cpp +++ b/src/core/thread/mle_ftd.cpp @@ -3510,7 +3510,7 @@ Error Mle::AddrSolicitInfo::ParseFrom(const Coap::Message &aMessage) case kErrorNone: break; case kErrorNotFound: - mXtalAccuracy = NumericLimits::kMax; + SetToUintMax(mXtalAccuracy); break; default: ExitNow(error = kErrorParse); diff --git a/src/core/utils/ping_sender.hpp b/src/core/utils/ping_sender.hpp index cd42f4f30..7f66ba81d 100644 --- a/src/core/utils/ping_sender.hpp +++ b/src/core/utils/ping_sender.hpp @@ -77,9 +77,9 @@ public: mSentCount = 0; mReceivedCount = 0; mTotalRoundTripTime = 0; - mMinRoundTripTime = NumericLimits::kMax; - mMaxRoundTripTime = NumericLimits::kMin; + mMaxRoundTripTime = 0; mIsMulticast = false; + SetToUintMax(mMinRoundTripTime); } };