From 8a73d38de39448a54c50a829924aa1d0a27fc39f Mon Sep 17 00:00:00 2001
From: pierre-luc <pierre-luc.bacon@savoirfairelinux.com>
Date: Tue, 11 Aug 2009 14:27:18 -0400
Subject: [PATCH] [#2027] Fix segmentation fault when showMessage callback is
 called after an error occured with libzrtpcpp. Also, exceptions in
 AudioRtpFactory are now derived from some more specialized types.

---
 .../src/config/accountwindow.c                |  1 +
 .../src/audio/audiortp/AudioRtpFactory.cpp    | 43 ++++++++-----------
 .../src/audio/audiortp/AudioRtpFactory.h      | 23 ++++------
 .../audio/audiortp/ZrtpSessionCallback.cpp    | 15 ++++---
 4 files changed, 37 insertions(+), 45 deletions(-)

diff --git a/sflphone-client-gnome/src/config/accountwindow.c b/sflphone-client-gnome/src/config/accountwindow.c
index d12f0f60aa..18d6f225c6 100644
--- a/sflphone-client-gnome/src/config/accountwindow.c
+++ b/sflphone-client-gnome/src/config/accountwindow.c
@@ -754,6 +754,7 @@ show_account_window (account_t * a)
 			gchar* keyExchange = (gchar *)gtk_combo_box_get_active_text(GTK_COMBO_BOX(keyExchangeCombo));
             if (g_strcasecmp(keyExchange, "ZRTP") == 0) {
                 g_hash_table_replace(currentAccount->properties, g_strdup(ACCOUNT_SRTP_ENABLED), g_strdup("TRUE"));
+                g_hash_table_replace(currentAccount->properties, g_strdup(ACCOUNT_KEY_EXCHANGE), g_strdup("1"));
             } else {
                 g_hash_table_replace(currentAccount->properties, g_strdup(ACCOUNT_SRTP_ENABLED), g_strdup("FALSE"));
             }
diff --git a/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp b/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
index 4745bd9160..e603c13731 100644
--- a/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
+++ b/sflphone-common/src/audio/audiortp/AudioRtpFactory.cpp
@@ -58,8 +58,8 @@ namespace sfl {
         assert(ca);
        
         if (_rtpSession != NULL) {
-            _debugException("An audio rtp thread was already created but not \
-                             destroyed. Forcing it before continuing.\n");
+            _debugException("An audio rtp thread was already created but not" \
+                            "destroyed. Forcing it before continuing.\n");
             stop();
         }
         
@@ -113,27 +113,23 @@ namespace sfl {
     void AudioRtpFactory::start(void)
     {
         if (_rtpSession == NULL) {
-            throw AudioRtpFactoryException();
-        }
-        
-        try {
-            switch(_rtpSessionType) {
-                case Sdes:
-                case Symmetric:
-                    _debug("Starting symmetric rtp thread\n");
-                    if(static_cast<AudioSymmetricRtpSession *>(_rtpSession)->startRtpThread() != 0) {
-                        throw AudioRtpFactoryException();
-                    }
-                    break;
-                case Zrtp:
-                    if(static_cast<AudioZrtpSession *>(_rtpSession)->startRtpThread() != 0) {
-                        throw AudioRtpFactoryException();
-                    }
-                    break;       
-             }
-        } catch (...) {
-            throw AudioRtpFactoryException();
+            throw AudioRtpFactoryException("_rtpSession was null when trying to start audio thread");
         }
+         
+        switch(_rtpSessionType) {
+            case Sdes:
+            case Symmetric:
+                _debug("Starting symmetric rtp thread\n");
+                if(static_cast<AudioSymmetricRtpSession *>(_rtpSession)->startRtpThread() != 0) {
+                    throw AudioRtpFactoryException("Failed to start AudioSymmetricRtpSession thread");
+                }
+                break;
+            case Zrtp:
+                if(static_cast<AudioZrtpSession *>(_rtpSession)->startRtpThread() != 0) {
+                    throw AudioRtpFactoryException("Failed to start AudioZrtpSession thread");
+                }
+                break;       
+            }
     }
 
     void AudioRtpFactory::stop(void) 
@@ -141,8 +137,7 @@ namespace sfl {
         ost::MutexLock mutex(_audioRtpThreadMutex);
         _debug("Stopping audio rtp session\n");
         if (_rtpSession == NULL) {
-            _debugException("_rtpSession is null\n");
-            throw AudioRtpFactoryException();
+            throw AudioRtpFactoryException("_rtpSession is null when trying to stop");
         }
         try {
             switch(_rtpSessionType) {
diff --git a/sflphone-common/src/audio/audiortp/AudioRtpFactory.h b/sflphone-common/src/audio/audiortp/AudioRtpFactory.h
index 964a2b4f13..0eefcbfc82 100644
--- a/sflphone-common/src/audio/audiortp/AudioRtpFactory.h
+++ b/sflphone-common/src/audio/audiortp/AudioRtpFactory.h
@@ -19,7 +19,7 @@
 #ifndef __SFL_AUDIO_RTP_FACTORY_H__
 #define __SFL_AUDIO_RTP_FACTORY_H__
 
-#include <exception>
+#include <stdexcept>
 #include <cc++/thread.h>
 
 class SIPCall;
@@ -38,20 +38,15 @@ namespace sfl {
         Sdes
     } RtpMethod;
 
-    class UnsupportedRtpSessionType: public std::exception
-    {
-      virtual const char* what() const throw()
-      {
-        return "Could not create RTP session of the given type";
-      }
-    };
 
-    class AudioRtpFactoryException: public std::exception
-    {
-      virtual const char* what() const throw()
-      {
-        return "An AudioRtpFactoryException occured";
-      }
+    class UnsupportedRtpSessionType : public std::logic_error {
+        public:
+        UnsupportedRtpSessionType(const std::string& msg = "") : std::logic_error(msg) {}
+    };
+    
+    class AudioRtpFactoryException : public std::logic_error {
+        public:
+        AudioRtpFactoryException(const std::string& msg = "") : std::logic_error(msg) {}
     };
 
     class AudioRtpFactory {
diff --git a/sflphone-common/src/audio/audiortp/ZrtpSessionCallback.cpp b/sflphone-common/src/audio/audiortp/ZrtpSessionCallback.cpp
index 7e6cc999a9..6eb7779249 100644
--- a/sflphone-common/src/audio/audiortp/ZrtpSessionCallback.cpp
+++ b/sflphone-common/src/audio/audiortp/ZrtpSessionCallback.cpp
@@ -143,29 +143,30 @@ namespace sfl {
         if (sev == Info) {
             msg = _infoMap[subCode];
             if (msg != NULL) {
-                _debug("ZRTP Debug: %s\n", msg->c_str());
+                _debug("ZRTP Debug:\n", msg->c_str());
             }
         }
         if (sev == Warning) {
             msg = _warningMap[subCode];
             if (msg != NULL) {
-                _debug("ZRTP Debug: %s\n", msg->c_str());
+                _debug("ZRTP Debug:\n", msg->c_str());
             }
         }
         if (sev == Severe) {
             msg = _severeMap[subCode];
             if (msg != NULL) {
-                _debug("ZRTP Debug: %s\n", msg->c_str());
+                _debug("ZRTP Debug:\n", msg->c_str());
             }
         }
         if (sev == ZrtpError) {
             if (subCode < 0) {  // received an error packet from peer
                 subCode *= -1;
-                _debug("Received an error packet from peer: %s\n", msg->c_str());
+                _debug("Received an error packet from peer:\n");
             }
             else {
-                _debug("Sent error packet to peer: %s\n", msg->c_str());
+                _debug("Sent error packet to peer:\n");
             }
+            
             msg = _zrtpMap[subCode];
             if (msg != NULL) {
                 _debug("ZRTP Debug: %s\n", msg->c_str());
@@ -180,10 +181,10 @@ namespace sfl {
         if (severity == ZrtpError) {
             if (subCode < 0) {  // received an error packet from peer
                 subCode *= -1;
-                _debug("Received error packet: ");
+                _debug("Received error packet: \n");
             }
             else {
-                _debug("Sent error packet: ");
+                _debug("Sent error packet: \n");
             }
             msg = _zrtpMap[subCode];
             if (msg != NULL) {
-- 
GitLab