mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Johannes Zink <j.zink@pengutronix.de>
To: barebox@lists.infradead.org
Subject: Re: [PATCH 2/3] commands: of_diff: simplify error handling
Date: Tue, 13 Jun 2023 13:48:42 +0200	[thread overview]
Message-ID: <19bece81-134f-a9bf-8305-7a5025999574@pengutronix.de> (raw)
In-Reply-To: <20230608134040.2123869-3-a.fatoum@pengutronix.de>

FTR - though I'm too late to the party:

This also fixes a null pointer exception when running of_diff with invalid 
arguments: 
https://lore.barebox.org/barebox/044fee32-5560-2e85-a0d3-cc7cea71ed48@pengutronix.de/

Tested-by: Johannes Zink <j.zink@pengutronix.de>

On 6/8/23 15:40, Ahmad Fatoum wrote:
> 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@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);
>   }
>   

-- 
Pengutronix e.K.                | Johannes Zink                  |
Steuerwalder Str. 21            | https://www.pengutronix.de/    |
31137 Hildesheim, Germany       | Phone: +49-5121-206917-0       |
Amtsgericht Hildesheim, HRA 2686| Fax:   +49-5121-206917-5555    |




  reply	other threads:[~2023-06-13 11:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08 13:40 [PATCH 0/3] commands: of_diff: support applying fixups on arbitrary device trees Ahmad Fatoum
2023-06-08 13:40 ` [PATCH 1/3] of: change superfluous of_fix_tree int return type to void Ahmad Fatoum
2023-06-08 13:40 ` [PATCH 2/3] commands: of_diff: simplify error handling Ahmad Fatoum
2023-06-13 11:48   ` Johannes Zink [this message]
2023-06-08 13:40 ` [PATCH 3/3] commands: of_diff: support applying fixups on arbitrary device trees Ahmad Fatoum
2023-06-09  6:41 ` [PATCH 0/3] " Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19bece81-134f-a9bf-8305-7a5025999574@pengutronix.de \
    --to=j.zink@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox