From 534da0ecfd8a9a0d7895f93529635b9fb7adc193 Mon Sep 17 00:00:00 2001
From: Luigi Rizzo <rizzo@icir.org>
Date: Fri, 3 Nov 2006 21:51:16 +0000
Subject: [PATCH] start integrating the simplifications proposed in bug
 0005860, as usual a bit at a time to ease locating new bugs or fixes worth
 merging into other branches.

In this commit, introduce a macro, S_REPLACE, that replaces
a string possibly freeing the previous value.
In one of these places (see the comment marked XXX) the previous
code might leak memory - if so, this ought to be merged in 1.4

The macro might be worth putting in one of the global headers
(e.g. include/asterisk/strings.h) as the construct is used
in a million places in the asterisk code.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47160 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_dial.c | 32 +++++++++++++-------------------
 1 file changed, 13 insertions(+), 19 deletions(-)

diff --git a/apps/app_dial.c b/apps/app_dial.c
index a09d8485bd..ad62372e9f 100644
--- a/apps/app_dial.c
+++ b/apps/app_dial.c
@@ -343,6 +343,13 @@ static void hanguptree(struct dial_localuser *outgoing, struct ast_channel *exce
 	} \
 } while (0)
 
+/* free the buffer if allocated, and set the pointer to the second arg */
+#define S_REPLACE(s, new_val)		\
+	do {				\
+		if (s)			\
+			free(s);	\
+		s = (new_val);		\
+	} while (0)
 
 static int onedigit_goto(struct ast_channel *chan, const char *context, char exten, int pri) 
 {
@@ -537,13 +544,9 @@ static struct ast_channel *wait_for_answer(struct ast_channel *in, struct dial_l
 					}
 
 					if (in->cid.cid_ani) {
-						if (c->cid.cid_ani)
-							free(c->cid.cid_ani);
-						c->cid.cid_ani = ast_strdup(in->cid.cid_ani);
+						S_REPLACE(c->cid.cid_ani, ast_strdup(in->cid.cid_ani));
 					}
-					if (c->cid.cid_rdnis) 
-						free(c->cid.cid_rdnis);
-					c->cid.cid_rdnis = ast_strdup(S_OR(in->macroexten, in->exten));
+					S_REPLACE(c->cid.cid_rdnis, ast_strdup(S_OR(in->macroexten, in->exten)));
 					if (ast_call(c, tmpchan, 0)) {
 						ast_log(LOG_NOTICE, "Failed to dial on local channel for call forward to '%s'\n", tmpchan);
 						ast_clear_flag(o, DIAL_STILLGOING);	
@@ -1148,17 +1151,10 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
 		tmp->chan->data = "(Outgoing Line)";
 		tmp->chan->whentohangup = 0;
 
-		if (tmp->chan->cid.cid_num)
-			free(tmp->chan->cid.cid_num);
-		tmp->chan->cid.cid_num = ast_strdup(chan->cid.cid_num);
-
-		if (tmp->chan->cid.cid_name)
-			free(tmp->chan->cid.cid_name);
-		tmp->chan->cid.cid_name = ast_strdup(chan->cid.cid_name);
-
-		if (tmp->chan->cid.cid_ani)
-			free(tmp->chan->cid.cid_ani);
-		tmp->chan->cid.cid_ani = ast_strdup(chan->cid.cid_ani);
+		S_REPLACE(tmp->chan->cid.cid_num, ast_strdup(chan->cid.cid_num));
+		S_REPLACE(tmp->chan->cid.cid_name, ast_strdup(chan->cid.cid_name));
+		S_REPLACE(tmp->chan->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
+		S_REPLACE(tmp->chan->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
 		
 		/* Copy language from incoming to outgoing */
 		ast_string_field_set(tmp->chan, language, chan->language);
@@ -1166,8 +1162,6 @@ static int dial_exec_full(struct ast_channel *chan, void *data, struct ast_flags
 		tmp->chan->cdrflags = chan->cdrflags;
 		if (ast_strlen_zero(tmp->chan->musicclass))
 			ast_string_field_set(tmp->chan, musicclass, chan->musicclass);
-		/* XXX don't we free previous values ? */
-		tmp->chan->cid.cid_rdnis = ast_strdup(chan->cid.cid_rdnis);
 		/* Pass callingpres setting */
 		tmp->chan->cid.cid_pres = chan->cid.cid_pres;
 		/* Pass type of number */
-- 
GitLab