All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] setup_tdb : fix memory leak
@ 2021-11-30  6:40 zhanchengbin
  2021-12-10  3:35 ` zhanchengbin
  2022-01-03 12:31 ` riteshh
  0 siblings, 2 replies; 5+ messages in thread
From: zhanchengbin @ 2021-11-30  6:40 UTC (permalink / raw)
  To: Theodore Ts'o; +Cc: linux-ext4, liuzhiqiang26, linfeilong

In setup_tdb(), need free tdb_dir before return,
otherwise it will cause memory leak.

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

diff --git a/e2fsck/dirinfo.c b/e2fsck/dirinfo.c
index 49d624c5..a2b36d8e 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 error;

  	retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
  	if (retval)
-		return;
+		goto 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 error;
  	}

  	if (num_dirs < 99991)
@@ -83,6 +83,11 @@ 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);
+
+error:
+	if(tdb_dir) {
+		free(tdb_dir);
+	}
  }
  #endif

-- 
2.23.0



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

* Re: [PATCH] setup_tdb : fix memory leak
  2021-11-30  6:40 [PATCH] setup_tdb : fix memory leak zhanchengbin
@ 2021-12-10  3:35 ` zhanchengbin
  2021-12-31  1:30   ` Zhiqiang Liu
  2022-01-03 12:31 ` riteshh
  1 sibling, 1 reply; 5+ messages in thread
From: zhanchengbin @ 2021-12-10  3:35 UTC (permalink / raw)
  To: Theodore Ts'o; +Cc: linux-ext4, liuzhiqiang26, linfeilong

ping

在 2021/11/30 14:40, zhanchengbin 写道:
> In setup_tdb(), need free tdb_dir before return,
> otherwise it will cause memory leak.
> 
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
> ---
>   e2fsck/dirinfo.c | 13 +++++++++----
>   1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/e2fsck/dirinfo.c b/e2fsck/dirinfo.c
> index 49d624c5..a2b36d8e 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 error;
> 
>       retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
>       if (retval)
> -        return;
> +        goto 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 error;
>       }
> 
>       if (num_dirs < 99991)
> @@ -83,6 +83,11 @@ 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);
> +
> +error:
> +    if(tdb_dir) {
> +        free(tdb_dir);
> +    }
>   }
>   #endif
> 

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

* Re: [PATCH] setup_tdb : fix memory leak
  2021-12-10  3:35 ` zhanchengbin
@ 2021-12-31  1:30   ` Zhiqiang Liu
  0 siblings, 0 replies; 5+ messages in thread
From: Zhiqiang Liu @ 2021-12-31  1:30 UTC (permalink / raw)
  To: zhanchengbin, Theodore Ts'o; +Cc: linux-ext4, linfeilong

friendly ping..

On 2021/12/10 11:35, zhanchengbin wrote:
> ping
>
> 在 2021/11/30 14:40, zhanchengbin 写道:
>> In setup_tdb(), need free tdb_dir before return,
>> otherwise it will cause memory leak.
>>
>> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>> Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
>> ---
>>   e2fsck/dirinfo.c | 13 +++++++++----
>>   1 file changed, 9 insertions(+), 4 deletions(-)
>>
>> diff --git a/e2fsck/dirinfo.c b/e2fsck/dirinfo.c
>> index 49d624c5..a2b36d8e 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 error;
>>
>>       retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
>>       if (retval)
>> -        return;
>> +        goto 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 error;
>>       }
>>
>>       if (num_dirs < 99991)
>> @@ -83,6 +83,11 @@ 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);
>> +
>> +error:
>> +    if(tdb_dir) {
>> +        free(tdb_dir);
>> +    }
>>   }
>>   #endif
>>
>
> .


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

* Re: [PATCH] setup_tdb : fix memory leak
  2021-11-30  6:40 [PATCH] setup_tdb : fix memory leak zhanchengbin
  2021-12-10  3:35 ` zhanchengbin
@ 2022-01-03 12:31 ` riteshh
  2022-01-04  2:34   ` zhanchengbin
  1 sibling, 1 reply; 5+ messages in thread
From: riteshh @ 2022-01-03 12:31 UTC (permalink / raw)
  To: zhanchengbin; +Cc: Theodore Ts'o, linux-ext4, liuzhiqiang26, linfeilong

On 21/11/30 02:40PM, zhanchengbin wrote:
> In setup_tdb(), need free tdb_dir before return,
> otherwise it will cause memory leak.
>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
> ---
>  e2fsck/dirinfo.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/e2fsck/dirinfo.c b/e2fsck/dirinfo.c
> index 49d624c5..a2b36d8e 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 error;
>
>  	retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);

I think freeing of db->tdb_fn should also be handled in case of an error.

>  	if (retval)
> -		return;
> +		goto 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 error;

So in case of an error we should call ext2fs_free_mem(&db->tdb_fn), right?

Rest looks good to me.

-ritesh


>  	}
>
>  	if (num_dirs < 99991)
> @@ -83,6 +83,11 @@ 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);
> +
> +error:
> +	if(tdb_dir) {
> +		free(tdb_dir);
> +	}
>  }
>  #endif
>
> --
> 2.23.0
>
>

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

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

Yes, you are right. I will submit Patch V2 in the future.

在 2022/1/3 20:31, riteshh 写道:
> On 21/11/30 02:40PM, zhanchengbin wrote:
>> In setup_tdb(), need free tdb_dir before return,
>> otherwise it will cause memory leak.
>>
>> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>> Signed-off-by: zhanchengbin <zhanchengbin1@huawei.com>
>> ---
>>   e2fsck/dirinfo.c | 13 +++++++++----
>>   1 file changed, 9 insertions(+), 4 deletions(-)
>>
>> diff --git a/e2fsck/dirinfo.c b/e2fsck/dirinfo.c
>> index 49d624c5..a2b36d8e 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 error;
>>
>>   	retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &db->tdb_fn);
> 
> I think freeing of db->tdb_fn should also be handled in case of an error.
> 
>>   	if (retval)
>> -		return;
>> +		goto 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 error;
> 
> So in case of an error we should call ext2fs_free_mem(&db->tdb_fn), right?
> 
> Rest looks good to me.
> 
> -ritesh
> 
> 
>>   	}
>>
>>   	if (num_dirs < 99991)
>> @@ -83,6 +83,11 @@ 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);
>> +
>> +error:
>> +	if(tdb_dir) {
>> +		free(tdb_dir);
>> +	}
>>   }
>>   #endif
>>
>> --
>> 2.23.0
>>
>>
> .
> 

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

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

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30  6:40 [PATCH] setup_tdb : fix memory leak zhanchengbin
2021-12-10  3:35 ` zhanchengbin
2021-12-31  1:30   ` Zhiqiang Liu
2022-01-03 12:31 ` riteshh
2022-01-04  2:34   ` zhanchengbin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.