Skip to contentSkip to conversation

dbus: Fix subpath comparisons in object tree walk

PATCH In review Andrew Zaborowski 2 participants 4 messages Aug 16, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 dbus: Fix subpath comparisons in object tree walk

By only using strncmp(child->subpath, path, len) we'd only check that child->subpath had path as its prefix in makepath_recurse. As a result, if an object at /ab was added first and at /a second, the second object would not get a new node created and strangely none of our tests or users have caught this.

---
 ell/dbus-service.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
ell/dbus-service.c +4 -2
index 43c7a5c..cf787bc 100644
@@ -649,7 +649,8 @@ static struct object_node *makepath_recurse(struct object_node *node,
 	child = node->children;
 
 	while (child) {
-		if (!strncmp(child->subpath, path, end - path))
+		if (!strncmp(child->subpath, path, end - path) &&
+				child->subpath[end - path] == '\0')
 			goto done;
 
 		child = child->next;
@@ -690,7 +691,8 @@ static struct object_node *lookup_recurse(struct object_node *node,
 	child = node->children;
 
 	while (child) {
-		if (!strncmp(child->subpath, path, end - path))
+		if (!strncmp(child->subpath, path, end - path) &&
+				child->subpath[end - path] == '\0')
 			return lookup_recurse(child->node, end);
 
 		child = child->next;
Signature · 1 line
2.14.1
#1 lore raw

PATCH 2/3 dbus: Split get_managed_objects and expose in dbus-private.h

Split the main logic in get_managed_objects out into _dbus_object_tree_get_objects and expose that in dbus-private.h so it can be used and/or tested in the unit tests.

---
 ell/dbus-private.h |  5 +++++
 ell/dbus-service.c | 21 +++++++++++++++------
 2 files changed, 20 insertions(+), 6 deletions(-)
ell/dbus-private.h +5 -0
index eb8e4ab..799c743 100644
@@ -225,6 +225,11 @@ void _dbus_object_tree_introspect(struct _dbus_object_tree *tree,
 bool _dbus_object_tree_dispatch(struct _dbus_object_tree *tree,
 					struct l_dbus *dbus,
 					struct l_dbus_message *message);
+struct l_dbus_message *_dbus_object_tree_get_objects(
+					struct _dbus_object_tree *tree,
+					struct l_dbus *dbus,
+					const char *path,
+					struct l_dbus_message *message);
 
 bool _dbus_object_tree_property_changed(struct l_dbus *dbus,
 					const char *path,
ell/dbus-service.c +15 -6
index cf787bc..edc46cd 100644
@@ -1990,17 +1990,16 @@ recurse:
 	return true;
 }
 
-static struct l_dbus_message *get_managed_objects(struct l_dbus *dbus,
-						struct l_dbus_message *message,
-						void *user_data)
+struct l_dbus_message *_dbus_object_tree_get_objects(
+						struct _dbus_object_tree *tree,
+						struct l_dbus *dbus,
+						const char *path,
+						struct l_dbus_message *message)
 {
-	struct _dbus_object_tree *tree = _dbus_get_tree(dbus);
 	const struct object_node *node;
 	struct l_dbus_message *reply;
 	struct l_dbus_message_builder *builder;
-	const char *path;
 
-	path = l_dbus_message_get_path(message);
 	node = l_hashmap_lookup(tree->objects, path);
 
 	reply = l_dbus_message_new_method_return(message);
@@ -2027,6 +2026,16 @@ static struct l_dbus_message *get_managed_objects(struct l_dbus *dbus,
 	return reply;
 }
 
+static struct l_dbus_message *get_managed_objects(struct l_dbus *dbus,
+						struct l_dbus_message *message,
+						void *user_data)
+{
+	struct _dbus_object_tree *tree = _dbus_get_tree(dbus);
+	const char *path = l_dbus_message_get_path(message);
+
+	return _dbus_object_tree_get_objects(tree, dbus, path, message);
+}
+
 static void object_manager_setup_func(struct l_dbus_interface *interface)
 {
 	l_dbus_interface_method(interface, "GetManagedObjects", 0,
Signature · 1 line
2.14.1
#2 lore raw

PATCH 3/3 unit: Test dbus object tree with complicated paths

In reply to Andrew Zaborowski

In test-dbus-service build an object tree with all sorts of relations between path elements including new subpath being a prefix of an existing subpath. The new test takes about a minute under valgrind.

---
 unit/test-dbus-service.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
unit/test-dbus-service.c +99 -0
index 269e0e6..358aa94 100644
@@ -299,6 +299,102 @@ static void test_dbus_object_tree_2(const void *test_data)
 	_dbus_object_tree_free(tree);
 }
 
+#define TEST_3_OBJ_COUNT 20000
+static void test_dbus_object_tree_3(const void *test_data)
+{
+	struct _dbus_object_tree *tree;
+	char path[50];
+	struct l_hashmap *paths;
+	unsigned int count = 0;
+	struct l_dbus_message *message, *reply;
+	struct l_dbus_message_iter objects, interfaces;
+	const char *obj_path;
+
+	tree = _dbus_object_tree_new();
+	assert(tree);
+
+	assert(_dbus_object_tree_register_interface(tree, "org.example",
+						setup_dummy_interface,
+						NULL, false));
+
+	srand48(50);
+	paths = l_hashmap_string_new();
+	while (count < TEST_3_OBJ_COUNT) {
+		unsigned int len = 0;
+		unsigned int subpath_len = 0;
+		bool repeat;
+
+		/* Generate a random legal path composed of a, b and / chars */
+		path[len++] = '/';
+
+		while (len < sizeof(path) - 1) {
+			const char *allowed;
+			char ch;
+
+			if (subpath_len)
+				allowed = "aabb/E";
+			else /* At least one letter required after a / */
+				allowed = "ab";
+
+			ch = allowed[lrand48() % strlen(allowed)];
+
+			if (ch == 'E')
+				break;
+			else if (ch == '/')
+				subpath_len = 0;
+			else
+				subpath_len++;
+
+			path[len++] = ch;
+		}
+
+		/* Last character can't be a / */
+		if (path[len - 1] == '/')
+			len--;
+		path[len] = '\0';
+
+		repeat = l_hashmap_lookup(paths, path) != NULL;
+
+		assert(_dbus_object_tree_add_interface(tree, path,
+					"org.example", NULL) == !repeat);
+
+		if (!repeat) {
+			l_hashmap_insert(paths, path, path);
+			count++;
+		}
+	}
+
+	assert(_dbus_object_tree_add_interface(tree, "/",
+					L_DBUS_INTERFACE_OBJECT_MANAGER, NULL));
+	l_hashmap_insert(paths, "/", path);
+
+	message = _dbus_message_new_method_call(1, "org.example", "/",
+						L_DBUS_INTERFACE_OBJECT_MANAGER,
+						"GetManagedObjects");
+	l_dbus_message_set_arguments(message, "");
+	_dbus_message_set_serial(message, 0);
+
+	reply = _dbus_object_tree_get_objects(tree, NULL, "/", message);
+	assert(reply);
+	assert(!l_dbus_message_get_error(reply, NULL, NULL));
+	assert(l_dbus_message_get_arguments(reply, "a{oa{sa{sv}}}", &objects));
+
+	while (l_dbus_message_iter_next_entry(&objects, &obj_path, &interfaces))
+		assert(l_hashmap_remove(paths, obj_path));
+
+	l_dbus_message_unref(message);
+	l_dbus_message_unref(reply);
+
+	assert(l_hashmap_isempty(paths));
+	l_hashmap_destroy(paths, NULL);
+
+	assert(_dbus_object_tree_remove_interface(tree, "/",
+					L_DBUS_INTERFACE_OBJECT_MANAGER));
+
+	assert(_dbus_object_tree_unregister_interface(tree, "org.example"));
+	_dbus_object_tree_free(tree);
+}
+
 static struct l_dbus_message *get_modems_callback(struct l_dbus *dbus,
 					struct l_dbus_message *message,
 					void *user_data)
@@ -452,6 +548,9 @@ int main(int argc, char *argv[])
 	l_test_add("_dbus_object_tree Sanity Tests 2",
 					test_dbus_object_tree_2, NULL);
 
+	l_test_add("_dbus_object_tree Sanity Tests 3",
+					test_dbus_object_tree_3, NULL);
+
 	l_test_add("_dbus_object_tree Introspection",
 					test_dbus_object_tree_introspection,
 					NULL);
Signature · 1 line
2.14.1
#3 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

By only using strncmp(child->subpath, path, len) we'd only check that child->subpath had path as its prefix in makepath_recurse. As a result, if an object at /ab was added first and at /a second, the second object would not get a new node created and strangely none of our tests or users have caught this.

---
  ell/dbus-service.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)

All applied, thanks.

Regards,
-Denis