From 514ced1243ecdce6eac6a168c92af0d051158a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 16 Apr 2020 10:09:53 +0200 Subject: [PATCH] crypto: Don't add the same session twice to the memory session store. --- src/crypto/memory_stores.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/crypto/memory_stores.rs b/src/crypto/memory_stores.rs index b0e39bb6..260bda56 100644 --- a/src/crypto/memory_stores.rs +++ b/src/crypto/memory_stores.rs @@ -37,7 +37,10 @@ impl SessionStore { } /// Add a session to the store. - pub async fn add(&mut self, session: Session) { + /// + /// Returns true if the the session was already in the store, false + /// otherwise. + pub async fn add(&mut self, session: Session) -> bool { if !self.entries.contains_key(&*session.sender_key) { self.entries.insert( session.sender_key.to_string(), @@ -45,7 +48,13 @@ impl SessionStore { ); } let sessions = self.entries.get_mut(&*session.sender_key).unwrap(); - sessions.lock().await.push(session); + + if !sessions.lock().await.contains(&session) { + sessions.lock().await.push(session); + false + } else { + true + } } /// Get all the sessions that belong to the given sender key. @@ -75,6 +84,9 @@ impl GroupSessionStore { } /// Add a inbound group session to the store. + /// + /// Returns true if the the session was already in the store, false + /// otherwise. pub fn add(&mut self, session: InboundGroupSession) -> bool { if !self.entries.contains_key(&session.room_id) { let room_id = &*session.room_id; @@ -228,7 +240,9 @@ mod test { let (account, session) = get_account_and_session().await; let mut store = SessionStore::new(); - store.add(session.clone()).await; + + assert!(!store.add(session.clone()).await); + assert!(store.add(session.clone()).await); let sessions = store.get(&session.sender_key).unwrap(); let sessions = sessions.lock().await;