Skip to content

Commit 29ed2f9

Browse files
authored
Use RAII for group session iteration (#32970) (#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
1 parent ed86881 commit 29ed2f9

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/transport/SessionManager.cpp

+31-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ using Transport::SecureSession;
5858
namespace {
5959
Global<GroupPeerTable> gGroupPeerTable;
6060

61+
/// RAII class for iterators that guarantees that Release() will be called
62+
/// on the underlying type
63+
template <typename Releasable>
64+
class AutoRelease
65+
{
66+
public:
67+
AutoRelease(Releasable * iter) : mIter(iter) {}
68+
~AutoRelease() { Release(); }
69+
70+
Releasable * operator->() { return mIter; }
71+
const Releasable * operator->() const { return mIter; }
72+
73+
bool IsNull() const { return mIter == nullptr; }
74+
75+
void Release()
76+
{
77+
VerifyOrReturn(mIter != nullptr);
78+
mIter->Release();
79+
mIter = nullptr;
80+
}
81+
82+
private:
83+
Releasable * mIter = nullptr;
84+
};
85+
6186
// Helper function that strips off the interface ID from a peer address that is
6287
// not an IPv6 link-local address. For any other address type we should rely on
6388
// the device's routing table to route messages sent. Forcing messages down a
@@ -883,8 +908,11 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
883908

884909
// Trial decryption with GroupDataProvider
885910
Credentials::GroupDataProvider::GroupSession groupContext;
886-
auto iter = groups->IterateGroupSessions(partialPacketHeader.GetSessionId());
887-
if (iter == nullptr)
911+
912+
AutoRelease<Credentials::GroupDataProvider::GroupSessionIterator> iter(
913+
groups->IterateGroupSessions(partialPacketHeader.GetSessionId()));
914+
915+
if (iter.IsNull())
888916
{
889917
ChipLogError(Inet, "Failed to retrieve Groups iterator. Discarding everything");
890918
return;
@@ -931,7 +959,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
931959
}
932960
#endif // CHIP_CONFIG_PRIVACY_ACCEPT_NONSPEC_SVE2
933961
}
934-
iter->Release();
962+
iter.Release();
935963

936964
if (!decrypted)
937965
{
@@ -969,7 +997,6 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
969997
gGroupPeerTable->FindOrAddPeer(groupContext.fabric_index, packetHeaderCopy.GetSourceNodeId().Value(),
970998
packetHeaderCopy.IsSecureSessionControlMsg(), counter))
971999
{
972-
9731000
if (Credentials::GroupDataProvider::SecurityPolicy::kTrustFirst == groupContext.security_policy)
9741001
{
9751002
err = counter->VerifyOrTrustFirstGroup(packetHeaderCopy.GetMessageCounter());

0 commit comments

Comments
 (0)