From 7ec658e47a39e1c3c55651e584e0748b259d2d4d Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 14 Feb 2022 14:03:23 -0800 Subject: [PATCH] [common] add `GetArrayLength()` and `GetArrayEnd()` (#7413) This commit adds two inline template functions to get the length of an array and a pointer to the end of the array. These functions replace similar macros that were used with OT core modules. The template functions are safer to use since they would perform type checking at compile time. The functions validate that the input variable is indeed an array and unlike the macros they will not work with a pointer variable. --- .../multicast_listeners_table.cpp | 3 +- src/core/backbone_router/ndproxy_table.cpp | 5 +- src/core/coap/coap.cpp | 3 +- src/core/coap/coap_message.cpp | 3 +- src/core/common/array.hpp | 57 +++++++++++++++++++ src/core/common/error.cpp | 3 +- src/core/common/instance.hpp | 1 + src/core/common/notifier.cpp | 3 +- src/core/common/pool.hpp | 3 +- src/core/common/settings.cpp | 3 +- src/core/mac/mac.cpp | 5 +- src/core/mac/mac_filter.cpp | 7 ++- src/core/meshcop/commissioner.cpp | 3 +- src/core/meshcop/joiner.cpp | 5 +- src/core/net/dhcp6_server.cpp | 7 ++- src/core/net/dns_client.cpp | 9 +-- src/core/net/dns_dso.cpp | 3 +- src/core/net/dnssd_server.cpp | 3 +- src/core/net/ip6_address.cpp | 5 +- src/core/radio/trel_interface.cpp | 3 +- src/core/thread/mle.cpp | 3 +- src/core/thread/network_data_publisher.cpp | 3 +- src/core/thread/network_diagnostic.cpp | 3 +- src/core/thread/topology.cpp | 5 +- src/core/utils/slaac_address.cpp | 3 +- src/core/utils/srp_client_buffers.hpp | 5 +- src/lib/hdlc/hdlc.hpp | 7 ++- tests/unit/test_child_table.cpp | 3 +- tests/unit/test_dns.cpp | 7 ++- tests/unit/test_hkdf_sha256.cpp | 9 +-- tests/unit/test_hmac_sha256.cpp | 5 +- tests/unit/test_link_quality.cpp | 9 +-- tests/unit/test_timer.cpp | 3 +- 33 files changed, 144 insertions(+), 55 deletions(-) diff --git a/src/core/backbone_router/multicast_listeners_table.cpp b/src/core/backbone_router/multicast_listeners_table.cpp index bf9b51e3c..fe15d47a3 100644 --- a/src/core/backbone_router/multicast_listeners_table.cpp +++ b/src/core/backbone_router/multicast_listeners_table.cpp @@ -35,6 +35,7 @@ #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/instance.hpp" #include "common/locator_getters.hpp" @@ -66,7 +67,7 @@ Error MulticastListenersTable::Add(const Ip6::Address &aAddress, Time aExpireTim } } - VerifyOrExit(mNumValidListeners < OT_ARRAY_LENGTH(mListeners), error = kErrorNoBufs); + VerifyOrExit(mNumValidListeners < GetArrayLength(mListeners), error = kErrorNoBufs); mListeners[mNumValidListeners].SetAddress(aAddress); mListeners[mNumValidListeners].SetExpireTime(aExpireTime); diff --git a/src/core/backbone_router/ndproxy_table.cpp b/src/core/backbone_router/ndproxy_table.cpp index f680daa0e..b1e97be8d 100644 --- a/src/core/backbone_router/ndproxy_table.cpp +++ b/src/core/backbone_router/ndproxy_table.cpp @@ -35,6 +35,7 @@ #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE +#include "common/array.hpp" #include "common/locator_getters.hpp" #include "common/logging.hpp" @@ -107,7 +108,7 @@ NdProxyTable::Iterator::Iterator(Instance &aInstance, NdProxyTable::Iterator::It : InstanceLocator(aInstance) { NdProxyTable &table = GetInstance().Get(); - mItem = OT_ARRAY_END(table.mProxies); + mItem = GetArrayEnd(table.mProxies); } void NdProxyTable::Iterator::Advance(void) @@ -117,7 +118,7 @@ void NdProxyTable::Iterator::Advance(void) do { mItem++; - } while (mItem < OT_ARRAY_END(table.mProxies) && !MatchesFilter(*mItem, mFilter)); + } while (mItem < GetArrayEnd(table.mProxies) && !MatchesFilter(*mItem, mFilter)); } void NdProxyTable::Erase(NdProxy &aNdProxy) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 37ceff93e..20108d925 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -28,6 +28,7 @@ #include "coap.hpp" +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -1289,7 +1290,7 @@ void CoapBase::ProcessReceivedRequest(Message &aMessage, const Ip6::MessageInfo *curUriPath++ = '/'; } - VerifyOrExit(curUriPath + iterator.GetOption()->GetLength() < OT_ARRAY_END(uriPath), error = kErrorParse); + VerifyOrExit(curUriPath + iterator.GetOption()->GetLength() < GetArrayEnd(uriPath), error = kErrorParse); IgnoreError(iterator.ReadOptionValue(curUriPath)); curUriPath += iterator.GetOption()->GetLength(); diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index 5d02662a7..ae0c33f7e 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -34,6 +34,7 @@ #include "coap_message.hpp" #include "coap/coap.hpp" +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" #include "common/encoding.hpp" @@ -259,7 +260,7 @@ Error Message::ReadUriPathOptions(char (&aUriPath)[kMaxReceivedUriPath + 1]) con *curUriPath++ = '/'; } - VerifyOrExit(curUriPath + optionLength < OT_ARRAY_END(aUriPath), error = kErrorParse); + VerifyOrExit(curUriPath + optionLength < GetArrayEnd(aUriPath), error = kErrorParse); IgnoreError(iterator.ReadOptionValue(curUriPath)); curUriPath += optionLength; diff --git a/src/core/common/array.hpp b/src/core/common/array.hpp index 9da3e779f..068bfbfea 100644 --- a/src/core/common/array.hpp +++ b/src/core/common/array.hpp @@ -44,6 +44,63 @@ namespace ot { +/** + * This function returns the length of a given array (number of elements in the array). + * + * This template function is `constexpr`. The template arguments are expected to be deduced by the compiler allowing + * callers to simply use `GetArrayLength(aArray)`. + * + * @tparam Type The array element type. + * @tparam kLength The array length. + * + * @param[in] aArray A reference to the array. + * + * @returns The array length (number of elements in the array). + * + */ +template constexpr inline uint16_t GetArrayLength(const Type (&)[kArrayLength]) +{ + return kArrayLength; +} + +/** + * This function returns a pointer to end of a given array (pointing to the past-the-end element). + * + * Note that the past-the-end element is a theoretical element that would follow the last element in the array. It does + * not point to an actual element in array, and thus should not be dereferenced. + * + * @tparam Type The array element type. + * @tparam kLength The array length. + * + * @param[in] aArray A reference to the array. + * + * @returns Pointer to the past-the-end element. + * + */ +template inline Type *GetArrayEnd(Type (&aArray)[kArrayLength]) +{ + return &aArray[kArrayLength]; +} + +/** + * This function returns a pointer to end of a given array (pointing to the past-the-end element). + * + * Note that the past-the-end element is a theoretical element that would follow the last element in the array. It does + * not point to an actual element in array, and thus should not be dereferenced. + * + * @tparam Type The array element type. + * @tparam kLength The array length. + * + * @param[in] aArray A reference to the array. + * + * @returns Pointer to the past-the-end element. + * + */ +template inline const Type *GetArrayEnd(const Type (&aArray)[kArrayLength]) +{ + return &aArray[kArrayLength]; +} + /** * This template class represents an array of elements with a fixed max size. * diff --git a/src/core/common/error.cpp b/src/core/common/error.cpp index 5d4a92c81..ec15c4aaf 100644 --- a/src/core/common/error.cpp +++ b/src/core/common/error.cpp @@ -33,6 +33,7 @@ #include "error.hpp" +#include "common/array.hpp" #include "common/code_utils.hpp" namespace ot { @@ -80,7 +81,7 @@ const char *ErrorToString(Error aError) "Rejected", // (37) kErrorRejected }; - return aError < OT_ARRAY_LENGTH(kErrorStrings) ? kErrorStrings[aError] : "UnknownErrorType"; + return aError < GetArrayLength(kErrorStrings) ? kErrorStrings[aError] : "UnknownErrorType"; } } // namespace ot diff --git a/src/core/common/instance.hpp b/src/core/common/instance.hpp index fa1b26f79..478f03b03 100644 --- a/src/core/common/instance.hpp +++ b/src/core/common/instance.hpp @@ -45,6 +45,7 @@ #include #endif +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/error.hpp" #include "common/extension.hpp" diff --git a/src/core/common/notifier.cpp b/src/core/common/notifier.cpp index 4b764ec1e..99663274c 100644 --- a/src/core/common/notifier.cpp +++ b/src/core/common/notifier.cpp @@ -34,6 +34,7 @@ #include "notifier.hpp" #include "border_router/routing_manager.hpp" +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" #include "common/locator_getters.hpp" @@ -289,7 +290,7 @@ const char *Notifier::EventToString(Event aEvent) const "PndDset", // kEventPendingDatasetChanged (1 << 29) }; - for (uint8_t index = 0; index < OT_ARRAY_LENGTH(kEventStrings); index++) + for (uint8_t index = 0; index < GetArrayLength(kEventStrings); index++) { if (static_cast(aEvent) == (1U << index)) { diff --git a/src/core/common/pool.hpp b/src/core/common/pool.hpp index ad22b1811..279655f14 100644 --- a/src/core/common/pool.hpp +++ b/src/core/common/pool.hpp @@ -36,6 +36,7 @@ #include "openthread-core-config.h" +#include "common/array.hpp" #include "common/linked_list.hpp" #include "common/non_copyable.hpp" @@ -147,7 +148,7 @@ public: * @retval FALSE if @p aObject is not from the pool. * */ - bool IsPoolEntry(const Type &aObject) const { return (&mPool[0] <= &aObject) && (&aObject < OT_ARRAY_END(mPool)); } + bool IsPoolEntry(const Type &aObject) const { return (&mPool[0] <= &aObject) && (&aObject < GetArrayEnd(mPool)); } /** * This method returns the associated index of a given entry from the pool. diff --git a/src/core/common/settings.cpp b/src/core/common/settings.cpp index 424db0f15..ed5fad6ee 100644 --- a/src/core/common/settings.cpp +++ b/src/core/common/settings.cpp @@ -33,6 +33,7 @@ #include "settings.hpp" +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/instance.hpp" #include "common/locator_getters.hpp" @@ -195,7 +196,7 @@ const uint16_t Settings::kCriticalKeys[] = { void Settings::Init(void) { Get().Init(); - Get().SetCriticalKeys(kCriticalKeys, OT_ARRAY_LENGTH(kCriticalKeys)); + Get().SetCriticalKeys(kCriticalKeys, GetArrayLength(kCriticalKeys)); } void Settings::Deinit(void) diff --git a/src/core/mac/mac.cpp b/src/core/mac/mac.cpp index 50073ebc9..b0f03f179 100644 --- a/src/core/mac/mac.cpp +++ b/src/core/mac/mac.cpp @@ -35,6 +35,7 @@ #include +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -1084,7 +1085,7 @@ void Mac::BeginTransmit(void) // copy the frame into correct `TxFrame` for each radio type // (if it is not already prepared). - for (uint8_t index = 0; index < OT_ARRAY_LENGTH(RadioTypes::kAllRadioTypes); index++) + for (uint8_t index = 0; index < GetArrayLength(RadioTypes::kAllRadioTypes); index++) { RadioType radio = RadioTypes::kAllRadioTypes[index]; @@ -1103,7 +1104,7 @@ void Mac::BeginTransmit(void) // process security for each radio type separately. This // allows radio links to handle security differently, e.g., // with different keys or link frame counters. - for (uint8_t index = 0; index < OT_ARRAY_LENGTH(RadioTypes::kAllRadioTypes); index++) + for (uint8_t index = 0; index < GetArrayLength(RadioTypes::kAllRadioTypes); index++) { RadioType radio = RadioTypes::kAllRadioTypes[index]; diff --git a/src/core/mac/mac_filter.cpp b/src/core/mac/mac_filter.cpp index d05a221d5..c119c425b 100644 --- a/src/core/mac/mac_filter.cpp +++ b/src/core/mac/mac_filter.cpp @@ -35,6 +35,7 @@ #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" @@ -123,7 +124,7 @@ Error Filter::GetNextAddress(Iterator &aIterator, Entry &aEntry) const { Error error = kErrorNotFound; - for (; aIterator < OT_ARRAY_LENGTH(mFilterEntries); aIterator++) + for (; aIterator < GetArrayLength(mFilterEntries); aIterator++) { const FilterEntry &entry = mFilterEntries[aIterator]; @@ -185,7 +186,7 @@ Error Filter::GetNextRssIn(Iterator &aIterator, Entry &aEntry) { Error error = kErrorNotFound; - for (; aIterator < OT_ARRAY_LENGTH(mFilterEntries); aIterator++) + for (; aIterator < GetArrayLength(mFilterEntries); aIterator++) { FilterEntry &entry = mFilterEntries[aIterator]; @@ -200,7 +201,7 @@ Error Filter::GetNextRssIn(Iterator &aIterator, Entry &aEntry) } // Return the default RssIn at the end of list - if ((aIterator == OT_ARRAY_LENGTH(mFilterEntries)) && (mDefaultRssIn != kFixedRssDisabled)) + if ((aIterator == GetArrayLength(mFilterEntries)) && (mDefaultRssIn != kFixedRssDisabled)) { AsCoreType(&aEntry.mExtAddress).Fill(0xff); aEntry.mRssIn = mDefaultRssIn; diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 81b2d7590..7789cba8e 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -38,6 +38,7 @@ #include #include "coap/coap_message.hpp" +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/encoding.hpp" #include "common/instance.hpp" @@ -516,7 +517,7 @@ Error Commissioner::GetNextJoinerInfo(uint16_t &aIterator, otJoinerInfo &aJoiner { Error error = kErrorNone; - while (aIterator < OT_ARRAY_LENGTH(mJoiners)) + while (aIterator < GetArrayLength(mJoiners)) { const Joiner &joiner = mJoiners[aIterator++]; diff --git a/src/core/meshcop/joiner.cpp b/src/core/meshcop/joiner.cpp index 52cac4cf7..e05a2bdbb 100644 --- a/src/core/meshcop/joiner.cpp +++ b/src/core/meshcop/joiner.cpp @@ -37,6 +37,7 @@ #include +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -302,7 +303,7 @@ void Joiner::SaveDiscoveredJoinerRouter(const Mle::DiscoverScanner::ScanResult & { uint8_t priority; bool doesAllowAny; - JoinerRouter *end = OT_ARRAY_END(mJoinerRouters); + JoinerRouter *end = GetArrayEnd(mJoinerRouters); JoinerRouter *entry; doesAllowAny = AsCoreType(&aResult.mSteeringData).PermitsAllJoiners(); @@ -342,7 +343,7 @@ exit: void Joiner::TryNextJoinerRouter(Error aPrevError) { - for (; mJoinerRouterIndex < OT_ARRAY_LENGTH(mJoinerRouters); mJoinerRouterIndex++) + for (; mJoinerRouterIndex < GetArrayLength(mJoinerRouters); mJoinerRouterIndex++) { JoinerRouter &router = mJoinerRouters[mJoinerRouterIndex]; Error error; diff --git a/src/core/net/dhcp6_server.cpp b/src/core/net/dhcp6_server.cpp index 805144d10..f654141f8 100644 --- a/src/core/net/dhcp6_server.cpp +++ b/src/core/net/dhcp6_server.cpp @@ -35,6 +35,7 @@ #if OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/encoding.hpp" @@ -317,7 +318,7 @@ Error Server::ProcessIaAddress(Message &aMessage, uint16_t aOffset) VerifyOrExit(option.GetLength() == sizeof(option) - sizeof(Option), error = kErrorParse); // mask matching prefix - for (uint16_t i = 0; i < OT_ARRAY_LENGTH(mPrefixAgents); i++) + for (uint16_t i = 0; i < GetArrayLength(mPrefixAgents); i++) { if (mPrefixAgents[i].IsValid() && mPrefixAgents[i].IsPrefixMatch(option.GetAddress())) { @@ -397,7 +398,7 @@ Error Server::AppendIaNa(Message &aMessage, IaNa &aIaNa) if (mPrefixAgentsMask) { - for (uint16_t i = 0; i < OT_ARRAY_LENGTH(mPrefixAgents); i++) + for (uint16_t i = 0; i < GetArrayLength(mPrefixAgents); i++) { if (mPrefixAgentsMask & (1 << i)) { @@ -437,7 +438,7 @@ Error Server::AppendIaAddress(Message &aMessage, ClientIdentifier &aClientId) if (mPrefixAgentsMask) { // if specified, only apply specified prefixes - for (uint16_t i = 0; i < OT_ARRAY_LENGTH(mPrefixAgents); i++) + for (uint16_t i = 0; i < GetArrayLength(mPrefixAgents); i++) { if (mPrefixAgentsMask & (1 << i)) { diff --git a/src/core/net/dns_client.cpp b/src/core/net/dns_client.cpp index 01ada8812..3e79d79a1 100644 --- a/src/core/net/dns_client.cpp +++ b/src/core/net/dns_client.cpp @@ -30,6 +30,7 @@ #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -533,13 +534,13 @@ const uint16_t Client::kServiceQueryRecordTypes[] = {ResourceRecord::kTypeSrv, R #endif const uint8_t Client::kQuestionCount[] = { - /* kIp6AddressQuery -> */ OT_ARRAY_LENGTH(kIp6AddressQueryRecordTypes), // AAAA records + /* kIp6AddressQuery -> */ GetArrayLength(kIp6AddressQueryRecordTypes), // AAAA records #if OPENTHREAD_CONFIG_DNS_CLIENT_NAT64_ENABLE - /* kIp4AddressQuery -> */ OT_ARRAY_LENGTH(kIp4AddressQueryRecordTypes), // A records + /* kIp4AddressQuery -> */ GetArrayLength(kIp4AddressQueryRecordTypes), // A records #endif #if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE - /* kBrowseQuery -> */ OT_ARRAY_LENGTH(kBrowseQueryRecordTypes), // PTR records - /* kServiceQuery -> */ OT_ARRAY_LENGTH(kServiceQueryRecordTypes), // SRV and TXT records + /* kBrowseQuery -> */ GetArrayLength(kBrowseQueryRecordTypes), // PTR records + /* kServiceQuery -> */ GetArrayLength(kServiceQueryRecordTypes), // SRV and TXT records #endif }; diff --git a/src/core/net/dns_dso.cpp b/src/core/net/dns_dso.cpp index 06712a53d..31985749f 100644 --- a/src/core/net/dns_dso.cpp +++ b/src/core/net/dns_dso.cpp @@ -30,6 +30,7 @@ #if OPENTHREAD_CONFIG_DNS_DSO_ENABLE +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -640,7 +641,7 @@ Error Dso::Connection::AppendPadding(Message &aMessage) // that its padded length is a multiple of the chosen block // length. - blockLength = kBlockLengths[Random::NonCrypto::GetUint8InRange(0, OT_ARRAY_LENGTH(kBlockLengths))]; + blockLength = kBlockLengths[Random::NonCrypto::GetUint8InRange(0, GetArrayLength(kBlockLengths))]; paddingTlv.Init((blockLength - ((aMessage.GetLength() + sizeof(Tlv)) % blockLength)) % blockLength); diff --git a/src/core/net/dnssd_server.cpp b/src/core/net/dnssd_server.cpp index 28240c558..ec4dbb4fa 100644 --- a/src/core/net/dnssd_server.cpp +++ b/src/core/net/dnssd_server.cpp @@ -35,6 +35,7 @@ #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -1022,7 +1023,7 @@ const otDnssdQuery *Server::GetNextQuery(const otDnssdQuery *aQuery) const cur = query + 1; } - for (; cur < OT_ARRAY_END(mQueryTransactions); cur++) + for (; cur < GetArrayEnd(mQueryTransactions); cur++) { if (cur->IsValid()) { diff --git a/src/core/net/ip6_address.cpp b/src/core/net/ip6_address.cpp index 27cc79336..11dcf9393 100644 --- a/src/core/net/ip6_address.cpp +++ b/src/core/net/ip6_address.cpp @@ -35,6 +35,7 @@ #include +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/encoding.hpp" @@ -613,7 +614,7 @@ Error Address::FromString(const char *aString) Ip4::Address ip4Addr; SuccessOrExit(error = ip4Addr.FromString(aString)); - memcpy(OT_ARRAY_END(mFields.m8) - Ip4::Address::kSize, ip4Addr.GetBytes(), Ip4::Address::kSize); + memcpy(GetArrayEnd(mFields.m8) - Ip4::Address::kSize, ip4Addr.GetBytes(), Ip4::Address::kSize); } error = kErrorNone; @@ -639,7 +640,7 @@ void Address::ToString(char *aBuffer, uint16_t aSize) const void Address::ToString(StringWriter &aWriter) const { - AppendHexWords(aWriter, OT_ARRAY_LENGTH(mFields.m16)); + AppendHexWords(aWriter, static_cast(GetArrayLength(mFields.m16))); } void Address::AppendHexWords(StringWriter &aWriter, uint8_t aLength) const diff --git a/src/core/radio/trel_interface.cpp b/src/core/radio/trel_interface.cpp index 3906bbea4..2e5515c5f 100644 --- a/src/core/radio/trel_interface.cpp +++ b/src/core/radio/trel_interface.cpp @@ -36,6 +36,7 @@ #include +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -148,7 +149,7 @@ void Interface::RegisterService(void) txtEntries[1].Init(kTxtRecordExtPanIdKey, Get().GetExtendedPanId().m8, sizeof(Mac::ExtendedPanId)); txtData.Init(txtDataBuffer, sizeof(txtDataBuffer)); - SuccessOrAssert(Dns::TxtEntry::AppendEntries(txtEntries, OT_ARRAY_LENGTH(txtEntries), txtData)); + SuccessOrAssert(Dns::TxtEntry::AppendEntries(txtEntries, GetArrayLength(txtEntries), txtData)); otLogInfoMac("Trel: Registering DNS-SD service: port:%u, txt:\"%s=%s, %s=%s\"", mUdpPort, kTxtRecordExtAddressKey, Get().GetExtAddress().ToString().AsCString(), kTxtRecordExtPanIdKey, diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 8eae3b0c1..4091a554a 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -36,6 +36,7 @@ #include #include +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -4522,7 +4523,7 @@ const char *Mle::RoleToString(DeviceRole aRole) static_assert(kRoleRouter == 3, "kRoleRouter value is incorrect"); static_assert(kRoleLeader == 4, "kRoleLeader value is incorrect"); - return (aRole < OT_ARRAY_LENGTH(kRoleStrings)) ? kRoleStrings[aRole] : "invalid"; + return (aRole < GetArrayLength(kRoleStrings)) ? kRoleStrings[aRole] : "invalid"; } // LCOV_EXCL_START diff --git a/src/core/thread/network_data_publisher.cpp b/src/core/thread/network_data_publisher.cpp index f31ccdce2..52981c6cc 100644 --- a/src/core/thread/network_data_publisher.cpp +++ b/src/core/thread/network_data_publisher.cpp @@ -36,6 +36,7 @@ #if OPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/const_cast.hpp" #include "common/instance.hpp" @@ -184,7 +185,7 @@ const Publisher::PrefixEntry *Publisher::FindMatchingPrefixEntry(const Ip6::Pref bool Publisher::IsAPrefixEntry(const Entry &aEntry) const { - return (&mPrefixEntries[0] <= &aEntry) && (&aEntry < OT_ARRAY_END(mPrefixEntries)); + return (&mPrefixEntries[0] <= &aEntry) && (&aEntry < GetArrayEnd(mPrefixEntries)); } void Publisher::NotifyPrefixEntryChange(Event aEvent, const Ip6::Prefix &aPrefix) const diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index e4a6c6acd..1600c8456 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -36,6 +36,7 @@ #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_TMF_NETWORK_DIAG_MTD_ENABLE #include "coap/coap_message.hpp" +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" @@ -837,7 +838,7 @@ Error NetworkDiagnostic::GetNextDiagTlv(const Coap::Message &aMessage, ChildTableTlv &childTable = As(tlv); VerifyOrExit(childTable.IsValid(), error = kErrorParse); - VerifyOrExit(childTable.GetNumEntries() <= OT_ARRAY_LENGTH(aNetworkDiagTlv.mData.mChildTable.mTable), + VerifyOrExit(childTable.GetNumEntries() <= GetArrayLength(aNetworkDiagTlv.mData.mChildTable.mTable), error = kErrorParse); for (uint8_t i = 0; i < childTable.GetNumEntries(); ++i) diff --git a/src/core/thread/topology.cpp b/src/core/thread/topology.cpp index b03abf337..1010bf717 100644 --- a/src/core/thread/topology.cpp +++ b/src/core/thread/topology.cpp @@ -33,6 +33,7 @@ #include "topology.hpp" +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" #include "common/instance.hpp" @@ -474,7 +475,7 @@ MlrState Child::GetAddressMlrState(const Ip6::Address &aAddress) const { uint16_t addressIndex; - OT_ASSERT(&mIp6Address[0] <= &aAddress && &aAddress < OT_ARRAY_END(mIp6Address)); + OT_ASSERT(&mIp6Address[0] <= &aAddress && &aAddress < GetArrayEnd(mIp6Address)); addressIndex = static_cast(&aAddress - mIp6Address); @@ -487,7 +488,7 @@ void Child::SetAddressMlrState(const Ip6::Address &aAddress, MlrState aState) { uint16_t addressIndex; - OT_ASSERT(&mIp6Address[0] <= &aAddress && &aAddress < OT_ARRAY_END(mIp6Address)); + OT_ASSERT(&mIp6Address[0] <= &aAddress && &aAddress < GetArrayEnd(mIp6Address)); addressIndex = static_cast(&aAddress - mIp6Address); diff --git a/src/core/utils/slaac_address.cpp b/src/core/utils/slaac_address.cpp index ccf299435..08c080522 100644 --- a/src/core/utils/slaac_address.cpp +++ b/src/core/utils/slaac_address.cpp @@ -35,6 +35,7 @@ #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/instance.hpp" #include "common/locator_getters.hpp" @@ -244,7 +245,7 @@ void Slaac::Update(UpdateMode aMode) if (!added) { otLogWarnUtil("SLAAC: Failed to add - max %d addresses supported and already in use", - OT_ARRAY_LENGTH(mAddresses)); + GetArrayLength(mAddresses)); } } } diff --git a/src/core/utils/srp_client_buffers.hpp b/src/core/utils/srp_client_buffers.hpp index fc4944173..7746de88d 100644 --- a/src/core/utils/srp_client_buffers.hpp +++ b/src/core/utils/srp_client_buffers.hpp @@ -40,6 +40,7 @@ #include +#include "common/array.hpp" #include "common/as_core_type.hpp" #include "common/clearable.hpp" #include "common/locator.hpp" @@ -165,7 +166,7 @@ public: */ const char **GetSubTypeLabelsArray(uint16_t &aArrayLength) { - aArrayLength = OT_ARRAY_LENGTH(mSubTypeLabels); + aArrayLength = GetArrayLength(mSubTypeLabels); return mSubTypeLabels; } @@ -213,7 +214,7 @@ public: */ Ip6::Address *GetHostAddressesArray(uint8_t &aArrayLength) { - aArrayLength = OT_ARRAY_LENGTH(mHostAddresses); + aArrayLength = static_cast(GetArrayLength(mHostAddresses)); return &mHostAddresses[0]; } diff --git a/src/lib/hdlc/hdlc.hpp b/src/lib/hdlc/hdlc.hpp index ea97ce748..26468aaa2 100644 --- a/src/lib/hdlc/hdlc.hpp +++ b/src/lib/hdlc/hdlc.hpp @@ -39,6 +39,7 @@ #include +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" #include "common/encoding.hpp" @@ -242,7 +243,7 @@ public: { otError error = OT_ERROR_NO_BUFS; - if (GetFrame() + aLength <= OT_ARRAY_END(mBuffer)) + if (GetFrame() + aLength <= GetArrayEnd(mBuffer)) { mWritePointer = GetFrame() + aLength; mRemainingLength = static_cast(mBuffer + kSize - mWritePointer); @@ -273,7 +274,7 @@ public: { otError error = OT_ERROR_NO_BUFS; - if (mWriteFrameStart + kHeaderSize + aSkipLength <= OT_ARRAY_END(mBuffer)) + if (mWriteFrameStart + kHeaderSize + aSkipLength <= GetArrayEnd(mBuffer)) { Encoding::LittleEndian::WriteUint16(aSkipLength, mWriteFrameStart + kHeaderSkipLengthOffset); mWritePointer = GetFrame(); @@ -366,7 +367,7 @@ public: { otError error = OT_ERROR_NONE; - OT_ASSERT(aFrame == nullptr || (mBuffer <= aFrame && aFrame < OT_ARRAY_END(mBuffer))); + OT_ASSERT(aFrame == nullptr || (mBuffer <= aFrame && aFrame < GetArrayEnd(mBuffer))); aFrame = (aFrame == nullptr) ? mBuffer : aFrame + aLength; diff --git a/tests/unit/test_child_table.cpp b/tests/unit/test_child_table.cpp index 1ce75fccc..3281baa1a 100644 --- a/tests/unit/test_child_table.cpp +++ b/tests/unit/test_child_table.cpp @@ -31,6 +31,7 @@ #include #include "test_util.h" +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/instance.hpp" #include "thread/child_table.hpp" @@ -290,7 +291,7 @@ void TestChildTable(void) }, }; - const uint16_t testListLength = OT_ARRAY_LENGTH(testChildList); + const uint16_t testListLength = GetArrayLength(testChildList); uint16_t testNumAllowedChildren = 2; diff --git a/tests/unit/test_dns.cpp b/tests/unit/test_dns.cpp index f5103b7c5..074cb9ca3 100644 --- a/tests/unit/test_dns.cpp +++ b/tests/unit/test_dns.cpp @@ -33,6 +33,7 @@ #include "test_platform.h" #include "test_util.hpp" +#include "common/array.hpp" #include "common/instance.hpp" #include "net/dns_types.hpp" @@ -1095,7 +1096,7 @@ void TestHeaderAndResourceRecords(void) printf("Use FindRecord() to search for specific records:\n"); printf(" Answer Section\n"); - for (index = 0; index < OT_ARRAY_LENGTH(kInstanceNames); index++) + for (index = 0; index < GetArrayLength(kInstanceNames); index++) { offset = answerSectionOffset; SuccessOrQuit( @@ -1248,13 +1249,13 @@ void TestDnsTxtEntry(void) VerifyOrQuit((message = messagePool->Allocate(Message::kTypeIp6)) != nullptr); data.Init(txtData, sizeof(txtData)); - SuccessOrQuit(Dns::TxtEntry::AppendEntries(kTxtEntries, OT_ARRAY_LENGTH(kTxtEntries), data)); + SuccessOrQuit(Dns::TxtEntry::AppendEntries(kTxtEntries, GetArrayLength(kTxtEntries), data)); VerifyOrQuit(data.GetBytes() == txtData); txtDataLength = data.GetLength(); VerifyOrQuit(txtDataLength < kMaxTxtDataSize, "TXT data is too long"); DumpBuffer("txt data", txtData, txtDataLength); - SuccessOrQuit(Dns::TxtEntry::AppendEntries(kTxtEntries, OT_ARRAY_LENGTH(kTxtEntries), *message)); + SuccessOrQuit(Dns::TxtEntry::AppendEntries(kTxtEntries, GetArrayLength(kTxtEntries), *message)); VerifyOrQuit(txtDataLength == message->GetLength()); VerifyOrQuit(message->CompareBytes(0, txtData, txtDataLength)); diff --git a/tests/unit/test_hkdf_sha256.cpp b/tests/unit/test_hkdf_sha256.cpp index 08cfb4baa..ca12be620 100644 --- a/tests/unit/test_hkdf_sha256.cpp +++ b/tests/unit/test_hkdf_sha256.cpp @@ -28,13 +28,14 @@ #include -#include "common/debug.hpp" -#include "crypto/hkdf_sha256.hpp" - #include "test_platform.h" #include "test_util.h" #include "test_util.hpp" +#include "common/array.hpp" +#include "common/debug.hpp" +#include "crypto/hkdf_sha256.hpp" + struct TestVector { const uint8_t *mInKey; @@ -125,7 +126,7 @@ void TestHkdfSha256(void) VerifyOrQuit(instance != nullptr); - for (const TestVector *test = &kTestVectors[0]; test < OT_ARRAY_END(kTestVectors); test++) + for (const TestVector *test = &kTestVectors[0]; test < ot::GetArrayEnd(kTestVectors); test++) { ot::Crypto::HkdfSha256 hkdf; uint8_t outKey[kMaxOuttKey]; diff --git a/tests/unit/test_hmac_sha256.cpp b/tests/unit/test_hmac_sha256.cpp index 7c67d2a59..5c9a97831 100644 --- a/tests/unit/test_hmac_sha256.cpp +++ b/tests/unit/test_hmac_sha256.cpp @@ -28,6 +28,7 @@ #include +#include "common/array.hpp" #include "common/debug.hpp" #include "common/message.hpp" #include "crypto/hmac_sha256.hpp" @@ -87,7 +88,7 @@ void TestSha256(void) Instance * instance = testInitInstance(); MessagePool *messagePool; Message * message; - uint16_t offsets[OT_ARRAY_LENGTH(kTestCases)]; + uint16_t offsets[GetArrayLength(kTestCases)]; uint8_t index; VerifyOrQuit(instance != nullptr); @@ -228,7 +229,7 @@ void TestHmacSha256(void) Instance * instance = testInitInstance(); MessagePool *messagePool; Message * message; - uint16_t offsets[OT_ARRAY_LENGTH(kTestCases)]; + uint16_t offsets[GetArrayLength(kTestCases)]; uint8_t index; printf("TestHmacSha256\n"); diff --git a/tests/unit/test_link_quality.cpp b/tests/unit/test_link_quality.cpp index e4c926115..f9162e366 100644 --- a/tests/unit/test_link_quality.cpp +++ b/tests/unit/test_link_quality.cpp @@ -26,12 +26,13 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include "common/code_utils.hpp" -#include "thread/link_quality.hpp" - #include "test_platform.h" #include "test_util.h" +#include "common/array.hpp" +#include "common/code_utils.hpp" +#include "thread/link_quality.hpp" + namespace ot { static ot::Instance *sInstance; @@ -412,7 +413,7 @@ void TestSuccessRateTracker(void) // Adding success/failure at different rates and checking the RateTracker rate for every sample - for (uint16_t testRound = 0; testRound < OT_ARRAY_LENGTH(kWeightLimit) * 2; testRound++) + for (uint16_t testRound = 0; testRound < GetArrayLength(kWeightLimit) * 2; testRound++) { uint16_t weightLimit; bool reverseLogic; diff --git a/tests/unit/test_timer.cpp b/tests/unit/test_timer.cpp index 660b56c81..00cc02ed8 100644 --- a/tests/unit/test_timer.cpp +++ b/tests/unit/test_timer.cpp @@ -28,6 +28,7 @@ #include "test_platform.h" +#include "common/array.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" #include "common/instance.hpp" @@ -585,7 +586,7 @@ template int TestTenTimers(void) size_t i; - for (i = 0; i < OT_ARRAY_LENGTH(kTimeShift); i++) + for (i = 0; i < ot::GetArrayLength(kTimeShift); i++) { TenTimers(kTimeShift[i]); }