@@ -58,6 +58,31 @@ using Transport::SecureSession;
58
58
namespace {
59
59
Global<GroupPeerTable> gGroupPeerTable ;
60
60
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
+
61
86
// Helper function that strips off the interface ID from a peer address that is
62
87
// not an IPv6 link-local address. For any other address type we should rely on
63
88
// the device's routing table to route messages sent. Forcing messages down a
@@ -883,8 +908,11 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
883
908
884
909
// Trial decryption with GroupDataProvider
885
910
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 ())
888
916
{
889
917
ChipLogError (Inet, " Failed to retrieve Groups iterator. Discarding everything" );
890
918
return ;
@@ -931,7 +959,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
931
959
}
932
960
#endif // CHIP_CONFIG_PRIVACY_ACCEPT_NONSPEC_SVE2
933
961
}
934
- iter-> Release ();
962
+ iter. Release ();
935
963
936
964
if (!decrypted)
937
965
{
@@ -969,7 +997,6 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
969
997
gGroupPeerTable ->FindOrAddPeer (groupContext.fabric_index , packetHeaderCopy.GetSourceNodeId ().Value (),
970
998
packetHeaderCopy.IsSecureSessionControlMsg (), counter))
971
999
{
972
-
973
1000
if (Credentials::GroupDataProvider::SecurityPolicy::kTrustFirst == groupContext.security_policy )
974
1001
{
975
1002
err = counter->VerifyOrTrustFirstGroup (packetHeaderCopy.GetMessageCounter ());
0 commit comments