[PATCH 2/3] commands: of_diff: simplify error handling

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Jun 8 06:40:42 PDT 2023


The error check and message is duplicated for each argument and
could be unified if we move it to get_tree instead.

While at it, we limit argc to exactly 3 in case we want to add options
in the future.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/of_diff.c | 55 ++++++++++++++++------------------------------
 drivers/of/base.c  |  3 +++
 2 files changed, 22 insertions(+), 36 deletions(-)

diff --git a/commands/of_diff.c b/commands/of_diff.c
index 19a4a26d2012..ff3d46c7f530 100644
--- a/commands/of_diff.c
+++ b/commands/of_diff.c
@@ -17,60 +17,43 @@ static struct device_node *get_tree(const char *filename, struct device_node *ro
 {
 	struct device_node *node;
 
+	if (!root)
+		root = ERR_PTR(-ENOENT);
+
 	if (!strcmp(filename, "-")) {
-		node = of_get_root_node();
-		if (!node)
-			return ERR_PTR(-ENOENT);
-
-		return of_dup(node);
-	}
-
-	if (!strcmp(filename, "+")) {
-		node = of_get_root_node();
-		if (!node)
-			return ERR_PTR(-ENOENT);
-
 		node = of_dup(root);
-
-		of_fix_tree(node);
-
-		return node;
+	} else if (!strcmp(filename, "+")) {
+		node = of_dup(root);
+		if (!IS_ERR(node))
+			of_fix_tree(node);
+	} else {
+		node = of_read_file(filename);
 	}
 
-	return of_read_file(filename);
+	if (IS_ERR(node))
+		printf("Cannot read %s: %pe\n", filename, node);
+
+	return node;
 }
 
 static int do_of_diff(int argc, char *argv[])
 {
-	int ret = 0;
+	int ret = COMMAND_ERROR;
 	struct device_node *a, *b, *root;
 
-	if (argc < 3)
+	if (argc != 3)
 		return COMMAND_ERROR_USAGE;
 
 	root = of_get_root_node();
 	a = get_tree(argv[1], root);
 	b = get_tree(argv[2], root);
 
-	if (IS_ERR(a)) {
-		printf("Cannot read %s: %pe\n", argv[1], a);
-		ret = COMMAND_ERROR;
-		a = NULL;
-		goto out;
-	}
+	if (!IS_ERR(a) && !IS_ERR(b))
+		ret = of_diff(a, b, 0) ? COMMAND_ERROR : COMMAND_SUCCESS;
 
-	if (IS_ERR(b)) {
-		printf("Cannot read %s: %pe\n", argv[2], b);
-		ret = COMMAND_ERROR;
-		b = NULL;
-		goto out;
-	}
-
-	ret = of_diff(a, b, 0) ? COMMAND_ERROR : COMMAND_SUCCESS;
-out:
-	if (a && a != root)
+	if (!IS_ERR(a) && a != root)
 		of_delete_node(a);
-	if (b && b != root)
+	if (!IS_ERR(b) && b != root)
 		of_delete_node(b);
 
 	return ret;
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 5da188115547..e3416b5b75a7 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -2660,6 +2660,9 @@ struct device_node *of_copy_node(struct device_node *parent, const struct device
 
 struct device_node *of_dup(const struct device_node *root)
 {
+	if (IS_ERR_OR_NULL(root))
+		return ERR_CAST(root);
+
 	return of_copy_node(NULL, root);
 }
 
-- 
2.39.2




More information about the barebox mailing list