GNU bug report logs - #72446
[PATCH] stat: Fix memleak

Previous Next

Package: coreutils;

Reported by: Dmitry Chestnykh <dm.chestnykh <at> gmail.com>

Date: Sat, 3 Aug 2024 17:24:02 UTC

Severity: normal

Tags: notabug, patch

Done: Pádraig Brady <P <at> draigBrady.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Pádraig Brady <P <at> draigBrady.com>
To: Dmitry Chestnykh <dm.chestnykh <at> gmail.com>, 72446 <at> debbugs.gnu.org
Subject: bug#72446: [PATCH] stat: Fix memleak
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




This bug report was last modified 289 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.