From 322359fd8886b43a66b06c9344f532aa9ee41f79 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Tue, 24 Mar 2020 07:25:53 -0700 Subject: [PATCH] [lowpan] avoid backward goto statement in Compress() (#4725) --- src/core/thread/lowpan.cpp | 37 ++++++++++++++++++++++--------------- src/core/thread/lowpan.hpp | 6 ++++++ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/core/thread/lowpan.cpp b/src/core/thread/lowpan.cpp index 47c63af04..549f2460b 100644 --- a/src/core/thread/lowpan.cpp +++ b/src/core/thread/lowpan.cpp @@ -246,12 +246,29 @@ otError Lowpan::Compress(Message & aMessage, const Mac::Address &aMacSource, const Mac::Address &aMacDest, BufferWriter & aBuf) +{ + otError error; + uint8_t headerDepth = 0xff; + + do + { + error = Compress(aMessage, aMacSource, aMacDest, aBuf, headerDepth); + } while ((error != OT_ERROR_NONE) && (headerDepth > 0)); + + return error; +} + +otError Lowpan::Compress(Message & aMessage, + const Mac::Address &aMacSource, + const Mac::Address &aMacDest, + BufferWriter & aBuf, + uint8_t & aHeaderDepth) { otError error = OT_ERROR_NONE; NetworkData::Leader &networkData = Get(); uint16_t startOffset = aMessage.GetOffset(); BufferWriter buf = aBuf; - uint16_t hcCtl; + uint16_t hcCtl = kHcDispatch; Ip6::Header ip6Header; uint8_t * ip6HeaderBytes = reinterpret_cast(&ip6Header); Context srcContext, dstContext; @@ -259,13 +276,8 @@ otError Lowpan::Compress(Message & aMessage, uint8_t nextHeader; uint8_t ecn; uint8_t dscp; - uint8_t headerDepth; - uint8_t headerMaxDepth = 0xff; - -compress: - - headerDepth = 0; - hcCtl = kHcDispatch; + uint8_t headerDepth = 0; + uint8_t headerMaxDepth = aHeaderDepth; VerifyOrExit(aMessage.Read(aMessage.GetOffset(), sizeof(ip6Header), &ip6Header) == sizeof(ip6Header), error = OT_ERROR_PARSE); @@ -441,6 +453,8 @@ compress: } exit: + aHeaderDepth = headerDepth; + if (error == OT_ERROR_NONE) { IgnoreReturnValue(aBuf.Write(hcCtl >> 8)); @@ -450,13 +464,6 @@ exit: else { aMessage.SetOffset(startOffset); - - if (headerDepth > 0) - { - buf = aBuf; - headerMaxDepth = headerDepth; - goto compress; - } } return error; diff --git a/src/core/thread/lowpan.hpp b/src/core/thread/lowpan.hpp index c7d5dd74e..3ab78b9d3 100644 --- a/src/core/thread/lowpan.hpp +++ b/src/core/thread/lowpan.hpp @@ -360,6 +360,12 @@ private: kUdpPortMask = 3 << 0, }; + otError Compress(Message & aMessage, + const Mac::Address &aMacSource, + const Mac::Address &aMacDest, + BufferWriter & aBuf, + uint8_t & aHeaderDepth); + otError CompressExtensionHeader(Message &aMessage, BufferWriter &aBuf, uint8_t &aNextHeader); otError CompressSourceIid(const Mac::Address &aMacAddr, const Ip6::Address &aIpAddr,