From f9cd4b4182d2fe6491ce161dd3ae297dad9c5847 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 13 Apr 2023 23:03:04 -0700 Subject: [PATCH] [net-diags] fix `GetNextDiagTlv()` when skipping over unknown TLVs (#8957) This commit fixes `GetNextDiagTlv()` to correctly return error `kErrorNotFound` if the message happens to end with an unknown TLV. We ensure to set the `error` after the end of `while()` loop. With the previous code we could return `kErrorNone` (since we would successfully read and skip over the unknown TLV which would set `error`). --- src/core/thread/network_diagnostic.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/thread/network_diagnostic.cpp b/src/core/thread/network_diagnostic.cpp index 2511047e6..14703c6df 100644 --- a/src/core/thread/network_diagnostic.cpp +++ b/src/core/thread/network_diagnostic.cpp @@ -572,7 +572,7 @@ static inline void ParseMacCounters(const MacCountersTlv &aMacCountersTlv, otNet Error Client::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator, TlvInfo &aTlvInfo) { - Error error = kErrorNotFound; + Error error; uint16_t offset = (aIterator == 0) ? aMessage.GetOffset() : aIterator; while (offset < aMessage.GetLength()) @@ -774,10 +774,13 @@ Error Client::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator, // Exit if a TLV is recognized and parsed successfully. aTlvInfo.mType = tlv.GetType(); aIterator = offset; + error = kErrorNone; ExitNow(); } } + error = kErrorNotFound; + exit: return error; }