Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RAII for group session iteration #33122

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use RAII for group session iteration (#32970)
* 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
andy31415 authored and andreilitvin committed Apr 23, 2024
commit 2c45ebea83426850a9b6f05fd5bc82723cccbbca
35 changes: 31 additions & 4 deletions src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
@@ -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
@@ -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;
@@ -931,7 +959,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
}
#endif // CHIP_CONFIG_PRIVACY_ACCEPT_NONSPEC_SVE2
}
iter->Release();
iter.Release();

if (!decrypted)
{
@@ -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());