Subject: Re: bug#72446: [PATCH] stat: Fix memleak

From: Pádraig Brady <[email-masked]>
Date: Sat, 3 Aug 2024 19:34:38 +0100

tag 72446 notabug
close 72446
stop

On 03/08/2024 17:10, Dmitry Chestnykh wrote:
> format and format2 strings are allocated
> by `malloc()` inside `xasprintf` so the memory
> should be freed
> 
> * src/stat.c: Call `free()` on `format` and `format2`
> ---
>   src/stat.c | 2 ++
>   1 file changed, 2 insertions(+)
> 
> diff --git a/src/stat.c b/src/stat.c
> index 1513abfaa..47f3b5052 100644
> --- a/src/stat.c
> +++ b/src/stat.c
> @@ -1974,5 +1974,7 @@ main (int argc, char *argv[])
>              ? do_statfs (argv[i], format)
>              : do_stat (argv[i], format, format2));
>   
> +  free(format);
> +  free(format2);
>     main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
>   }

Hi,

There are a could of problems with this.
1. It's redundant to free just before exit
2. It's invalid to free this memory with the -c format specified.

valgrind currently shows no leaks with or without -c
(it does show reachable blocks, but that's fine).

thanks,
Pádraig