diff mbox series

xfsdump: Fix memory leak

Message ID 20231214121715.562273-1-preichl@redhat.com (mailing list archive)
State Superseded
Headers show
Series xfsdump: Fix memory leak | expand

Commit Message

Pavel Reichl Dec. 14, 2023, 12:17 p.m. UTC
Fix memory leak found by coverity.

>>>     CID 1554295:  Resource leaks  (RESOURCE_LEAK)
>>>     Failing to save or free storage allocated by strdup(path) leaks it.

Signed-off-by: Pavel Reichl <preichl@redhat.com>
---
 restore/tree.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

Comments

Darrick J. Wong Dec. 14, 2023, 5:09 p.m. UTC | #1
On Thu, Dec 14, 2023 at 01:17:15PM +0100, Pavel Reichl wrote:
> Fix memory leak found by coverity.
> 
> >>>     CID 1554295:  Resource leaks  (RESOURCE_LEAK)
> >>>     Failing to save or free storage allocated by strdup(path) leaks it.
> 
> Signed-off-by: Pavel Reichl <preichl@redhat.com>
> ---
>  restore/tree.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/restore/tree.c b/restore/tree.c
> index 6f3180f..66dd9df 100644
> --- a/restore/tree.c
> +++ b/restore/tree.c
> @@ -4977,9 +4977,22 @@ static int
>  mkdir_r(char *path)
>  {
>  	struct stat sbuf;
> +	char *path_copy;
> +	int ret;
>  
>  	if (stat(path, &sbuf) < 0) {
> -		if (mkdir_r(dirname(strdup(path))) < 0)
> +		path_copy = strdup(path);
> +		if (!path_copy) {
> +			mlog(MLOG_TRACE | MLOG_ERROR | MLOG_TREE, _(
> +				"unable to allocate memory for a path\n"));

Nit: the _( should be on the same line as the format string, right?

With that fixed,
Reviewed-by: Darrick J. Wong <djwong@kernel.org>

--D


> +			mlog_exit(EXIT_ERROR, RV_ERROR);
> +			exit(1);
> +		}
> +
> +		ret = mkdir_r(dirname(path_copy));
> +		free(path_copy);
> +
> +		if (ret < 0)
>  			return -1;
>  		return mkdir(path, 0755);
>  	}
> -- 
> 2.43.0
> 
>
diff mbox series

Patch

diff --git a/restore/tree.c b/restore/tree.c
index 6f3180f..66dd9df 100644
--- a/restore/tree.c
+++ b/restore/tree.c
@@ -4977,9 +4977,22 @@  static int
 mkdir_r(char *path)
 {
 	struct stat sbuf;
+	char *path_copy;
+	int ret;
 
 	if (stat(path, &sbuf) < 0) {
-		if (mkdir_r(dirname(strdup(path))) < 0)
+		path_copy = strdup(path);
+		if (!path_copy) {
+			mlog(MLOG_TRACE | MLOG_ERROR | MLOG_TREE, _(
+				"unable to allocate memory for a path\n"));
+			mlog_exit(EXIT_ERROR, RV_ERROR);
+			exit(1);
+		}
+
+		ret = mkdir_r(dirname(path_copy));
+		free(path_copy);
+
+		if (ret < 0)
 			return -1;
 		return mkdir(path, 0755);
 	}