From 0932ef43ade9485b18d751de1f26267227488de9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Fri, 27 Nov 2020 09:46:10 -0500
Subject: [PATCH] sipcall: fix asan crash on callAVStreams

Change-Id: I6463e8315a84df28fb7a7e8b52f6c8e18899fa44
GitLab: #359
---
 src/sip/sipcall.cpp | 17 ++++++++++++-----
 src/sip/sipcall.h   |  1 +
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/sip/sipcall.cpp b/src/sip/sipcall.cpp
index 5a22fbcc27..2e22f42935 100644
--- a/src/sip/sipcall.cpp
+++ b/src/sip/sipcall.cpp
@@ -162,12 +162,16 @@ SIPCall::createCallAVStream(const StreamData& StreamData,
                             const std::shared_ptr<MediaStreamSubject>& mediaStreamSubject)
 {
     const std::string AVStreamId = StreamData.id + std::to_string(StreamData.direction);
-    callAVStreams[AVStreamId] = mediaStreamSubject;
-    streamSource.attachPriorityObserver(callAVStreams[AVStreamId]);
+    std::lock_guard<std::mutex> lk(avStreamsMtx_);
+    auto it = callAVStreams.find(AVStreamId);
+    if (it != callAVStreams.end())
+        return;
+    it = callAVStreams.insert(it, {AVStreamId, mediaStreamSubject});
+    streamSource.attachPriorityObserver(it->second);
     jami::Manager::instance()
         .getJamiPluginManager()
         .getCallServicesManager()
-        .createAVSubject(StreamData, callAVStreams[AVStreamId]);
+        .createAVSubject(StreamData, it->second);
 }
 #endif // ENABLE_PLUGIN
 
@@ -1251,8 +1255,11 @@ SIPCall::stopAllMedia()
     videortp_->stop();
 #endif
 #ifdef ENABLE_PLUGIN
-    callAVStreams.erase(getCallId() + "0");
-    callAVStreams.erase(getCallId() + "1");
+    {
+        std::lock_guard<std::mutex> lk(avStreamsMtx_);
+        callAVStreams.erase(getCallId() + "0");
+        callAVStreams.erase(getCallId() + "1");
+    }
     jami::Manager::instance().getJamiPluginManager().getCallServicesManager().clearAVSubject(
         getCallId());
 #endif
diff --git a/src/sip/sipcall.h b/src/sip/sipcall.h
index 3365299a49..652e06d2c0 100644
--- a/src/sip/sipcall.h
+++ b/src/sip/sipcall.h
@@ -279,6 +279,7 @@ private:
     void createCallAVStreams();
 #endif // ENABLE_PLUGIN
 
+    std::mutex avStreamsMtx_ {};
     std::map<std::string, std::shared_ptr<MediaStreamSubject>> callAVStreams;
 
     void setCallMediaLocal();
-- 
GitLab