From e9261b68424cec2ee83534844c08e55070ecd339 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Sun, 9 Feb 2020 18:21:59 -0800 Subject: [PATCH] [settings] initialize structure before reading from storage (#4499) This commit initializes the settings structures before reading. This helps prepare for a following commit that allows reading stored values that are shorter than expected. This commit also renames the Clear() methods to Init(). --- src/core/common/settings.cpp | 3 +++ src/core/common/settings.hpp | 6 +++--- src/core/thread/mle.cpp | 4 ++-- src/core/thread/mle_router.cpp | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/core/common/settings.cpp b/src/core/common/settings.cpp index d56d00c33..37d469c1b 100644 --- a/src/core/common/settings.cpp +++ b/src/core/common/settings.cpp @@ -143,6 +143,7 @@ otError Settings::ReadNetworkInfo(NetworkInfo &aNetworkInfo) const { otError error; + aNetworkInfo.Init(); SuccessOrExit(error = ReadFixedSize(kKeyNetworkInfo, &aNetworkInfo, sizeof(NetworkInfo))); LogNetworkInfo("Read", aNetworkInfo); @@ -186,6 +187,7 @@ otError Settings::ReadParentInfo(ParentInfo &aParentInfo) const { otError error; + aParentInfo.Init(); SuccessOrExit(error = ReadFixedSize(kKeyParentInfo, &aParentInfo, sizeof(ParentInfo))); LogParentInfo("Read", aParentInfo); @@ -291,6 +293,7 @@ void Settings::ChildInfoIterator::Read(void) uint16_t size = sizeof(ChildInfo); otError error; + mChildInfo.Init(); SuccessOrExit(error = otPlatSettingsGet(&GetInstance(), kKeyChildInfo, mIndex, reinterpret_cast(&mChildInfo), &size)); VerifyOrExit(size >= sizeof(ChildInfo), error = OT_ERROR_NOT_FOUND); diff --git a/src/core/common/settings.hpp b/src/core/common/settings.hpp index 76854f7e1..e3623dcf7 100644 --- a/src/core/common/settings.hpp +++ b/src/core/common/settings.hpp @@ -92,7 +92,7 @@ public: * This method clears the struct object (setting all the fields to zero). * */ - void Clear(void) { memset(this, 0, sizeof(*this)); } + void Init(void) { memset(this, 0, sizeof(*this)); } /** * This method returns the Thread role. @@ -271,7 +271,7 @@ public: * This method clears the struct object (setting all the fields to zero). * */ - void Clear(void) { memset(this, 0, sizeof(*this)); } + void Init(void) { memset(this, 0, sizeof(*this)); } /** * This method returns the extended address. @@ -305,7 +305,7 @@ public: * This method clears the struct object (setting all the fields to zero). * */ - void Clear(void) { memset(this, 0, sizeof(*this)); } + void Init(void) { memset(this, 0, sizeof(*this)); } /** * This method returns the extended address. diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 9224c3e27..9cd95e7c5 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -446,7 +446,7 @@ otError Mle::Store(void) otError error = OT_ERROR_NONE; Settings::NetworkInfo networkInfo; - networkInfo.Clear(); + networkInfo.Init(); if (IsAttached()) { @@ -464,7 +464,7 @@ otError Mle::Store(void) { Settings::ParentInfo parentInfo; - parentInfo.Clear(); + parentInfo.Init(); parentInfo.SetExtAddress(mParent.GetExtAddress()); SuccessOrExit(error = Get().SaveParentInfo(parentInfo)); diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index 9822cbdfc..b2b0596b2 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -3617,7 +3617,7 @@ otError MleRouter::StoreChild(const Child &aChild) IgnoreReturnValue(RemoveStoredChild(aChild.GetRloc16())); - childInfo.Clear(); + childInfo.Init(); childInfo.SetExtAddress(aChild.GetExtAddress()); childInfo.SetTimeout(aChild.GetTimeout()); childInfo.SetRloc16(aChild.GetRloc16());