public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] setup_tdb : fix memory leak
@ 2022-01-04 11:21 zhanchengbin
  2022-01-04 11:37 ` riteshh
  0 siblings, 1 reply; 2+ messages in thread
From: zhanchengbin @ 2022-01-04 11:21 UTC (permalink / raw)
  To: Theodore Ts'o; +Cc: linux-ext4, liuzhiqiang26, linfeilong, riteshh

In setup_tdb(), need free tdb_dir and db->tdb_fn before return,
otherwise it will cause memory leak.

Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
---
  e2fsck/dirinfo.c | 18 ++++++++++++++----
  1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/e2fsck/dirinfo.c b/e2fsck/dirinfo.c
index 49d624c5..97a303e5 100644
--- a/e2fsck/dirinfo.c
+++ b/e2fsck/dirinfo.c
@@ -49,7 +49,7 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
  	ext2_ino_t		threshold;
  	errcode_t		retval;
  	mode_t			save_umask;
-	char			*tdb_dir, uuid[40];
+	char			*tdb_dir = NULL, uuid[40];
  	int			fd, enable;

  	profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
@@ -61,11 +61,11 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)

  	if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
  	    (threshold && num_dirs <= threshold))
-		return;
+		goto tdb_dir_error;

  	retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
  	if (retval)
-		return;
+		goto tdb_dir_error;

  	uuid_unparse(ctx->fs->super->s_uuid, uuid);
  	sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
@@ -74,7 +74,7 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
  	umask(save_umask);
  	if (fd < 0) {
  		db->tdb = NULL;
-		return;
+		goto tdb_fn_error;
  	}

  	if (num_dirs < 99991)
@@ -83,6 +83,16 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
  	db->tdb = tdb_open(db->tdb_fn, num_dirs, TDB_NOLOCK | TDB_NOSYNC,
  			   O_RDWR | O_CREAT | O_TRUNC, 0600);
  	close(fd);
+
+	return;
+
+tdb_fn_error:
+	ext2fs_free_mem(&db->tdb_fn);
+	db->tdb_fn = NULL;
+tdb_dir_error:
+	if (tdb_dir) {
+		free(tdb_dir);	
+	}
  }
  #endif

-- 
2.27.0


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

* Re: [PATCH v2] setup_tdb : fix memory leak
  2022-01-04 11:21 [PATCH v2] setup_tdb : fix memory leak zhanchengbin
@ 2022-01-04 11:37 ` riteshh
  0 siblings, 0 replies; 2+ messages in thread
From: riteshh @ 2022-01-04 11:37 UTC (permalink / raw)
  To: zhanchengbin; +Cc: Theodore Ts'o, linux-ext4, liuzhiqiang26, linfeilong

On 22/01/04 07:21PM, zhanchengbin wrote:
> In setup_tdb(), need free tdb_dir and db->tdb_fn before return,
> otherwise it will cause memory leak.
>
> Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> ---
>  e2fsck/dirinfo.c | 18 ++++++++++++++----
>  1 file changed, 14 insertions(+), 4 deletions(-)
>
> diff --git a/e2fsck/dirinfo.c b/e2fsck/dirinfo.c
> index 49d624c5..97a303e5 100644
> --- a/e2fsck/dirinfo.c
> +++ b/e2fsck/dirinfo.c
> @@ -49,7 +49,7 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
>  	ext2_ino_t		threshold;
>  	errcode_t		retval;
>  	mode_t			save_umask;
> -	char			*tdb_dir, uuid[40];
> +	char			*tdb_dir = NULL, uuid[40];
>  	int			fd, enable;
>
>  	profile_get_string(ctx->profile, "scratch_files", "directory", 0, 0,
> @@ -61,11 +61,11 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
>
>  	if (!enable || !tdb_dir || access(tdb_dir, W_OK) ||
>  	    (threshold && num_dirs <= threshold))
> -		return;
> +		goto tdb_dir_error;
>
>  	retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
>  	if (retval)
> -		return;
> +		goto tdb_dir_error;
>
>  	uuid_unparse(ctx->fs->super->s_uuid, uuid);
>  	sprintf(db->tdb_fn, "%s/%s-dirinfo-XXXXXX", tdb_dir, uuid);
> @@ -74,7 +74,7 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
>  	umask(save_umask);
>  	if (fd < 0) {
>  		db->tdb = NULL;
> -		return;
> +		goto tdb_fn_error;
>  	}
>
>  	if (num_dirs < 99991)
> @@ -83,6 +83,16 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
>  	db->tdb = tdb_open(db->tdb_fn, num_dirs, TDB_NOLOCK | TDB_NOSYNC,
>  			   O_RDWR | O_CREAT | O_TRUNC, 0600);
>  	close(fd);
> +
> +	return;
> +
> +tdb_fn_error:
> +	ext2fs_free_mem(&db->tdb_fn);
> +	db->tdb_fn = NULL;

I think making tdb_fn = NULL is not needed, since ext2fs_free_mem() takes care of it.
With that removed, feel free to add:

Reviewed-by: Ritesh Harjani <riteshh@linux.ibm.com>

-riteshh



> +tdb_dir_error:
> +	if (tdb_dir) {
> +		free(tdb_dir);
> +	}
>  }
>  #endif
>
> --
> 2.27.0
>

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

end of thread, other threads:[~2022-01-04 11:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-04 11:21 [PATCH v2] setup_tdb : fix memory leak zhanchengbin
2022-01-04 11:37 ` riteshh

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