@@ -57,6 +57,32 @@ using Transport::SecureSession;
57
57
58
58
namespace {
59
59
Global<GroupPeerTable> gGroupPeerTable ;
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
+
60
86
} // namespace
61
87
62
88
uint32_t EncryptedPacketBufferHandle::GetMessageCounter () const
@@ -868,8 +894,11 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
868
894
869
895
// Trial decryption with GroupDataProvider
870
896
Credentials::GroupDataProvider::GroupSession groupContext;
871
- auto iter = groups->IterateGroupSessions (partialPacketHeader.GetSessionId ());
872
- if (iter == nullptr )
897
+
898
+ AutoRelease<Credentials::GroupDataProvider::GroupSessionIterator> iter (
899
+ groups->IterateGroupSessions (partialPacketHeader.GetSessionId ()));
900
+
901
+ if (iter.IsNull ())
873
902
{
874
903
ChipLogError (Inet, " Failed to retrieve Groups iterator. Discarding everything" );
875
904
return ;
@@ -916,7 +945,7 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
916
945
}
917
946
#endif // CHIP_CONFIG_PRIVACY_ACCEPT_NONSPEC_SVE2
918
947
}
919
- iter-> Release ();
948
+ iter. Release ();
920
949
921
950
if (!decrypted)
922
951
{
@@ -954,7 +983,6 @@ void SessionManager::SecureGroupMessageDispatch(const PacketHeader & partialPack
954
983
gGroupPeerTable ->FindOrAddPeer (groupContext.fabric_index , packetHeaderCopy.GetSourceNodeId ().Value (),
955
984
packetHeaderCopy.IsSecureSessionControlMsg (), counter))
956
985
{
957
-
958
986
if (Credentials::GroupDataProvider::SecurityPolicy::kTrustFirst == groupContext.security_policy )
959
987
{
960
988
err = counter->VerifyOrTrustFirstGroup (packetHeaderCopy.GetMessageCounter ());
0 commit comments