Skip to content

Commit

Permalink
Use RAII for group session iteration (project-chip#32970) (project-ch…
Browse files Browse the repository at this point in the history
…ip#33122)

* Use RAII for iterator management

* Restyle

* Fix typo

* Fix naming a bit now that I made this a template

* Make it clear that class member is initialized
  • Loading branch information
andy31415 authored Apr 24, 2024
1 parent ed86881 commit 29ed2f9
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ using Transport::SecureSession;
namespace {
Global<GroupPeerTable> gGroupPeerTable;

/// RAII class for iterators that guarantees that Release() will be called
/// on the underlying type
template <typename Releasable>
class AutoRelease
{
public:
AutoRelease(Releasable * iter) : mIter(iter) {}
~AutoRelease() { Release(); }

Releasable * operator->() { return mIter; }
const Releasable * operator->() const { return mIter; }

bool IsNull() const { return mIter == nullptr; }

void Release()
{
VerifyOrReturn(mIter != nullptr);
mIter->Release();
mIter = nullptr;
}

private:
Releasable * mIter = nullptr;
};

// Helper function that strips off the interface ID from a peer address that is
// not an IPv6 link-local address. For any other address type we should rely on
// the device's routing table to route messages sent. Forcing messages down a
Expand Down Expand Up @@ -883,8 +908,11 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack

// Trial decryption with GroupDataProvider
Credentials::GroupDataProvider::GroupSession groupContext;
auto iter = groups->IterateGroupSessions(partialPacketHeader.GetSessionId());
if (iter == nullptr)

AutoRelease<Credentials::GroupDataProvider::GroupSessionIterator> iter(
groups->IterateGroupSessions(partialPacketHeader.GetSessionId()));

if (iter.IsNull())
{
ChipLogError(Inet, "Failed to retrieve Groups iterator. Discarding everything");
return;
Expand Down Expand Up @@ -931,7 +959,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
}
#endif // CHIP_CONFIG_PRIVACY_ACCEPT_NONSPEC_SVE2
}
iter->Release();
iter.Release();

if (!decrypted)
{
Expand Down Expand Up @@ -969,7 +997,6 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
gGroupPeerTable->FindOrAddPeer(groupContext.fabric_index, packetHeaderCopy.GetSourceNodeId().Value(),
packetHeaderCopy.IsSecureSessionControlMsg(), counter))
{

if (Credentials::GroupDataProvider::SecurityPolicy::kTrustFirst == groupContext.security_policy)
{
err = counter->VerifyOrTrustFirstGroup(packetHeaderCopy.GetMessageCounter());
Expand Down

0 comments on commit 29ed2f9

Please sign in to comment.