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.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 72446 in the body.
You can then email your comments to 72446 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#72446; Package coreutils. (Sat, 03 Aug 2024 17:24:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Dmitry Chestnykh <dm.chestnykh <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Sat, 03 Aug 2024 17:24:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Dmitry Chestnykh <dm.chestnykh <at> gmail.com>
To: bug-coreutils <at> gnu.org
Cc: Dmitry Chestnykh <dm.chestnykh <at> gmail.com>
Subject: [PATCH] stat: Fix memleak
Date: Sat,  3 Aug 2024 19:10:28 +0300
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);
 }
-- 
2.46.0





Information forwarded to bug-coreutils <at> gnu.org:
bug#72446; Package coreutils. (Sat, 03 Aug 2024 18:37:01 GMT) Full text and rfc822 format available.

Message #8 received at 72446 <at> debbugs.gnu.org (full text, mbox):

From: Pádraig Brady <P <at> draigBrady.com>
To: Dmitry Chestnykh <dm.chestnykh <at> gmail.com>, 72446 <at> debbugs.gnu.org
Subject: Re: 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




Added tag(s) notabug. Request was from Pádraig Brady <P <at> draigBrady.com> to control <at> debbugs.gnu.org. (Sat, 03 Aug 2024 18:37:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 72446 <at> debbugs.gnu.org and Dmitry Chestnykh <dm.chestnykh <at> gmail.com> Request was from Pádraig Brady <P <at> draigBrady.com> to control <at> debbugs.gnu.org. (Sat, 03 Aug 2024 18:37:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 01 Sep 2024 11:24:09 GMT) Full text and rfc822 format available.

This bug report was last modified 288 days ago.

Previous Next


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