Skip to content

Commit

Permalink
Auto-release group iterators (project-chip#36127)
Browse files Browse the repository at this point in the history
* Fix up group iterator releasing

* Add missing bracket

* Renames and comments

* Restyled by clang-format

* Update src/app/WriteHandler.cpp

Co-authored-by: Arkadiusz Bokowy <[email protected]>

---------

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Arkadiusz Bokowy <[email protected]>
  • Loading branch information
3 people authored Oct 17, 2024
1 parent 1ae2e6e commit bdfc0d1
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/app/WriteHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,31 @@
namespace chip {
namespace app {

namespace {

/// Wraps a EndpointIterator and ensures that `::Release()` is called
/// for the iterator (assuming it is non-null)
class AutoReleaseGroupEndpointIterator
{
public:
explicit AutoReleaseGroupEndpointIterator(Credentials::GroupDataProvider::EndpointIterator * iterator) : mIterator(iterator) {}
~AutoReleaseGroupEndpointIterator()
{
if (mIterator != nullptr)
{
mIterator->Release();
}
}

bool IsNull() const { return mIterator == nullptr; }
bool Next(Credentials::GroupDataProvider::GroupEndpoint & item) { return mIterator->Next(item); }

private:
Credentials::GroupDataProvider::EndpointIterator * mIterator;
};

} // namespace

using namespace Protocols::InteractionModel;
using Status = Protocols::InteractionModel::Status;

Expand Down Expand Up @@ -436,10 +461,6 @@ CHIP_ERROR WriteHandler::ProcessGroupAttributeDataIBs(TLV::TLVReader & aAttribut
ConcreteDataAttributePath dataAttributePath;
TLV::TLVReader reader = aAttributeDataIBsReader;

Credentials::GroupDataProvider::GroupEndpoint mapping;
Credentials::GroupDataProvider * groupDataProvider = Credentials::GetGroupDataProvider();
Credentials::GroupDataProvider::EndpointIterator * iterator;

err = element.Init(reader);
SuccessOrExit(err);

Expand All @@ -461,16 +482,17 @@ CHIP_ERROR WriteHandler::ProcessGroupAttributeDataIBs(TLV::TLVReader & aAttribut
"Received group attribute write for Group=%u Cluster=" ChipLogFormatMEI " attribute=" ChipLogFormatMEI,
groupId, ChipLogValueMEI(dataAttributePath.mClusterId), ChipLogValueMEI(dataAttributePath.mAttributeId));

iterator = groupDataProvider->IterateEndpoints(fabric);
VerifyOrExit(iterator != nullptr, err = CHIP_ERROR_NO_MEMORY);
AutoReleaseGroupEndpointIterator iterator(Credentials::GetGroupDataProvider()->IterateEndpoints(fabric));
VerifyOrExit(!iterator.IsNull(), err = CHIP_ERROR_NO_MEMORY);

bool shouldReportListWriteEnd = ShouldReportListWriteEnd(
mProcessingAttributePath, mStateFlags.Has(StateBits::kProcessingAttributeIsList), dataAttributePath);
bool shouldReportListWriteBegin = false; // This will be set below.

std::optional<bool> isListAttribute = std::nullopt;

while (iterator->Next(mapping))
Credentials::GroupDataProvider::GroupEndpoint mapping;
while (iterator.Next(mapping))
{
if (groupId != mapping.group_id)
{
Expand Down Expand Up @@ -552,7 +574,6 @@ CHIP_ERROR WriteHandler::ProcessGroupAttributeDataIBs(TLV::TLVReader & aAttribut
dataAttributePath.mEndpointId = kInvalidEndpointId;
mStateFlags.Set(StateBits::kProcessingAttributeIsList, dataAttributePath.IsListOperation());
mProcessingAttributePath.SetValue(dataAttributePath);
iterator->Release();
}

if (CHIP_END_OF_TLV == err)
Expand Down

0 comments on commit bdfc0d1

Please sign in to comment.