[asterisk-commits] rmudgett: branch 10 r352956 - in /branches/10: ./ apps/ channels/ include/ast...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jan 27 12:36:49 CST 2012


Author: rmudgett
Date: Fri Jan 27 12:36:43 2012
New Revision: 352956

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=352956
Log:
Audit of ao2_iterator_init() usage for v1.8.

Fixes numerous reference leaks and missing ao2_iterator_destroy() calls as
a result.

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

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

Modified:
    branches/10/   (props changed)
    branches/10/apps/app_chanspy.c
    branches/10/apps/app_queue.c
    branches/10/channels/chan_iax2.c
    branches/10/channels/chan_sip.c
    branches/10/include/asterisk/indications.h
    branches/10/main/indications.c
    branches/10/main/pbx.c
    branches/10/main/taskprocessor.c
    branches/10/res/res_odbc.c
    branches/10/res/res_srtp.c
    branches/10/res/snmp/agent.c

Propchange: branches/10/
------------------------------------------------------------------------------
Binary property 'branch-1.8-merged' - no diff available.

Modified: branches/10/apps/app_chanspy.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/apps/app_chanspy.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/apps/app_chanspy.c (original)
+++ branches/10/apps/app_chanspy.c Fri Jan 27 12:36:43 2012
@@ -828,11 +828,13 @@
 		}
 
 		if (!iter) {
-			return -1;
+			res = -1;
+			goto exit;
 		}
 
 		res = ast_waitfordigit(chan, waitms);
 		if (res < 0) {
+			iter = ast_channel_iterator_destroy(iter);
 			ast_clear_flag(chan, AST_FLAG_SPYING);
 			break;
 		}
@@ -840,10 +842,12 @@
 			char tmp[2];
 			tmp[0] = res;
 			tmp[1] = '\0';
-			if (!ast_goto_if_exists(chan, exitcontext, tmp, 1))
+			if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) {
+				iter = ast_channel_iterator_destroy(iter);
 				goto exit;
-			else
+			} else {
 				ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext);
+			}
 		}
 
 		/* reset for the next loop around, unless overridden later */
@@ -982,10 +986,12 @@
 
 			if (res == -1) {
 				ast_autochan_destroy(autochan);
+				iter = ast_channel_iterator_destroy(iter);
 				goto exit;
 			} else if (res == -2) {
 				res = 0;
 				ast_autochan_destroy(autochan);
+				iter = ast_channel_iterator_destroy(iter);
 				goto exit;
 			} else if (res > 1 && spec) {
 				struct ast_channel *next;
@@ -1005,6 +1011,7 @@
 					}
 				}
 			} else if (res == 0 && ast_test_flag(flags, OPTION_EXITONHANGUP)) {
+				iter = ast_channel_iterator_destroy(iter);
 				goto exit;
 			}
 		}

Modified: branches/10/apps/app_queue.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/apps/app_queue.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/apps/app_queue.c (original)
+++ branches/10/apps/app_queue.c Fri Jan 27 12:36:43 2012
@@ -1419,9 +1419,9 @@
 				ast_debug(4, "%s is unavailable because it has only been %d seconds since his last call (wrapup time is %d)\n", member->membername, (int) (time(NULL) - member->lastcall), q->wrapuptime);
 				break;
 			} else {
-				ao2_unlock(q);
 				ao2_ref(member, -1);
 				ao2_iterator_destroy(&mem_iter);
+				ao2_unlock(q);
 				ast_debug(4, "%s is available.\n", member->membername);
 				return 0;
 			}
@@ -4520,24 +4520,24 @@
 		AST_LIST_HEAD(, ast_dialed_interface) *dialed_interfaces;
 		if (!tmp) {
 			ao2_ref(cur, -1);
+			ao2_iterator_destroy(&memi);
 			ao2_unlock(qe->parent);
-			ao2_iterator_destroy(&memi);
 			goto out;
 		}
 		if (!datastore) {
 			if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
+				callattempt_free(tmp);
 				ao2_ref(cur, -1);
+				ao2_iterator_destroy(&memi);
 				ao2_unlock(qe->parent);
-				ao2_iterator_destroy(&memi);
-				callattempt_free(tmp);
 				goto out;
 			}
 			datastore->inheritance = DATASTORE_INHERIT_FOREVER;
 			if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
+				callattempt_free(tmp);
 				ao2_ref(cur, -1);
+				ao2_iterator_destroy(&memi);
 				ao2_unlock(&qe->parent);
-				ao2_iterator_destroy(&memi);
-				callattempt_free(tmp);
 				goto out;
 			}
 			datastore->data = dialed_interfaces;
@@ -4561,6 +4561,7 @@
 
 		if (di) {
 			callattempt_free(tmp);
+			ao2_ref(cur, -1);
 			continue;
 		}
 
@@ -4570,10 +4571,10 @@
 		 * it won't call one that has already been called. */
 		if (strncasecmp(cur->interface, "Local/", 6)) {
 			if (!(di = ast_calloc(1, sizeof(*di) + strlen(cur->interface)))) {
+				callattempt_free(tmp);
 				ao2_ref(cur, -1);
+				ao2_iterator_destroy(&memi);
 				ao2_unlock(qe->parent);
-				ao2_iterator_destroy(&memi);
-				callattempt_free(tmp);
 				goto out;
 			}
 			strcpy(di->interface, cur->interface);
@@ -6559,9 +6560,10 @@
 
 	if ((q = ao2_t_find(queues, &tmpq, OBJ_POINTER, "Find for QUEUE_MEMBER_LIST()"))) {
 		int buflen = 0, count = 0;
-		struct ao2_iterator mem_iter = ao2_iterator_init(q->members, 0);
+		struct ao2_iterator mem_iter;
 
 		ao2_lock(q);
+		mem_iter = ao2_iterator_init(q->members, 0);
 		while ((m = ao2_iterator_next(&mem_iter))) {
 			/* strcat() is always faster than printf() */
 			if (count++) {
@@ -7080,7 +7082,9 @@
 static int clear_stats(const char *queuename)
 {
 	struct call_queue *q;
-	struct ao2_iterator queue_iter = ao2_iterator_init(queues, 0);
+	struct ao2_iterator queue_iter;
+
+	queue_iter = ao2_iterator_init(queues, 0);
 	while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
 		ao2_lock(q);
 		if (ast_strlen_zero(queuename) || !strcasecmp(q->name, queuename))
@@ -7168,8 +7172,8 @@
 		}
 	}
 
+	ao2_lock(queues);
 	queue_iter = ao2_iterator_init(queues, AO2_ITERATOR_DONTLOCK);
-	ao2_lock(queues);
 	while ((q = ao2_t_iterator_next(&queue_iter, "Iterate through queues"))) {
 		float sl;
 		struct call_queue *realtime_queue = NULL;
@@ -7869,11 +7873,11 @@
 		while ((m = ao2_iterator_next(&mem_iter))) {
 			if (!strncasecmp(word, m->membername, wordlen) && ++which > state) {
 				char *tmp;
-				ao2_unlock(q);
 				tmp = ast_strdup(m->interface);
 				ao2_ref(m, -1);
+				ao2_iterator_destroy(&mem_iter);
+				ao2_unlock(q);
 				queue_t_unref(q, "Done with iterator, returning interface name");
-				ao2_iterator_destroy(&mem_iter);
 				ao2_iterator_destroy(&queue_iter);
 				return tmp;
 			}
@@ -8415,6 +8419,7 @@
 
 		ao2_ref(member, -1);
 	}
+	ao2_iterator_destroy(&im);
 
 	/* include the callers inside the result. */
 	if (queue->head) {

Modified: branches/10/channels/chan_iax2.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/channels/chan_iax2.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/channels/chan_iax2.c (original)
+++ branches/10/channels/chan_iax2.c Fri Jan 27 12:36:43 2012
@@ -2640,6 +2640,7 @@
 			sin.sin_addr.s_addr = peercnt->addr;
 			if (a->argc == 5 && (!strcasecmp(a->argv[4], ast_inet_ntoa(sin.sin_addr)))) {
 					ast_cli(a->fd, "%-15s %-12d %-12d\n", ast_inet_ntoa(sin.sin_addr), peercnt->cur, peercnt->limit);
+					ao2_ref(peercnt, -1);
 					found = 1;
 					break;
 			} else {
@@ -6684,8 +6685,7 @@
 
 	ast_cli(a->fd, FORMAT, "Username", "Secret", "Authen", "Def.Context", "A/C","Codec Pref");
 	i = ao2_iterator_init(users, 0);
-	for (user = ao2_iterator_next(&i); user; 
-		user_unref(user), user = ao2_iterator_next(&i)) {
+	for (; (user = ao2_iterator_next(&i)); user_unref(user)) {
 		if (havepattern && regexec(&regexbuf, user->name, 0, NULL, 0))
 			continue;
 
@@ -6773,8 +6773,7 @@
 		ast_cli(fd, FORMAT2, "Name/Username", "Host", "   ", "Mask", "Port", "   ", "Status", "Description");
 
 	i = ao2_iterator_init(peers, 0);
-	for (peer = ao2_iterator_next(&i); peer;
-		peer_unref(peer), peer = ao2_iterator_next(&i)) {
+	for (; (peer = ao2_iterator_next(&i)); peer_unref(peer)) {
 		char nm[20];
 		char status[20];
 		int retstatus;
@@ -7100,7 +7099,7 @@
 
 
 	i = ao2_iterator_init(peers, 0);
-	for (peer = ao2_iterator_next(&i); peer; peer_unref(peer), peer = ao2_iterator_next(&i)) {
+	for (; (peer = ao2_iterator_next(&i)); peer_unref(peer)) {
 		encmethods_to_str(peer->encmethods, encmethods);
 		astman_append(s, "Event: PeerEntry\r\n%sChanneltype: IAX\r\n", idtext);
 		if (!ast_strlen_zero(peer->username)) {
@@ -7683,8 +7682,8 @@
 		return res;
 	}
 	/* Search the userlist for a compatible entry, and fill in the rest */
+	ast_sockaddr_from_sin(&addr, sin);
 	i = ao2_iterator_init(users, 0);
-	ast_sockaddr_from_sin(&addr, sin);
 	while ((user = ao2_iterator_next(&i))) {
 		if ((ast_strlen_zero(iaxs[callno]->username) ||				/* No username specified */
 			!strcmp(iaxs[callno]->username, user->name))	/* Or this username specified */
@@ -14698,10 +14697,9 @@
 	char *pstr = "";
 
 	i = ao2_iterator_init(users, 0);
-	while ((user = ao2_iterator_next(&i))) {
+	for (; (user = ao2_iterator_next(&i)); user_unref(user)) {
 		data_user = ast_data_add_node(data_root, "user");
 		if (!data_user) {
-			user_unref(user);
 			continue;
 		}
 
@@ -14749,8 +14747,6 @@
 			pstr = ast_test_flag64(user, IAX_CODEC_USER_FIRST) ? "caller" : "host";
 		}
 		ast_data_add_str(data_user, "codec-preferences", pstr);
-
-		user_unref(user);
 
 		if (!ast_data_search_match(search, data_user)) {
 			ast_data_remove_node(data_root, data_user);

Modified: branches/10/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/channels/chan_sip.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/channels/chan_sip.c (original)
+++ branches/10/channels/chan_sip.c Fri Jan 27 12:36:43 2012
@@ -17598,8 +17598,8 @@
 			while ((pi = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
 				ao2_lock(pi);
 				if (name && regexec(&regexbuf, pi->name, 0, NULL, 0)) {
+					ao2_unlock(pi);
 					sip_unref_peer(pi, "toss iterator peer ptr before continue");
-					ao2_unlock(pi);
 					continue;
 				};
 				if (ast_test_flag(&pi->flags[1], SIP_PAGE2_RTCACHEFRIENDS)) {
@@ -30951,6 +30951,8 @@
 		/* transfer mode */
 		enum_node = ast_data_add_node(data_peer, "allowtransfer");
 		if (!enum_node) {
+			ao2_unlock(peer);
+			ao2_ref(peer, -1);
 			continue;
 		}
 		ast_data_add_str(enum_node, "text", transfermode2str(peer->allowtransfer));
@@ -30990,6 +30992,8 @@
 		/* amaflags */
 		enum_node = ast_data_add_node(data_peer, "amaflags");
 		if (!enum_node) {
+			ao2_unlock(peer);
+			ao2_ref(peer, -1);
 			continue;
 		}
 		ast_data_add_int(enum_node, "value", peer->amaflags);
@@ -30998,6 +31002,8 @@
 		/* sip options */
 		data_sip_options = ast_data_add_node(data_peer, "sipoptions");
 		if (!data_sip_options) {
+			ao2_unlock(peer);
+			ao2_ref(peer, -1);
 			continue;
 		}
 		for (x = 0 ; x < ARRAY_LEN(sip_options); x++) {
@@ -31007,6 +31013,8 @@
 		/* callingpres */
 		enum_node = ast_data_add_node(data_peer, "callingpres");
 		if (!enum_node) {
+			ao2_unlock(peer);
+			ao2_ref(peer, -1);
 			continue;
 		}
 		ast_data_add_int(enum_node, "value", peer->callingpres);

Modified: branches/10/include/asterisk/indications.h
URL: http://svnview.digium.com/svn/asterisk/branches/10/include/asterisk/indications.h?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/include/asterisk/indications.h (original)
+++ branches/10/include/asterisk/indications.h Fri Jan 27 12:36:43 2012
@@ -174,7 +174,8 @@
 /*!
  * \brief Get an iterator for the available tone zones
  *
- * Use ao2_iterator_next() to iterate the tone zones.
+ * \note Use ao2_iterator_next() to iterate the tone zones.
+ * \note Use ao2_iterator_destroy() to clean up.
  *
  * \return an initialized iterator
  */

Modified: branches/10/main/indications.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/main/indications.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/main/indications.c (original)
+++ branches/10/main/indications.c Fri Jan 27 12:36:43 2012
@@ -658,6 +658,7 @@
 			break;
 		}
 	}
+	ao2_iterator_destroy(&i);
 
 	return res;
 }
@@ -835,6 +836,7 @@
 			ast_tone_zone_unlock(tz);
 			tz = ast_tone_zone_unref(tz);
 		}
+		ao2_iterator_destroy(&iter);
 		return CLI_SUCCESS;
 	}
 

Modified: branches/10/main/pbx.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/main/pbx.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/main/pbx.c (original)
+++ branches/10/main/pbx.c Fri Jan 27 12:36:43 2012
@@ -7605,6 +7605,7 @@
 			AST_LIST_INSERT_HEAD(&hints_stored, saved_hint, list);
 		}
 	}
+	ao2_iterator_destroy(&i);
 
 	/* save the old table and list */
 	oldtable = contexts_table;

Modified: branches/10/main/taskprocessor.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/main/taskprocessor.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/main/taskprocessor.c (original)
+++ branches/10/main/taskprocessor.c Fri Jan 27 12:36:43 2012
@@ -173,6 +173,7 @@
 		}
 		ao2_ref(p, -1);
 	}
+	ao2_iterator_destroy(&i);
 	return name;
 }
 
@@ -266,6 +267,7 @@
 		ast_cli(a->fd, "\n%24s   %17ld %12ld %12ld", name, processed, qsize, maxqsize);
 		ao2_ref(p, -1);
 	}
+	ao2_iterator_destroy(&i);
 	tcount = ao2_container_count(tps_singletons); 
 	ast_cli(a->fd, "\n\t+---------------------+-----------------+------------+-------------+\n\t%d taskprocessors\n\n", tcount);
 	return CLI_SUCCESS;	

Modified: branches/10/res/res_odbc.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/res/res_odbc.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/res/res_odbc.c (original)
+++ branches/10/res/res_odbc.c Fri Jan 27 12:36:43 2012
@@ -911,7 +911,7 @@
 
 static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-	struct ao2_iterator aoi = ao2_iterator_init(class_container, 0);
+	struct ao2_iterator aoi;
 	struct odbc_class *class;
 	struct odbc_obj *current;
 	int length = 0;
@@ -930,6 +930,7 @@
 		if (a->pos != 2)
 			return NULL;
 		length = strlen(a->word);
+		aoi = ao2_iterator_init(class_container, 0);
 		while ((class = ao2_iterator_next(&aoi))) {
 			if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
 				ret = ast_strdup(class->name);
@@ -1722,12 +1723,14 @@
 
 			ao2_ref(current, -1);
 		}
+		ao2_iterator_destroy(&aoi2);
 		ao2_ref(class, -1);
 
 		if (!ast_data_search_match(search, data_odbc_class)) {
 			ast_data_remove_node(root, data_odbc_class);
 		}
 	}
+	ao2_iterator_destroy(&aoi);
 	return 0;
 }
 

Modified: branches/10/res/res_srtp.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/res/res_srtp.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/res/res_srtp.c (original)
+++ branches/10/res/res_srtp.c Fri Jan 27 12:36:43 2012
@@ -322,7 +322,7 @@
 	int retry = 0;
 	struct ast_rtp_instance_stats stats = {0,};
 
-	tryagain:
+tryagain:
 
 	for (i = 0; i < 2; i++) {
 		res = rtcp ? srtp_unprotect_rtcp(srtp->session, buf, len) : srtp_unprotect(srtp->session, buf, len);
@@ -348,38 +348,42 @@
 		if (srtp->session) {
 			struct ast_srtp_policy *policy;
 			struct ao2_iterator it;
-			int policies_count = 0;
-			
+			int policies_count;
+
 			// dealloc first
 			ast_log(LOG_WARNING, "SRTP destroy before re-create\n");
 			srtp_dealloc(srtp->session);
-			
+
 			// get the count
 			policies_count = ao2_container_count(srtp->policies);
-			
+
 			// get the first to build up
 			it = ao2_iterator_init(srtp->policies, 0);
 			policy = ao2_iterator_next(&it);
 
 			ast_log(LOG_WARNING, "SRTP try to re-create\n");
-        		if (srtp_create(&srtp->session, &policy->sp) == err_status_ok) {
-				ast_log(LOG_WARNING, "SRTP re-created with first policy\n");
-				
-				// unref first element
-				ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
-				
-				// if we have more than one policy, add them afterwards	
-				if (policies_count > 1) {
-					ast_log(LOG_WARNING, "Add all the other %d policies\n", policies_count-1);
-					while ((policy = ao2_iterator_next(&it))) {
-						srtp_add_stream(srtp->session, &policy->sp);
-						ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
+			if (policy) {
+				if (srtp_create(&srtp->session, &policy->sp) == err_status_ok) {
+					ast_log(LOG_WARNING, "SRTP re-created with first policy\n");
+
+					// unref first element
+					ao2_t_ref(policy, -1, "Unreffing first policy for re-creating srtp session");
+
+					// if we have more than one policy, add them afterwards
+					if (policies_count > 1) {
+						ast_log(LOG_WARNING, "Add all the other %d policies\n",
+							policies_count - 1);
+						while ((policy = ao2_iterator_next(&it))) {
+							srtp_add_stream(srtp->session, &policy->sp);
+							ao2_t_ref(policy, -1, "Unreffing n-th policy for re-creating srtp session");
+						}
 					}
+
+					retry++;
+					ao2_iterator_destroy(&it);
+					goto tryagain;
 				}
-				
-				retry++;
-				ao2_iterator_destroy(&it);
-				goto tryagain;
+				ao2_t_ref(policy, -1, "Unreffing first policy after srtp_create failed");
 			}
 			ao2_iterator_destroy(&it);
 		}

Modified: branches/10/res/snmp/agent.c
URL: http://svnview.digium.com/svn/asterisk/branches/10/res/snmp/agent.c?view=diff&rev=352956&r1=352955&r2=352956
==============================================================================
--- branches/10/res/snmp/agent.c (original)
+++ branches/10/res/snmp/agent.c Fri Jan 27 12:36:43 2012
@@ -703,6 +703,7 @@
 			tz = ast_tone_zone_unref(tz);
 			long_ret++;
 		}
+		ao2_iterator_destroy(&i);
 
 		return (u_char *) &long_ret;
 	}
@@ -743,6 +744,7 @@
 		tz = ast_tone_zone_unref(tz);
 		i--;
 	}
+	ao2_iterator_destroy(&iter);
 
 	if (tz == NULL) {
 		return NULL;
@@ -750,24 +752,27 @@
 
 	switch (vp->magic) {
 	case ASTINDINDEX:
+		ast_tone_zone_unref(tz);
 		long_ret = name[*length - 1];
 		return (u_char *)&long_ret;
 	case ASTINDCOUNTRY:
 		ast_copy_string(ret_buf, tz->country, sizeof(ret_buf));
-		tz = ast_tone_zone_unref(tz);
+		ast_tone_zone_unref(tz);
 		*var_len = strlen(ret_buf);
 		return (u_char *) ret_buf;
 	case ASTINDALIAS:
 		/* No longer exists */
+		ast_tone_zone_unref(tz);
 		return NULL;
 	case ASTINDDESCRIPTION:
 		ast_tone_zone_lock(tz);
 		ast_copy_string(ret_buf, tz->description, sizeof(ret_buf));
 		ast_tone_zone_unlock(tz);
-		tz = ast_tone_zone_unref(tz);
+		ast_tone_zone_unref(tz);
 		*var_len = strlen(ret_buf);
 		return (u_char *) ret_buf;
 	default:
+		ast_tone_zone_unref(tz);
 		break;
 	}
 	return NULL;




More information about the asterisk-commits mailing list