[asterisk-commits] eliel: branch group/data_api_gsoc2009 r209710 - /team/group/data_api_gsoc2009...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jul 31 16:49:04 CDT 2009


Author: eliel
Date: Fri Jul 31 16:48:53 2009
New Revision: 209710

URL: http://svn.asterisk.org/svn-view/asterisk?view=rev&rev=209710
Log:
Fix a leak in the data_search mechanism.
(there is still a leak when trying to destroy the returned tree).


Modified:
    team/group/data_api_gsoc2009/main/data.c

Modified: team/group/data_api_gsoc2009/main/data.c
URL: http://svn.asterisk.org/svn-view/asterisk/team/group/data_api_gsoc2009/main/data.c?view=diff&rev=209710&r1=209709&r2=209710
==============================================================================
--- team/group/data_api_gsoc2009/main/data.c (original)
+++ team/group/data_api_gsoc2009/main/data.c Fri Jul 31 16:48:53 2009
@@ -804,31 +804,23 @@
 	const char *path)
 {
 	char *rpath, *node_name;
-	struct ast_data_search *child, *ret = NULL;
+	struct ast_data_search *child = NULL;
+	struct ao2_container *current = parent;
 
 	rpath = ast_strdupa(path);
 
 	node_name = next_node_name(&rpath);
-	if (!node_name) {
-		/* no more nodes to create. */
-		return NULL;
-	}
-
-	child = data_search_find(parent, node_name);
-
-	if (!child) {
-		/* nodes without handler are non-terminal nodes. */
-		child = data_search_add_child(parent, node_name);
-	}
-
-	if (rpath) {
-		ret = data_search_create(child->children, rpath);
-		if (ret) {
-			ao2_ref(child, -1);
-		}
-	}
-
-	return ret ? ret : child;
+	while (node_name) {
+		child = data_search_find(current, node_name);
+		if (!child) {
+			child = data_search_add_child(current, node_name);
+		}
+		ao2_ref(child, -1);
+		current = child->children;
+		node_name = next_node_name(&rpath);
+	}
+
+	return child;
 }
 
 /*!
@@ -923,8 +915,6 @@
 			child->cmp_type = cmp_type;
 			child->value = ast_strdup(ast_str_buffer(value));
 		}
-
-		ao2_ref(child, -1);
 	}
 
 	ast_free(name);
@@ -1004,25 +994,29 @@
 static struct ast_data_search *data_search_get_node(const struct ast_data_search *node,
 	const char *path)
 {
-	char *savepath;
-	struct ast_data_search *child;
+	char *savepath, *node_name;
+	struct ast_data_search *child, *current = (struct ast_data_search *) node;
 
 	if (!node) {
 		return NULL;
 	}
 
-	if (!path) {
-		return (struct ast_data_search *) node;
-	}
-
 	savepath = ast_strdupa(path);
-
-	child = data_search_find(node->children, next_node_name(&savepath));
-	if (!child) {
-		return NULL;
-	}
-
-	return data_search_get_node(child, savepath);
+	node_name = next_node_name(&savepath);
+
+	while (node_name) {
+		child = data_search_find(current->children, node_name);
+		if (current != node) {
+			ao2_ref(current, -1);
+		}
+		if (!child) {
+			return NULL;
+		};
+		current = child;
+		node_name = next_node_name(&savepath);
+	}
+
+	return current;
 }
 
 int ast_data_search_cmp_string(const struct ast_data_search *root, const char *name,
@@ -1355,11 +1349,6 @@
 	/* release the temporary created node used for searching. */
 	ao2_ref(find_node, -1);
 
-	/* do not increment the reference counter, we are not using it. */
-	if (found) {
-		ao2_ref(found, -1);
-	}
-
 	return found;
 }
 
@@ -1374,21 +1363,28 @@
 static struct ast_data *data_result_get_node(struct ast_data *node,
 	const char *path)
 {
-	char *savepath;
-	struct ast_data *child;
-
-	if (!path) {
-		return node;
-	}
+	char *savepath, *node_name;
+	struct ast_data *child, *current = node;
 
 	savepath = ast_strdupa(path);
-
-	child = data_result_find_child(node, next_node_name(&savepath));
-	if (!child) {
-		return NULL;
-	}
-
-	return data_result_get_node(child, savepath);
+	node_name = next_node_name(&savepath);
+
+	while (node_name) {
+		child = data_result_find_child(current, node_name);
+		if (current != node) {
+			ao2_ref(current, -1);
+		}
+		if (!child) {
+			return NULL;
+		}
+		current = child;
+		node_name = next_node_name(&savepath);
+	}
+
+	/* do not increment the refcount of the returned object. */
+	ao2_ref(current, -1);
+
+	return current;
 }
 
 /*!
@@ -2070,21 +2066,23 @@
 	}
 
 	while ((res = ao2_iterator_next(&iterator->internal_iterator))) {
+		/* if there is no node name pattern specified, return
+		 * the next node. */
 		if (!iterator->pattern) {
-			/* if there is no node name pattern specified, return
-			 * the next node. */
-			break;
-		}
-
+			break;
+		}
+
+		/* if the pattern is a regular expression, check if this node
+		 * matches. */
 		if (iterator->is_pattern && !regexec(&(iterator->regex_pattern),
 			res->name, 0, NULL, 0)) {
 			break;
 		}
 
+		/* if there is a pattern specified, check if this node matches
+		 * the wanted node names. */
 		if (!iterator->is_pattern &&
 			(iterator->pattern && !strcasecmp(res->name, iterator->pattern))) {
-			/* if there is a pattern specified, check if this node matches
-			 * the wanted node names. */
 			break;
 		}
 




More information about the asterisk-commits mailing list