public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: u-boot@lists.denx.de
Subject: [PATCH 05/17] fs/squashfs: sqfs_split_path: fix memory leak and dangling pointers
Date: Thu, 15 Oct 2020 15:49:10 +0200	[thread overview]
Message-ID: <20201015154910.16911d78@xps13> (raw)
In-Reply-To: <20201014080622.14970-6-richard.genoud@posteo.net>

Hi Richard,

Thank you very much for this entire series, so far I'm fine with all
your changes, but perhaps this patch needs some editions.

Richard Genoud <richard.genoud@posteo.net> wrote on Wed, 14 Oct 2020
10:06:10 +0200:

> *file and *dir were not freed on error
> 
> Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
> ---
>  fs/squashfs/sqfs.c | 39 +++++++++++++++++++++++++++------------
>  1 file changed, 27 insertions(+), 12 deletions(-)
> 
> diff --git a/fs/squashfs/sqfs.c b/fs/squashfs/sqfs.c
> index 0ac922af9e7..55d183663a8 100644
> --- a/fs/squashfs/sqfs.c
> +++ b/fs/squashfs/sqfs.c
> @@ -1089,15 +1089,27 @@ static int sqfs_split_path(char **file, char **dir, const char *path)
>  	char *dirc, *basec, *bname, *dname, *tmp_path;
>  	int ret = 0;
>  
> +	*file = NULL;
> +	*dir = NULL;
> +	dirc = NULL;
> +	basec = NULL;
> +	bname = NULL;
> +	dname = NULL;
> +	tmp_path = NULL;

I personally prefer the use of a different goto statement per error
condition instead of a big kfree(foo); kfree(bar); kfree(barz); even if
setting them to NULL makes it legal.

> +
>  	/* check for first slash in path*/
>  	if (path[0] == '/') {
>  		tmp_path = strdup(path);
> -		if (!tmp_path)
> -			return -ENOMEM;
> +		if (!tmp_path) {
> +			ret = -ENOMEM;
> +			goto out;
> +		}
>  	} else {
>  		tmp_path = malloc(strlen(path) + 2);
> -		if (!tmp_path)
> -			return -ENOMEM;
> +		if (!tmp_path) {
> +			ret = -ENOMEM;
> +			goto out;
> +		}
>  		tmp_path[0] = '/';
>  		strcpy(tmp_path + 1, path);
>  	}
> @@ -1106,13 +1118,13 @@ static int sqfs_split_path(char **file, char **dir, const char *path)
>  	dirc = strdup(tmp_path);
>  	if (!dirc) {
>  		ret = -ENOMEM;
> -		goto free_tmp;
> +		goto out;
>  	}
>  
>  	basec = strdup(tmp_path);
>  	if (!basec) {
>  		ret = -ENOMEM;
> -		goto free_dirc;
> +		goto out;
>  	}
>  
>  	dname = sqfs_dirname(dirc);
> @@ -1122,14 +1134,14 @@ static int sqfs_split_path(char **file, char **dir, const char *path)
>  
>  	if (!*file) {
>  		ret = -ENOMEM;
> -		goto free_basec;
> +		goto out;
>  	}
>  
>  	if (*dname == '\0') {
>  		*dir = malloc(2);
>  		if (!*dir) {
>  			ret = -ENOMEM;
> -			goto free_basec;
> +			goto out;
>  		}
>  
>  		(*dir)[0] = '/';
> @@ -1138,15 +1150,18 @@ static int sqfs_split_path(char **file, char **dir, const char *path)
>  		*dir = strdup(dname);
>  		if (!*dir) {
>  			ret = -ENOMEM;
> -			goto free_basec;
> +			goto out;
>  		}
>  	}
>  
> -free_basec:
> +out:
> +	if (ret) {
> +		free(*file);
> +		free(*dir);
> +		*dir = *file = NULL;

I also dislike these constructions (prefer a line per assignation).

> +	}
>  	free(basec);
> -free_dirc:
>  	free(dirc);
> -free_tmp:
>  	free(tmp_path);
>  
>  	return ret;

Thanks,
Miqu?l

  reply	other threads:[~2020-10-15 13:49 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-14  8:06 [PATCH 00/17] fs/squashfs: fix memory leaks and introduce exists() function Richard Genoud
2020-10-14  8:06 ` [PATCH 01/17] fs/squashfs: fix board hang-up when calling .exists() Richard Genoud
2020-10-14  8:06 ` [PATCH 02/17] fs/squashfs: sqfs_opendir: fix some memory leaks and dangling pointers Richard Genoud
2020-10-14  8:06 ` [PATCH 03/17] fs/squashfs: sqfs_opendir: simplify error handling Richard Genoud
2020-10-14  8:06 ` [PATCH 04/17] fs/squashfs: sqfs_closedir: fix memory leak Richard Genoud
2020-10-14  8:06 ` [PATCH 05/17] fs/squashfs: sqfs_split_path: fix memory leak and dangling pointers Richard Genoud
2020-10-15 13:49   ` Miquel Raynal [this message]
2020-10-14  8:06 ` [PATCH 06/17] fs/squashfs: sqfs_read_directory_table: fix memory leak Richard Genoud
2020-10-15 13:54   ` Miquel Raynal
2020-10-15 16:29     ` Richard Genoud
2020-10-15 16:38       ` Miquel Raynal
2020-10-16 12:31         ` Richard Genoud
2020-10-16 12:34           ` Miquel Raynal
2020-10-14  8:06 ` [PATCH 07/17] fs/squashfs: sqfs_search_dir: fix dangling pointer Richard Genoud
2020-10-14  8:06 ` [PATCH 08/17] fs/squashfs: sqfs_search_dir: fix memory leaks Richard Genoud
2020-10-14  8:06 ` [PATCH 09/17] fs/squashfs: sqfs_read_inode_table: fix dangling pointer Richard Genoud
2020-10-14  8:06 ` [PATCH 10/17] fs/squashfs: sqfs_concat_tokens: check if malloc succeeds Richard Genoud
2020-10-14  8:06 ` [PATCH 11/17] fs/squashfs: sqfs_size: fix dangling pointer dirs->entry Richard Genoud
2020-10-14  8:06 ` [PATCH 12/17] fs/squashfs: sqfs_size: remove useless sqfs_closedir() Richard Genoud
2020-10-14  8:06 ` [PATCH 13/17] fs/squashfs: sqfs_read: fix dangling pointer dirs->entry Richard Genoud
2020-10-14  8:06 ` [PATCH 14/17] fs/squashfs: sqfs_read: remove useless sqfs_closedir() Richard Genoud
2020-10-14  8:06 ` [PATCH 15/17] fs/squashfs: sqfs_read: fix memory leak Richard Genoud
2020-10-16 14:49   ` Richard Genoud
2020-10-14  8:06 ` [PATCH 16/17] fs/squashfs: sqfs_read: fix another " Richard Genoud
2020-10-14  8:06 ` [PATCH 17/17] fs/squashfs: implement exists() function Richard Genoud

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=20201015154910.16911d78@xps13 \
    --to=miquel.raynal@bootlin.com \
    --cc=u-boot@lists.denx.de \
    /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