Initialize length parameter before calling otPlatSettingsGet(). (#1116)

- Handle input length in `otPlatSettingsGet()` example.
This commit is contained in:
Jonathan Hui
2017-01-06 15:32:08 -08:00
committed by GitHub
parent 3e6546c658
commit 7c743f3d8d
4 changed files with 46 additions and 21 deletions
+21 -12
View File
@@ -341,6 +341,7 @@ ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex,
{
ThreadError error = kThreadError_NotFound;
uint32_t address = sSettingsBaseAddress + kSettingsFlagSize;
uint16_t valueLength = 0;
int index = 0;
(void)aInstance;
@@ -362,18 +363,22 @@ ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex,
{
if (index == aIndex)
{
uint16_t readLength = block.length;
// only perform read if an input buffer was passed in
if (aValue != NULL && aValueLength != NULL)
{
// adjust read length if input buffer length is smaller
if (readLength > *aValueLength)
{
readLength = *aValueLength;
}
utilsFlashRead(address + sizeof(struct settingsBlock), aValue, readLength);
}
valueLength = readLength;
error = kThreadError_None;
if (aValueLength)
{
*aValueLength = block.length;
}
if (aValue)
{
VerifyOrExit(aValueLength, error = kThreadError_InvalidArgs);
utilsFlashRead(address + sizeof(struct settingsBlock), aValue, block.length);
}
}
index++;
@@ -383,7 +388,11 @@ ThreadError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex,
address += (getAlignLength(block.length) + sizeof(struct settingsBlock));
}
exit:
if (aValueLength != NULL)
{
*aValueLength = valueLength;
}
return error;
}
+11 -2
View File
@@ -428,8 +428,17 @@ int Dataset::Compare(const Dataset &aCompare) const
ThreadError Dataset::Restore(void)
{
return otPlatSettingsGet(mInstance, static_cast<uint16_t>(mType == Tlv::kActiveTimestamp ? kKeyActiveDataset :
kKeyPendingDataset), 0, mTlvs, &mLength);
ThreadError error;
uint16_t length = sizeof(mTlvs);
error = otPlatSettingsGet(mInstance, static_cast<uint16_t>(mType == Tlv::kActiveTimestamp ? kKeyActiveDataset :
kKeyPendingDataset), 0, mTlvs, &length);
SuccessOrExit(error);
mLength = length;
exit:
return error;
}
ThreadError Dataset::Store(void)
+3 -1
View File
@@ -268,9 +268,9 @@ ThreadError Mle::Restore(void)
mNetif.GetActiveDataset().Restore();
mNetif.GetPendingDataset().Restore();
length = sizeof(networkInfo);
SuccessOrExit(error = otPlatSettingsGet(mNetif.GetInstance(), kKeyNetworkInfo, 0,
reinterpret_cast<uint8_t *>(&networkInfo), &length));
VerifyOrExit(length == sizeof(networkInfo), error = kThreadError_NotFound);
VerifyOrExit(networkInfo.mDeviceState >= kDeviceStateChild, error = kThreadError_NotFound);
@@ -284,8 +284,10 @@ ThreadError Mle::Restore(void)
if (networkInfo.mDeviceState == kDeviceStateChild)
{
length = sizeof(mParent);
SuccessOrExit(error = otPlatSettingsGet(mNetif.GetInstance(), kKeyParentInfo, 0,
reinterpret_cast<uint8_t *>(&mParent), &length));
VerifyOrExit(length == sizeof(mParent), error = kThreadError_NotFound);
}
else if (networkInfo.mDeviceState == kDeviceStateRouter || networkInfo.mDeviceState == kDeviceStateLeader)
{
+11 -6
View File
@@ -3339,16 +3339,19 @@ exit:
ThreadError MleRouter::RestoreChildren(void)
{
ThreadError error = kThreadError_None;
Child *child;
otChildInfo childInfo;
uint16_t length;
for (uint8_t i = 0; i < kMaxChildren; i++)
{
Child *child;
otChildInfo childInfo;
uint16_t length;
length = sizeof(childInfo);
SuccessOrExit(otPlatSettingsGet(mNetif.GetInstance(), kKeyChildInfo, i,
reinterpret_cast<uint8_t *>(&childInfo), &length));
VerifyOrExit((child = NewChild()) != NULL, error = kThreadError_NoBufs);
VerifyOrExit(length == sizeof(childInfo), ;);
VerifyOrExit((child = NewChild()) != NULL, error = kThreadError_NoBufs);
memset(child, 0, sizeof(*child));
memcpy(&child->mMacAddr, &childInfo.mExtAddress, sizeof(child->mMacAddr));
@@ -3369,13 +3372,15 @@ exit:
ThreadError MleRouter::RemoveStoredChild(uint16_t aChildRloc16)
{
ThreadError error = kThreadError_NotFound;
otChildInfo childInfo;
uint16_t length;
for (uint8_t i = 0; i < kMaxChildren; i++)
{
otChildInfo childInfo;
uint16_t length = sizeof(childInfo);
SuccessOrExit(otPlatSettingsGet(mNetif.GetInstance(), kKeyChildInfo, i,
reinterpret_cast<uint8_t *>(&childInfo), &length));
VerifyOrExit(length == sizeof(childInfo), ;);
if (childInfo.mRloc16 == aChildRloc16)
{