From 93acc99a148eeeac02bf78ee1aa54ab0d9feea03 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 29 Mar 2022 08:28:04 -0700 Subject: [PATCH] Adjust the logic to release UserLabelIterator --- .../user-label-server/user-label-server.cpp | 29 +++++++++++-------- src/include/platform/DeviceInfoProvider.h | 4 +-- src/platform/Linux/DeviceInfoProviderImpl.cpp | 4 +-- src/platform/Linux/DeviceInfoProviderImpl.h | 4 +-- 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/app/clusters/user-label-server/user-label-server.cpp b/src/app/clusters/user-label-server/user-label-server.cpp index 12b2cd880d5724..e24d19a81f57f8 100644 --- a/src/app/clusters/user-label-server/user-label-server.cpp +++ b/src/app/clusters/user-label-server/user-label-server.cpp @@ -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 { diff --git a/src/include/platform/DeviceInfoProvider.h b/src/include/platform/DeviceInfoProvider.h index e4fc5135352e24..6861c2eae0c591 100644 --- a/src/include/platform/DeviceInfoProvider.h +++ b/src/include/platform/DeviceInfoProvider.h @@ -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 @@ -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. */ diff --git a/src/platform/Linux/DeviceInfoProviderImpl.cpp b/src/platform/Linux/DeviceInfoProviderImpl.cpp index 20e2968e26373f..1c5ad3e55c0fb8 100644 --- a/src/platform/Linux/DeviceInfoProviderImpl.cpp +++ b/src/platform/Linux/DeviceInfoProviderImpl.cpp @@ -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); diff --git a/src/platform/Linux/DeviceInfoProviderImpl.h b/src/platform/Linux/DeviceInfoProviderImpl.h index 6a279b34e6722c..8e72f0e910c668 100644 --- a/src/platform/Linux/DeviceInfoProviderImpl.h +++ b/src/platform/Linux/DeviceInfoProviderImpl.h @@ -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;