From 2b40187d3e8cc4cb3e4932a1ac4d91cb0efd7150 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Fri, 7 Apr 2017 08:43:54 -0700 Subject: [PATCH] Remove use of zero-length arrays. (#1570) --- src/core/thread/network_diagnostic.cpp | 8 ++--- src/core/thread/network_diagnostic.hpp | 4 +-- src/core/thread/network_diagnostic_tlvs.hpp | 39 ++++++++++++++------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 68e8d474a..67f568597 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -190,10 +190,10 @@ exit: return; } -ThreadError NetworkDiagnostic::AppendIPv6AddressList(Message &aMessage) +ThreadError NetworkDiagnostic::AppendIp6AddressList(Message &aMessage) { ThreadError error = kThreadError_None; - IPv6AddressListTlv tlv; + Ip6AddressListTlv tlv; uint8_t count = 0; tlv.Init(); @@ -355,9 +355,9 @@ ThreadError NetworkDiagnostic::FillRequestedTlvs(Message &aRequest, Message &aRe break; } - case NetworkDiagnosticTlv::kIPv6AddressList: + case NetworkDiagnosticTlv::kIp6AddressList: { - SuccessOrExit(error = AppendIPv6AddressList(aResponse)); + SuccessOrExit(error = AppendIp6AddressList(aResponse)); break; } diff --git a/src/core/thread/network_diagnostic.hpp b/src/core/thread/network_diagnostic.hpp index 2d0a9b5e3..dfb5c2f34 100644 --- a/src/core/thread/network_diagnostic.hpp +++ b/src/core/thread/network_diagnostic.hpp @@ -49,7 +49,7 @@ using namespace Coap; namespace NetworkDiagnostic { -class IPv6AddressListTlv; +class Ip6AddressListTlv; class ChildTableTlv; class NetworkDiagnosticTlv; @@ -116,7 +116,7 @@ public: private: - ThreadError AppendIPv6AddressList(Message &aMessage); + ThreadError AppendIp6AddressList(Message &aMessage); ThreadError AppendChildTable(Message &aMessage); ThreadError FillRequestedTlvs(Message &aRequest, Message &aResponse, NetworkDiagnosticTlv &aNetworkDiagnosticTlv); diff --git a/src/core/thread/network_diagnostic_tlvs.hpp b/src/core/thread/network_diagnostic_tlvs.hpp index ce71bf820..436af9d1d 100644 --- a/src/core/thread/network_diagnostic_tlvs.hpp +++ b/src/core/thread/network_diagnostic_tlvs.hpp @@ -90,7 +90,7 @@ public: kRoute = 5, ///< Routing-Table TLV kLeaderData = 6, ///< Leader Data TLV kNetworkData = 7, ///< Network Data TLV - kIPv6AddressList = 8, ///< IPv6 Address List TLV + kIp6AddressList = 8, ///< Ip6 Address List TLV kMacCounters = 9, ///< Mac Counters TLV kBatteryLevel = 14, ///< Battery Level TLV kSupplyVoltage = 15, ///< Supply Voltage TLV @@ -843,7 +843,7 @@ private: * */ OT_TOOL_PACKED_BEGIN -class IPv6AddressListTlv: public NetworkDiagnosticTlv +class Ip6AddressListTlv: public NetworkDiagnosticTlv { public: enum @@ -855,7 +855,7 @@ public: * This method initializes the TLV. * */ - void Init(void) { SetType(kIPv6AddressList); SetLength(sizeof(*this) - sizeof(NetworkDiagnosticTlv)); } + void Init(void) { SetType(kIp6AddressList); SetLength(sizeof(*this) - sizeof(NetworkDiagnosticTlv)); } /** * This method indicates whether or not the TLV appears to be well-formed. @@ -864,18 +864,20 @@ public: * @retval FALSE If the TLV does not appear to be well-formed. * */ - bool IsValid(void) const { return GetLength() <= 8 * kMaxSize; } + bool IsValid(void) const { return GetLength() <= sizeof(Ip6::Address) * kMaxSize; } /** - * This method returns a pointer to the Challenge value. + * This method returns a pointer to the IPv6 address entry. * - * @returns A pointer to the Challenge value. + * @param[in] aIndex The index into the IPv6 address list. + * + * @returns A reference to the IPv6 address. * */ - const Ip6::Address *GetIPv6Address(uint8_t aIndex) const { return ((aIndex * 8 < GetLength()) ? &mIPv6AddressList[aIndex] : NULL); } + const Ip6::Address &GetIp6Address(uint8_t aIndex) const { + return *reinterpret_cast(GetValue() + (aIndex * sizeof(Ip6::Address))); + } -private: - Ip6::Address mIPv6AddressList[0]; } OT_TOOL_PACKED_END; /** @@ -1261,14 +1263,25 @@ public: */ bool IsValid(void) const { return GetLength() == sizeof(*this) - sizeof(NetworkDiagnosticTlv); } + /** + * This method returns the number of Child Table entries. + * + * @returns The number of Child Table entries. + * + */ uint8_t GetNumEntries(void) const { return GetLength() / sizeof(ChildTableEntry); } - ChildTableEntry &GetEntry(uint8_t i) { - return mChildTableEntry[i]; + /** + * This method returns the Child Table entry at @p aIndex. + * + * @param[in] aIndex The index into the Child Table list. + * + * @returns A reference to the Child Table entry. + */ + ChildTableEntry &GetEntry(uint8_t aIndex) { + return *reinterpret_cast(GetValue() + (aIndex * sizeof(ChildTableEntry))); } -private: - ChildTableEntry mChildTableEntry[0]; } OT_TOOL_PACKED_END;