mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] blspec: fix memory leak handling NFS URLs
@ 2022-09-05  7:02 Ahmad Fatoum
  2022-09-13  8:16 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2022-09-05  7:02 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

parse_nfs_url() returns either an allocated string on success or an
error pointer otherwise. blspec_bootentry_provider() will early
exit if the path couldn't be resolved and thus not free() the string.

Fix this memory leak. While at it, change parse_nfs_url() to return NULL
on error. The error code is unused and just returning NULL, simplifies
the code.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/blspec.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/common/blspec.c b/common/blspec.c
index 9bb25ee72123..d391f690ad0c 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -341,10 +341,10 @@ static char *parse_nfs_url(const char *url)
 	int ret;
 
 	if (!IS_ENABLED(CONFIG_FS_NFS))
-		return ERR_PTR(-ENOSYS);
+		return NULL;
 
 	if (strncmp(url, "nfs://", 6))
-		return ERR_PTR(-EINVAL);
+		return NULL;
 
 	url += 6;
 
@@ -413,7 +413,7 @@ out:
 	if (ret)
 		free(mountpath);
 
-	return ret ? ERR_PTR(ret) : mountpath;
+	return ret ? NULL : mountpath;
 }
 
 /*
@@ -824,12 +824,12 @@ static int blspec_bootentry_provider(struct bootentries *bootentries,
 	if (*name == '/' || !strncmp(name, "nfs://", 6)) {
 		char *nfspath = parse_nfs_url(name);
 
-		if (!IS_ERR(nfspath))
+		if (nfspath)
 			name = nfspath;
 
 		ret = stat(name, &s);
 		if (ret)
-			return found;
+			goto out;
 
 		if (S_ISDIR(s.st_mode))
 			ret = blspec_scan_directory(bootentries, name);
@@ -838,8 +838,8 @@ static int blspec_bootentry_provider(struct bootentries *bootentries,
 		if (ret > 0)
 			found += ret;
 
-		if (!IS_ERR(nfspath))
-			free(nfspath);
+out:
+		free(nfspath);
 	}
 
 	return found;
-- 
2.30.2




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] blspec: fix memory leak handling NFS URLs
  2022-09-05  7:02 [PATCH] blspec: fix memory leak handling NFS URLs Ahmad Fatoum
@ 2022-09-13  8:16 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-09-13  8:16 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Mon, Sep 05, 2022 at 09:02:47AM +0200, Ahmad Fatoum wrote:
> parse_nfs_url() returns either an allocated string on success or an
> error pointer otherwise. blspec_bootentry_provider() will early
> exit if the path couldn't be resolved and thus not free() the string.
> 
> Fix this memory leak. While at it, change parse_nfs_url() to return NULL
> on error. The error code is unused and just returning NULL, simplifies
> the code.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  common/blspec.c | 14 +++++++-------
>  1 file changed, 7 insertions(+), 7 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/common/blspec.c b/common/blspec.c
> index 9bb25ee72123..d391f690ad0c 100644
> --- a/common/blspec.c
> +++ b/common/blspec.c
> @@ -341,10 +341,10 @@ static char *parse_nfs_url(const char *url)
>  	int ret;
>  
>  	if (!IS_ENABLED(CONFIG_FS_NFS))
> -		return ERR_PTR(-ENOSYS);
> +		return NULL;
>  
>  	if (strncmp(url, "nfs://", 6))
> -		return ERR_PTR(-EINVAL);
> +		return NULL;
>  
>  	url += 6;
>  
> @@ -413,7 +413,7 @@ out:
>  	if (ret)
>  		free(mountpath);
>  
> -	return ret ? ERR_PTR(ret) : mountpath;
> +	return ret ? NULL : mountpath;
>  }
>  
>  /*
> @@ -824,12 +824,12 @@ static int blspec_bootentry_provider(struct bootentries *bootentries,
>  	if (*name == '/' || !strncmp(name, "nfs://", 6)) {
>  		char *nfspath = parse_nfs_url(name);
>  
> -		if (!IS_ERR(nfspath))
> +		if (nfspath)
>  			name = nfspath;
>  
>  		ret = stat(name, &s);
>  		if (ret)
> -			return found;
> +			goto out;
>  
>  		if (S_ISDIR(s.st_mode))
>  			ret = blspec_scan_directory(bootentries, name);
> @@ -838,8 +838,8 @@ static int blspec_bootentry_provider(struct bootentries *bootentries,
>  		if (ret > 0)
>  			found += ret;
>  
> -		if (!IS_ERR(nfspath))
> -			free(nfspath);
> +out:
> +		free(nfspath);
>  	}
>  
>  	return found;
> -- 
> 2.30.2
> 
> 
> 

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



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-13  8:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-05  7:02 [PATCH] blspec: fix memory leak handling NFS URLs Ahmad Fatoum
2022-09-13  8:16 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox