From 48128acb76b24713158096ebd64932822d5272aa Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 2 Feb 2023 21:26:38 -0800 Subject: [PATCH] [ip6] remove default constructor for `Ip6::Option` (#8712) All sub-classes of `Option` provide `Init()` method to initialize the Option. This commit also updates `Lowpan::CompressExtensionHeader()` to check that we did read an option from message before checking if it can be compressed. --- src/core/net/ip6_headers.cpp | 13 +++++++++++++ src/core/net/ip6_headers.hpp | 17 +---------------- src/core/thread/lowpan.cpp | 26 ++++++++++++++++---------- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/core/net/ip6_headers.cpp b/src/core/net/ip6_headers.cpp index be72fb601..9406adf33 100644 --- a/src/core/net/ip6_headers.cpp +++ b/src/core/net/ip6_headers.cpp @@ -38,6 +38,9 @@ namespace ot { namespace Ip6 { +//--------------------------------------------------------------------------------------------------------------------- +// Header + Error Header::ParseFrom(const Message &aMessage) { Error error = kErrorParse; @@ -63,5 +66,15 @@ bool Header::IsValid(void) const return IsVersion6() && ((sizeof(Header) + GetPayloadLength()) <= kMaxLength); } +//--------------------------------------------------------------------------------------------------------------------- +// PadNOption + +void PadNOption::Init(uint8_t aPadLength) +{ + SetType(kType); + SetLength(aPadLength - sizeof(Option)); + memset(mPad, kData, aPadLength - sizeof(Option)); +} + } // namespace Ip6 } // namespace ot diff --git a/src/core/net/ip6_headers.hpp b/src/core/net/ip6_headers.hpp index c785d833b..47c5b339d 100644 --- a/src/core/net/ip6_headers.hpp +++ b/src/core/net/ip6_headers.hpp @@ -450,16 +450,6 @@ OT_TOOL_PACKED_BEGIN class Option { public: - /** - * Default constructor. - * - */ - Option(void) - : mType(0) - , mLength(0) - { - } - /** * This method returns the IPv6 Option Type value. * @@ -545,12 +535,7 @@ public: * @param[in] aPadLength The length of needed padding. Allowed value from range 2-7. * */ - void Init(uint8_t aPadLength) - { - SetType(kType); - SetLength(aPadLength - sizeof(Option)); - memset(mPad, kData, aPadLength - sizeof(Option)); - } + void Init(uint8_t aPadLength); private: uint8_t mPad[kMaxLength]; diff --git a/src/core/thread/lowpan.cpp b/src/core/thread/lowpan.cpp index b79f5dbc6..f4938cc33 100644 --- a/src/core/thread/lowpan.cpp +++ b/src/core/thread/lowpan.cpp @@ -472,7 +472,8 @@ Error Lowpan::CompressExtensionHeader(Message &aMessage, FrameBuilder &aFrameBui // Pad1 or PadN option MAY be elided by the compressor." if (aNextHeader == Ip6::kProtoHopOpts || aNextHeader == Ip6::kProtoDstOpts) { - uint16_t offset = aMessage.GetOffset(); + uint16_t offset = aMessage.GetOffset(); + bool hasOption = false; Ip6::Option option; while ((offset - aMessage.GetOffset()) < len) @@ -487,19 +488,24 @@ Error Lowpan::CompressExtensionHeader(Message &aMessage, FrameBuilder &aFrameBui { offset += option.GetSize(); } + + hasOption = true; } - // Check if the last option can be compressed. - if (option.GetType() == Ip6::Pad1Option::kType) + if (hasOption) { - padLength = sizeof(Ip6::Pad1Option); - } - else if (option.GetType() == Ip6::PadNOption::kType) - { - padLength = option.GetSize(); - } + // Check if the last option can be compressed. + if (option.GetType() == Ip6::Pad1Option::kType) + { + padLength = sizeof(Ip6::Pad1Option); + } + else if (option.GetType() == Ip6::PadNOption::kType) + { + padLength = option.GetSize(); + } - len -= padLength; + len -= padLength; + } } VerifyOrExit(aMessage.GetOffset() + len + padLength <= aMessage.GetLength(), error = kErrorParse);