Skip to content

Commit

Permalink
Adjust the logic to release UserLabelIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Mar 30, 2022
1 parent e9382f7 commit 93acc99
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
29 changes: 17 additions & 12 deletions src/app/clusters/user-label-server/user-label-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,27 @@ CHIP_ERROR UserLabelAttrAccess::ReadLabelList(EndpointId endpoint, AttributeValu

DeviceLayer::DeviceInfoProvider::UserLabelIterator * it = DeviceLayer::GetDeviceInfoProvider()->IterateUserLabel(endpoint);

if (it && it->Count() > 0)
if (it)
{
err = aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
CHIP_ERROR error = CHIP_NO_ERROR;
UserLabel::Structs::LabelStruct::Type userlabel;
if (it->Count() > 0)
{
err = aEncoder.EncodeList([&it](const auto & encoder) -> CHIP_ERROR {
UserLabel::Structs::LabelStruct::Type userlabel;

while (it->Next(userlabel))
{
SuccessOrExit(error = encoder.Encode(userlabel));
}
while (it->Next(userlabel))
{
ReturnErrorOnFailure(encoder.Encode(userlabel));
}

exit:
it->Release();
return CHIP_NO_ERROR;
});
}
else
{
err = aEncoder.EncodeEmptyList();
}

return error;
});
it->Release();
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/include/platform/DeviceInfoProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace chip {
namespace DeviceLayer {

static constexpr size_t kMaxUserLabelListLength = 10;
static constexpr size_t kMaxLabelLength = 16;
static constexpr size_t kMaxLabelNameLength = 16;
static constexpr size_t kMaxLabelValueLength = 16;

class DeviceInfoProvider
Expand All @@ -48,7 +48,7 @@ class DeviceInfoProvider
*/
virtual size_t Count() = 0;
/**
* @param[out] item Value associated with the next element in the iteration.
* @param[out] item Value associated with the next element in the iteration.
* @retval true if the next entry is successfully retrieved.
* @retval false if no more entries can be found.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Linux/DeviceInfoProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ bool DeviceInfoProviderImpl::UserLabelIteratorImpl::Next(UserLabelType & output)

if (err == CHIP_NO_ERROR)
{
VerifyOrReturnError(std::strlen(labelPtr) <= kMaxLabelLength, false);
VerifyOrReturnError(std::strlen(labelPtr) <= kMaxLabelNameLength, false);
VerifyOrReturnError(std::strlen(valuePtr) <= kMaxLabelValueLength, false);

Platform::CopyString(mUserLabelBuf, kMaxLabelLength, labelPtr);
Platform::CopyString(mUserLabelBuf, kMaxLabelNameLength, labelPtr);
Platform::CopyString(mUserValueBuf, kMaxLabelValueLength, valuePtr);

output.label = CharSpan::fromCharString(mUserLabelBuf);
Expand Down
4 changes: 2 additions & 2 deletions src/platform/Linux/DeviceInfoProviderImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class DeviceInfoProviderImpl : public DeviceInfoProvider
EndpointId mEndpoint = 0;
size_t mIndex = 0;
size_t mTotal = 0;
char mUserLabelBuf[kMaxLabelLength];
char mUserValueBuf[kMaxLabelValueLength];
char mUserLabelBuf[kMaxLabelNameLength + 1];
char mUserValueBuf[kMaxLabelValueLength + 1];
};

CHIP_ERROR SetUserLabelCount(EndpointId endpoint, size_t val) override;
Expand Down

0 comments on commit 93acc99

Please sign in to comment.