mirror of
https://github.com/espressif/openthread.git
synced 2026-06-05 21:14:49 +00:00
[message] introduce ReadAndAdvance() to simplify sequential parsing (#13106)
This commit introduces `Message::ReadAndAdvance()` and its template flavor to the `Message` class. This helper method reads data from a `Message` at a given `OffsetRange` and advances the `OffsetRange` by the number of bytes read upon success. Sequential parsing of structured data (such as TLVs or protocol headers) is a common pattern across the OpenThread codebase. Previously, this required two separate calls: one to `Read()` and another to `AdvanceOffset()`. The new `ReadAndAdvance()` method consolidates these into a single, safer operation that ensures the offset is only advanced if the read operation succeeds. This commit updates numerous call sites across the core stack (MLE, BBR, DatasetManager, NetworkDiagnostic, DHCPv6, etc.) to use the new helper, improving code clarity and reducing boilerplate.
This commit is contained in:
committed by
GitHub
parent
27737f616e
commit
a84fc2e50b
@@ -342,10 +342,9 @@ void Dhcp6Msg::ParseFrom(const Message &aMessage)
|
||||
Clear();
|
||||
offsetRange.InitFromMessageFullLength(aMessage);
|
||||
|
||||
SuccessOrQuit(aMessage.Read(offsetRange, header));
|
||||
SuccessOrQuit(aMessage.ReadAndAdvance(offsetRange, header));
|
||||
mMsgType = header.GetMsgType();
|
||||
mTransactionId = header.GetTransactionId();
|
||||
offsetRange.AdvanceOffset(sizeof(header));
|
||||
|
||||
while (!offsetRange.IsEmpty())
|
||||
{
|
||||
@@ -432,8 +431,7 @@ void Dhcp6Msg::ParseFrom(const Message &aMessage)
|
||||
break;
|
||||
|
||||
case Dhcp6::Option::kIaPd:
|
||||
SuccessOrQuit(aMessage.Read(optionOffsetRange, iaPdOption));
|
||||
optionOffsetRange.AdvanceOffset(sizeof(iaPdOption));
|
||||
SuccessOrQuit(aMessage.ReadAndAdvance(optionOffsetRange, iaPdOption));
|
||||
VerifyOrQuit(!mIaPds.ContainsMatching(iaPdOption.GetIaid()));
|
||||
|
||||
iaPd = mIaPds.PushBack();
|
||||
|
||||
Reference in New Issue
Block a user