Re: [PATCH V3 1/3] core/device: Add function to return child node using name at substring "@"
From: Mahesh J Salgaonkar <mahesh@linux.ibm.com>
Date: 2023-02-23 08:20:21
On 2023-02-16 13:38:57 Thu, Athira Rajeev wrote:
Add a function dt_find_by_name_substr() that returns the child node if it matches till first occurence at "@" of a given name, otherwise NULL. This is helpful for cases with node name like: "name@addr". In scenarios where nodes are added with "name@addr" format and if the value of "addr" is not known, that node can't be matched with node name or addr. Hence matching with substring as node name will return the expected result. Patch adds dt_find_by_name_substr() function and testcase for the same in core/test/run-device.c Signed-off-by: Athira Rajeev <redacted> ---
[...]
quoted hunk ↗ jump to hunk
diff --git a/core/device.c b/core/device.c index 2de37c74..26562235 100644 --- a/core/device.c +++ b/core/device.c@@ -395,6 +395,41 @@ struct dt_node *dt_find_by_name(struct dt_node *root, const char *name) } +struct dt_node *dt_find_by_name_substr(struct dt_node *root, const char *name) +{ + struct dt_node *child, *match; + char *node, *child_node = NULL; +
+ node = malloc(strlen(name) + 1); + if (!node) + return NULL; + memcpy(node, name, strlen(name)); + node[strlen(name)] = '\0';
You may want to use strdup() which does exactly what you trying to do: node = strdup(name); if (!node) return NULL;
+ node = strtok(node, "@");
+ list_for_each(&root->children, child, list) {+ child_node = malloc(strlen(child->name) + 1); + if (!child_node) + goto err; + memcpy(child_node, child->name, strlen(child->name)); + child_node[strlen(child->name)] = '\0';
same here ^^^ Rest looks fine to me. Reviewed-by: Mahesh Salgaonkar <mahesh@linux.ibm.com> Thanks, -Mahesh.