From 0e42d0b8c9cb6464a68ee6aa8a0d9ae10f68f4d2 Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Fri, 4 Jan 2019 01:16:04 +0800 Subject: [PATCH] [mac] remove redundant checks (#3408) --- src/core/mac/channel_mask.hpp | 22 +++++++++++++++++++--- src/core/mac/mac.cpp | 2 -- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/core/mac/channel_mask.hpp b/src/core/mac/channel_mask.hpp index b5189792e..584675315 100644 --- a/src/core/mac/channel_mask.hpp +++ b/src/core/mac/channel_mask.hpp @@ -36,6 +36,7 @@ #include "openthread-core-config.h" +#include #include #include "common/string.hpp" @@ -140,7 +141,10 @@ public: * @returns TRUE if the channel @p aChannel is included in the mask, FALSE otherwise. * */ - bool ContainsChannel(uint8_t aChannel) const { return ((1UL << aChannel) & mMask) != 0; } + bool ContainsChannel(uint8_t aChannel) const + { + return (aChannel < sizeof(mMask) * CHAR_BIT) ? ((1UL << aChannel) & mMask) != 0 : false; + } /** * This method adds a channel to the channel mask. @@ -148,7 +152,13 @@ public: * @param[in] aChannel A channel * */ - void AddChannel(uint8_t aChannel) { mMask |= (1UL << aChannel); } + void AddChannel(uint8_t aChannel) + { + if (aChannel < sizeof(mMask) * CHAR_BIT) + { + mMask |= (1UL << aChannel); + } + } /** * This method removes a channel from the channel mask. @@ -156,7 +166,13 @@ public: * @param[in] aChannel A channel * */ - void RemoveChannel(uint8_t aChannel) { mMask &= ~(1UL << aChannel); } + void RemoveChannel(uint8_t aChannel) + { + if (aChannel < sizeof(mMask) * CHAR_BIT) + { + mMask &= ~(1UL << aChannel); + } + } /** * This method updates the channel mask by intersecting it with another mask. diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 651301b5e..1ed3d8da8 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -355,7 +355,6 @@ otError Mac::SetPanChannel(uint8_t aChannel) { otError error = OT_ERROR_NONE; - VerifyOrExit(OT_RADIO_CHANNEL_MIN <= aChannel && aChannel <= OT_RADIO_CHANNEL_MAX, error = OT_ERROR_INVALID_ARGS); VerifyOrExit(mSupportedChannelMask.ContainsChannel(aChannel), error = OT_ERROR_INVALID_ARGS); VerifyOrExit(mPanChannel != aChannel, GetNotifier().SignalIfFirst(OT_CHANGED_THREAD_CHANNEL)); @@ -379,7 +378,6 @@ otError Mac::SetRadioChannel(uint16_t aAcquisitionId, uint8_t aChannel) { otError error = OT_ERROR_NONE; - VerifyOrExit(OT_RADIO_CHANNEL_MIN <= aChannel && aChannel <= OT_RADIO_CHANNEL_MAX, error = OT_ERROR_INVALID_ARGS); VerifyOrExit(mSupportedChannelMask.ContainsChannel(aChannel), error = OT_ERROR_INVALID_ARGS); VerifyOrExit(mRadioChannelAcquisitionId && aAcquisitionId == mRadioChannelAcquisitionId,