Skip to content

Commit

Permalink
[ESP32] Do not crash if storage delegate is nullptr (#21822)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp authored and pull[bot] committed Aug 21, 2023
1 parent c109be2 commit 1714713
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/platform/ESP32/ESP32DeviceInfoProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,22 @@ bool ESP32DeviceInfoProvider::FixedLabelIteratorImpl::Next(FixedLabelType & outp

CHIP_ERROR ESP32DeviceInfoProvider::SetUserLabelLength(EndpointId endpoint, size_t val)
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
DefaultStorageKeyAllocator keyAlloc;
return mStorage->SyncSetKeyValue(keyAlloc.UserLabelLengthKey(endpoint), &val, static_cast<uint16_t>(sizeof(val)));
}

CHIP_ERROR ESP32DeviceInfoProvider::GetUserLabelLength(EndpointId endpoint, size_t & val)
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
DefaultStorageKeyAllocator keyAlloc;
uint16_t len = static_cast<uint16_t>(sizeof(val));
return mStorage->SyncGetKeyValue(keyAlloc.UserLabelLengthKey(endpoint), &val, len);
}

CHIP_ERROR ESP32DeviceInfoProvider::SetUserLabelAt(EndpointId endpoint, size_t index, const UserLabelType & userLabel)
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
DefaultStorageKeyAllocator keyAlloc;
uint8_t buf[UserLabelTLVMaxSize()];
TLV::TLVWriter writer;
Expand All @@ -125,6 +128,7 @@ CHIP_ERROR ESP32DeviceInfoProvider::SetUserLabelAt(EndpointId endpoint, size_t i

CHIP_ERROR ESP32DeviceInfoProvider::DeleteUserLabelAt(EndpointId endpoint, size_t index)
{
VerifyOrReturnError(mStorage != nullptr, CHIP_ERROR_INCORRECT_STATE);
DefaultStorageKeyAllocator keyAlloc;
return mStorage->SyncDeleteKeyValue(keyAlloc.UserLabelIndexKey(endpoint, index));
}
Expand All @@ -146,15 +150,14 @@ ESP32DeviceInfoProvider::UserLabelIteratorImpl::UserLabelIteratorImpl(ESP32Devic

bool ESP32DeviceInfoProvider::UserLabelIteratorImpl::Next(UserLabelType & output)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrReturnError(mProvider.mStorage != nullptr, false);
VerifyOrReturnError(mIndex < mTotal, false);

DefaultStorageKeyAllocator keyAlloc;
uint8_t buf[UserLabelTLVMaxSize()];
uint16_t len = static_cast<uint16_t>(sizeof(buf));

err = mProvider.mStorage->SyncGetKeyValue(keyAlloc.UserLabelIndexKey(mEndpoint, mIndex), buf, len);
CHIP_ERROR err = mProvider.mStorage->SyncGetKeyValue(keyAlloc.UserLabelIndexKey(mEndpoint, mIndex), buf, len);
VerifyOrReturnError(err == CHIP_NO_ERROR, false);

TLV::ContiguousBufferTLVReader reader;
Expand Down

0 comments on commit 1714713

Please sign in to comment.