From 82fb33911957361c0ddeaafb7fc3fdf715d7b6ac Mon Sep 17 00:00:00 2001
From: Olivier Dion <olivier.dion@savoirfairelinux.com>
Date: Fri, 22 Apr 2022 14:21:55 -0400
Subject: [PATCH] sip/sip_utils: Duplicate contact header

pjsip scans the contact header but keep a string view of it internally instead of
copying it.  Thus, duplicate the contact header and bind its lifetime to the
pool allocator of the tdata structure.

Gitlab: #668

Change-Id: Ief31fcc6033b394dcb43ee0403f9459654d29f1f
---
 src/sip/sip_utils.cpp | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/sip/sip_utils.cpp b/src/sip/sip_utils.cpp
index bdf017c4b2..31d295a4f6 100644
--- a/src/sip/sip_utils.cpp
+++ b/src/sip/sip_utils.cpp
@@ -180,7 +180,15 @@ addContactHeader(const std::string& contactHdr, pjsip_tx_data* tdata)
         JAMI_WARN("Contact header won't be added (empty string)");
         return;
     }
-    auto pjContact = sip_utils::CONST_PJ_STR(contactHdr);
+
+    /*
+     * Duplicate contact header because tdata->msg keep a reference to it and
+     * can be used in a callback after destruction of the contact header in
+     * Jami.  Bind lifetime of the duplicated string to the pool allocator of
+     * tdata.
+     */
+    auto pjContact = pj_strdup3(tdata->pool, contactHdr.c_str());
+
     pjsip_contact_hdr* contact = pjsip_contact_hdr_create(tdata->pool);
     contact->uri = pjsip_parse_uri(tdata->pool,
                                    pjContact.ptr,
-- 
GitLab