[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.
This commit is contained in:
Abtin Keshavarzian
2023-02-02 21:26:38 -08:00
committed by GitHub
parent d0f6f8dc30
commit 48128acb76
3 changed files with 30 additions and 26 deletions
+13
View File
@@ -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
+1 -16
View File
@@ -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];
+16 -10
View File
@@ -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);