From ed443d08cff72094ab65a5cbebbd7a12fc9c7d9a Mon Sep 17 00:00:00 2001
From: Mark Spencer <markster@digium.com>
Date: Sun, 16 May 2004 16:31:42 +0000
Subject: [PATCH] Timely packet removal and make valgrind happy

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@2980 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 channels/chan_sip.c | 41 ++++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 13 deletions(-)

diff --git a/channels/chan_sip.c b/channels/chan_sip.c
index 6227116796..a9f5205120 100755
--- a/channels/chan_sip.c
+++ b/channels/chan_sip.c
@@ -577,7 +577,7 @@ static int append_history(struct sip_pvt *p, char *event, char *data)
 /*--- retrans_pkt: Retransmit SIP message if no answer ---*/
 static int retrans_pkt(void *data)
 {
-	struct sip_pkt *pkt=data;
+	struct sip_pkt *pkt=data, *prev, *cur;
 	int res = 0;
 	ast_mutex_lock(&pkt->owner->lock);
 	if (pkt->retrans < MAX_RETRANS) {
@@ -602,17 +602,32 @@ static int retrans_pkt(void *data)
 				ast_mutex_lock(&pkt->owner->lock);
 			}
 			if (pkt->owner->owner) {
-				/* XXX Potential deadlocK?? XXX */
 				ast_queue_hangup(pkt->owner->owner);
 				ast_mutex_unlock(&pkt->owner->owner->lock);
 			} else {
 				/* If no owner, destroy now */
 				pkt->owner->needdestroy = 1;
 			}
-		} else {
-			/* Okay, it's not fatal, just continue.  XXX If we were nice, we'd free it now, rather than wait for the
-			   end of the call XXX */
 		}
+		/* In any case, go ahead and remove the packet */
+		prev = NULL;
+		cur = pkt->owner->packets;
+		while(cur) {
+			if (cur == pkt)
+				break;
+			prev = cur;
+			cur = cur->next;
+		}
+		if (cur) {
+			if (prev)
+				prev->next = cur->next;
+			else
+				pkt->owner->packets = cur->next;
+			ast_mutex_unlock(&pkt->owner->lock);
+			free(cur);
+			pkt = NULL;
+		} else
+			ast_log(LOG_WARNING, "Weird, couldn't find packet owner!\n");
 	}
 	if (pkt)
 		ast_mutex_unlock(&pkt->owner->lock);
@@ -1503,7 +1518,7 @@ static int sip_hangup(struct ast_channel *ast)
 			if (p->outgoing) {
 				transmit_request_with_auth(p, "CANCEL", p->ocseq, 1, 0);
 				/* Actually don't destroy us yet, wait for the 487 on our original 
-				   INVITE, but do set an autodestruct just in case. */
+				   INVITE, but do set an autodestruct just in case we never get it. */
 				needdestroy = 0;
 				sip_scheddestroy(p, 15000);
 				if ( p->initid != -1 ) {
@@ -2976,13 +2991,13 @@ static int add_sdp(struct sip_request *resp, struct sip_pvt *p, struct ast_rtp *
 	struct sockaddr_in sin;
 	struct sockaddr_in vsin;
 	struct sip_codec_pref *cur;
-	char v[256];
-	char s[256];
-	char o[256];
-	char c[256];
-	char t[256];
-	char m[256];
-	char m2[256];
+	char v[256] = "";
+	char s[256] = "";
+	char o[256] = "";
+	char c[256] = "";
+	char t[256] = "";
+	char m[256] = "";
+	char m2[256] = "";
 	char a[1024] = "";
 	char a2[1024] = "";
 	int x;
-- 
GitLab