[asterisk-commits] mmichelson: trunk r371593 - in /trunk: ./ apps/ cdr/ channels/ funcs/ main/ p...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Aug 21 16:01:16 CDT 2012


Author: mmichelson
Date: Tue Aug 21 16:01:11 2012
New Revision: 371593

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=371593
Log:
Fix misuses of asprintf throughout the code.

This fixes three main issues

* Change asprintf() uses to ast_asprintf() so that it
pairs properly with ast_free() and no longer causes
MALLOC_DEBUG to freak out.

* When ast_asprintf() fails, set the pointer NULL if
it will be referenced later.

* Fix some memory leaks that were spotted while taking
care of the first two points.

(Closes issue ASTERISK-20135)
reported by Richard Mudgett

Review: https://reviewboard.asterisk.org/r/2071
........

Merged revisions 371590 from http://svn.asterisk.org/svn/asterisk/branches/1.8
........

Merged revisions 371591 from http://svn.asterisk.org/svn/asterisk/branches/10
........

Merged revisions 371592 from http://svn.asterisk.org/svn/asterisk/branches/11

Modified:
    trunk/   (props changed)
    trunk/apps/app_dial.c
    trunk/apps/app_queue.c
    trunk/apps/app_stack.c
    trunk/cdr/cdr_tds.c
    trunk/channels/chan_dahdi.c
    trunk/channels/chan_oss.c
    trunk/channels/chan_sip.c
    trunk/funcs/func_odbc.c
    trunk/main/file.c
    trunk/main/utils.c
    trunk/main/xmldoc.c
    trunk/pbx/pbx_config.c
    trunk/res/res_config_sqlite.c
    trunk/res/res_jabber.c

Propchange: trunk/
------------------------------------------------------------------------------
--- branch-11-merged (original)
+++ branch-11-merged Tue Aug 21 16:01:11 2012
@@ -1,1 +1,1 @@
-/branches/11:1-371121,371143,371146,371200,371227,371258,371272,371295,371324,371355,371382,371395,371399,371425-371426,371438,371482,371492,371507,371516,371518,371520,371546,371571
+/branches/11:1-371121,371143,371146,371200,371227,371258,371272,371295,371324,371355,371382,371395,371399,371425-371426,371438,371482,371492,371507,371516,371518,371520,371546,371571,371592

Modified: trunk/apps/app_dial.c
URL: http://svnview.digium.com/svn/asterisk/trunk/apps/app_dial.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/apps/app_dial.c (original)
+++ trunk/apps/app_dial.c Tue Aug 21 16:01:11 2012
@@ -2900,8 +2900,7 @@
 					 ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "~~s~~", 1, S_COR(ast_channel_caller(peer)->id.number.valid, ast_channel_caller(peer)->id.number.str, NULL))) {
 					what_is_s = "~~s~~";
 				}
-				if (asprintf(&gosub_args, "%s,%s,1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s, gosub_argstart + 1) < 0) {
-					ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+				if (ast_asprintf(&gosub_args, "%s,%s,1(%s)", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s, gosub_argstart + 1) < 0) {
 					gosub_args = NULL;
 				}
 				*gosub_argstart = ',';
@@ -2911,8 +2910,7 @@
 					 ast_exists_extension(peer, opt_args[OPT_ARG_CALLEE_GOSUB], "~~s~~", 1, S_COR(ast_channel_caller(peer)->id.number.valid, ast_channel_caller(peer)->id.number.str, NULL))) {
 					what_is_s = "~~s~~";
 				}
-				if (asprintf(&gosub_args, "%s,%s,1", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s) < 0) {
-					ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+				if (ast_asprintf(&gosub_args, "%s,%s,1", opt_args[OPT_ARG_CALLEE_GOSUB], what_is_s) < 0) {
 					gosub_args = NULL;
 				}
 			}

Modified: trunk/apps/app_queue.c
URL: http://svnview.digium.com/svn/asterisk/trunk/apps/app_queue.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/apps/app_queue.c (original)
+++ trunk/apps/app_queue.c Tue Aug 21 16:01:11 2012
@@ -5517,8 +5517,7 @@
 					 ast_exists_extension(peer, gosubexec, "~~s~~", 1, S_COR(ast_channel_caller(peer)->id.number.valid, ast_channel_caller(peer)->id.number.str, NULL))) {
 					what_is_s = "~~s~~";
 				}
-				if (asprintf(&gosub_args, "%s,%s,1(%s)", gosubexec, what_is_s, gosub_argstart + 1) < 0) {
-					ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+				if (ast_asprintf(&gosub_args, "%s,%s,1(%s)", gosubexec, what_is_s, gosub_argstart + 1) < 0) {
 					gosub_args = NULL;
 				}
 				*gosub_argstart = ',';
@@ -5528,8 +5527,7 @@
 					 ast_exists_extension(peer, gosubexec, "~~s~~", 1, S_COR(ast_channel_caller(peer)->id.number.valid, ast_channel_caller(peer)->id.number.str, NULL))) {
 					what_is_s = "~~s~~";
 				}
-				if (asprintf(&gosub_args, "%s,%s,1", gosubexec, what_is_s) < 0) {
-					ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+				if (ast_asprintf(&gosub_args, "%s,%s,1", gosubexec, what_is_s) < 0) {
 					gosub_args = NULL;
 				}
 			}

Modified: trunk/apps/app_stack.c
URL: http://svnview.digium.com/svn/asterisk/trunk/apps/app_stack.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/apps/app_stack.c (original)
+++ trunk/apps/app_stack.c Tue Aug 21 16:01:11 2012
@@ -1094,13 +1094,11 @@
 	}
 
 	if (argc == 5) {
-		if (asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority, argv[4]) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+		if (ast_asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority, argv[4]) < 0) {
 			gosub_args = NULL;
 		}
 	} else {
-		if (asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+		if (ast_asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority) < 0) {
 			gosub_args = NULL;
 		}
 	}

Modified: trunk/cdr/cdr_tds.c
URL: http://svnview.digium.com/svn/asterisk/trunk/cdr/cdr_tds.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/cdr/cdr_tds.c (original)
+++ trunk/cdr/cdr_tds.c Tue Aug 21 16:01:11 2012
@@ -360,11 +360,11 @@
 	va_end(ap);
 
 	if (dbfcmd(dbproc, buffer) == FAIL) {
-		free(buffer);
+		ast_free(buffer);
 		return 1;
 	}
 
-	free(buffer);
+	ast_free(buffer);
 
 	if (dbsqlexec(dbproc) == FAIL) {
 		return 1;

Modified: trunk/channels/chan_dahdi.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_dahdi.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/channels/chan_dahdi.c (original)
+++ trunk/channels/chan_dahdi.c Tue Aug 21 16:01:11 2012
@@ -14504,8 +14504,8 @@
 
 	for (which = span = 0; span < NUM_SPANS; span++) {
 		if (pris[span].pri.pri && ++which > state) {
-			if (asprintf(&ret, "%d", span + 1) < 0) {	/* user indexes start from 1 */
-				ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+			if (ast_asprintf(&ret, "%d", span + 1) < 0) {	/* user indexes start from 1 */
+				ret = NULL;
 			}
 			break;
 		}

Modified: trunk/channels/chan_oss.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_oss.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/channels/chan_oss.c (original)
+++ trunk/channels/chan_oss.c Tue Aug 21 16:01:11 2012
@@ -1394,9 +1394,7 @@
 	if (o->mixer_cmd) {
 		char *cmd;
 
-		if (asprintf(&cmd, "mixer %s", o->mixer_cmd) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
-		} else {
+		if (ast_asprintf(&cmd, "mixer %s", o->mixer_cmd) >= 0) {
 			ast_log(LOG_WARNING, "running [%s]\n", cmd);
 			if (system(cmd) < 0) {
 				ast_log(LOG_WARNING, "system() failed: %s\n", strerror(errno));

Modified: trunk/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/trunk/channels/chan_sip.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Tue Aug 21 16:01:11 2012
@@ -30193,9 +30193,7 @@
 	olddirectmediaacl = ast_free_acl_list(olddirectmediaacl);
 	if (!ast_strlen_zero(peer->callback)) { /* build string from peer info */
 		char *reg_string;
-		if (asprintf(&reg_string, "%s?%s:%s@%s/%s", peer->name, peer->username, !ast_strlen_zero(peer->remotesecret) ? peer->remotesecret : peer->secret, peer->tohost, peer->callback) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
-		} else if (reg_string) {
+		if (ast_asprintf(&reg_string, "%s?%s:%s@%s/%s", peer->name, peer->username, !ast_strlen_zero(peer->remotesecret) ? peer->remotesecret : peer->secret, peer->tohost, peer->callback) >= 0) {
 			sip_register(reg_string, 0); /* XXX TODO: count in registry_count */
 			ast_free(reg_string);
 		}

Modified: trunk/funcs/func_odbc.c
URL: http://svnview.digium.com/svn/asterisk/trunk/funcs/func_odbc.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/funcs/func_odbc.c (original)
+++ trunk/funcs/func_odbc.c Tue Aug 21 16:01:11 2012
@@ -953,12 +953,12 @@
 	}
 
 	if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) {
-		if (asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+		if (ast_asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg) < 0) {
+			(*query)->acf->name = NULL;
 		}
 	} else {
-		if (asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+		if (ast_asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg) < 0) {
+			(*query)->acf->name = NULL;
 		}
 	}
 

Modified: trunk/main/file.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/file.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/main/file.c (original)
+++ trunk/main/file.c Tue Aug 21 16:01:11 2012
@@ -261,14 +261,12 @@
 		ext = "WAV";
 
 	if (filename[0] == '/') {
-		if (asprintf(&fn, "%s.%s", filename, ext) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+		if (ast_asprintf(&fn, "%s.%s", filename, ext) < 0) {
 			fn = NULL;
 		}
 	} else {
-		if (asprintf(&fn, "%s/sounds/%s.%s",
+		if (ast_asprintf(&fn, "%s/sounds/%s.%s",
 			     ast_config_AST_DATA_DIR, filename, ext) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
 			fn = NULL;
 		}
 	}
@@ -1070,6 +1068,9 @@
 			format_found = 1;
 
 		fn = build_filename(filename, type);
+		if (!fn) {
+			continue;
+		}
 		errno = 0;
 		bfile = fopen(fn, "r");
 
@@ -1090,6 +1091,7 @@
 		fs->mode = mode;
 		fs->filename = ast_strdup(filename);
 		fs->vfs = NULL;
+		ast_free(fn);
 		break;
 	}
 
@@ -1137,6 +1139,9 @@
 			format_found = 1;
 
 		fn = build_filename(filename, type);
+		if (!fn) {
+			continue;
+		}
 		fd = open(fn, flags | myflags, mode);
 		if (fd > -1) {
 			/* fdopen() the resulting file stream */
@@ -1197,6 +1202,9 @@
 				if (fs) {
 					ast_closestream(fs);
 					fs = NULL;
+				}
+				if (!buf) {
+					ast_free(fn);
 				}
 				continue;
 			}

Modified: trunk/main/utils.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/utils.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/main/utils.c (original)
+++ trunk/main/utils.c Tue Aug 21 16:01:11 2012
@@ -1070,9 +1070,8 @@
 		a->start_routine = start_routine;
 		a->data = data;
 		start_routine = dummy_start;
-		if (asprintf(&a->name, "%-20s started at [%5d] %s %s()",
+		if (ast_asprintf(&a->name, "%-20s started at [%5d] %s %s()",
 			     start_fn, line, file, caller) < 0) {
-			ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
 			a->name = NULL;
 		}
 		data = a;

Modified: trunk/main/xmldoc.c
URL: http://svnview.digium.com/svn/asterisk/trunk/main/xmldoc.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/main/xmldoc.c (original)
+++ trunk/main/xmldoc.c Tue Aug 21 16:01:11 2012
@@ -695,7 +695,9 @@
 
 	if (!rootnode || !ast_xml_node_get_children(rootnode)) {
 		/* If the rootnode field is not found, at least print name. */
-		ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : ""));
+		if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
+			syntax = NULL;
+		}
 		return syntax;
 	}
 
@@ -735,7 +737,9 @@
 
 	if (!hasparams) {
 		/* This application, function, option, etc, doesn't have any params. */
-		ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : ""));
+		if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
+			syntax = NULL;
+		}
 		return syntax;
 	}
 
@@ -807,11 +811,17 @@
 					ast_free(syntax);
 				}
 				/* to give up is ok? */
-				ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : ""));
+				if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
+					syntax = NULL;
+				}
 				return syntax;
 			}
 			paramname = ast_strdup(paramnameattr);
 			ast_xml_free_attr(paramnameattr);
+		}
+
+		if (!paramname) {
+			return NULL;
 		}
 
 		/* Defaults to 'false'. */
@@ -1504,8 +1514,7 @@
 	}
 
 	/* use this spacing (add 4 spaces) inside a variablelist node. */
-	ast_asprintf(&vartabs, "%s    ", tabs);
-	if (!vartabs) {
+	if (ast_asprintf(&vartabs, "%s    ", tabs) < 0) {
 		return ret;
 	}
 	for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
@@ -1641,7 +1650,9 @@
 	int ret = 0;
 	char *optiontabs;
 
-	ast_asprintf(&optiontabs, "%s    ", tabs);
+	if (ast_asprintf(&optiontabs, "%s    ", tabs) < 0) {
+		return ret;
+	}
 
 	for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
 		if (xmldoc_parse_common_elements(node, (ret ? tabs : " - "), "\n", buffer)) {
@@ -1705,8 +1716,7 @@
 	int ret = 0;
 	char *optiontabs;
 
-	ast_asprintf(&optiontabs, "%s    ", tabs);
-	if (!optiontabs) {
+	if (ast_asprintf(&optiontabs, "%s    ", tabs) < 0) {
 		return ret;
 	}
 	for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
@@ -1810,8 +1820,8 @@
 		return;
 	}
 
-	ast_asprintf(&internaltabs, "%s    ", tabs);
-	if (!internaltabs) {
+	if (ast_asprintf(&internaltabs, "%s    ", tabs) < 0) {
+		ast_xml_free_attr(paramname);
 		return;
 	}
 
@@ -2378,8 +2388,10 @@
 	globret = xml_pathmatch(xmlpattern, xmlpattern_maxlen, &globbuf);
 #else
 	/* Get every *-LANG.xml file inside $(ASTDATADIR)/documentation */
-	ast_asprintf(&xmlpattern, "%s/documentation{/thirdparty/,/}*-{%s,%.2s_??,%s}.xml", ast_config_AST_DATA_DIR,
-		documentation_language, documentation_language, default_documentation_language);
+	if (ast_asprintf(&xmlpattern, "%s/documentation{/thirdparty/,/}*-{%s,%.2s_??,%s}.xml", ast_config_AST_DATA_DIR,
+		documentation_language, documentation_language, default_documentation_language) < 0) {
+		return 1;
+	}
 	globret = glob(xmlpattern, MY_GLOB_FLAGS, NULL, &globbuf);
 #endif
 

Modified: trunk/pbx/pbx_config.c
URL: http://svnview.digium.com/svn/asterisk/trunk/pbx/pbx_config.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/pbx/pbx_config.c (original)
+++ trunk/pbx/pbx_config.c Tue Aug 21 16:01:11 2012
@@ -472,14 +472,12 @@
 						if (++which > a->n) {
 							/* If there is an extension then return exten at context. */
 							if (ast_get_extension_matchcid(e) && (!strchr(a->word, '@') || strchr(a->word, '/'))) {
-								if (asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
-									ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+								if (ast_asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
 									ret = NULL;
 								}
 								break;
 							} else if (!ast_get_extension_matchcid(e) && !strchr(a->word, '/')) {
-								if (asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
-									ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+								if (ast_asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
 									ret = NULL;
 								}
 								break;

Modified: trunk/res/res_config_sqlite.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_config_sqlite.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/res/res_config_sqlite.c (original)
+++ trunk/res/res_config_sqlite.c Tue Aug 21 16:01:11 2012
@@ -668,13 +668,13 @@
 	}
 
 	/* Table structure not cached; build the structure now */
-	if (asprintf(&sql, sql_table_structure, tablename) < 0) {
-		ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
+	if (ast_asprintf(&sql, sql_table_structure, tablename) < 0) {
 		sql = NULL;
 	}
 	if (!(tblptr = ast_calloc(1, sizeof(*tblptr) + strlen(tablename) + 1))) {
 		AST_RWLIST_UNLOCK(&sqlite_tables);
 		ast_log(LOG_ERROR, "Memory error.  Cannot cache table '%s'\n", tablename);
+		ast_free(sql);
 		return NULL;
 	}
 	tblptr->name = (char *)tblptr + sizeof(*tblptr);
@@ -690,9 +690,11 @@
 		ast_free(errstr);
 		free_table(tblptr);
 		AST_RWLIST_UNLOCK(&sqlite_tables);
+		ast_free(sql);
 		return NULL;
 	}
 	ast_mutex_unlock(&mutex);
+	ast_free(sql);
 
 	if (AST_LIST_EMPTY(&(tblptr->columns))) {
 		free_table(tblptr);

Modified: trunk/res/res_jabber.c
URL: http://svnview.digium.com/svn/asterisk/trunk/res/res_jabber.c?view=diff&rev=371593&r1=371592&r2=371593
==============================================================================
--- trunk/res/res_jabber.c (original)
+++ trunk/res/res_jabber.c Tue Aug 21 16:01:11 2012
@@ -1760,11 +1760,9 @@
 
 				sprintf(secret, "%s%s", pak->id, client->password);
 				ast_sha1_hash(shasum, secret);
-				handshake = NULL;
-				if (asprintf(&handshake, "<handshake>%s</handshake>", shasum) >= 0) {
+				if (ast_asprintf(&handshake, "<handshake>%s</handshake>", shasum) >= 0) {
 					aji_send_raw(client, handshake);
 					ast_free(handshake);
-					handshake = NULL;
 				}
 				client->state = AJI_CONNECTING;
 				if (aji_recv(client, 1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
@@ -4479,8 +4477,7 @@
 		return 0;
 	}
 	if (!strchr(client->user, '/') && !client->component) { /*client */
-		resource = NULL;
-		if (asprintf(&resource, "%s/asterisk", client->user) >= 0) {
+		if (ast_asprintf(&resource, "%s/asterisk", client->user) >= 0) {
 			client->jid = iks_id_new(client->stack, resource);
 			ast_free(resource);
 		}




More information about the asterisk-commits mailing list