Re: Memory leak in adjust_data_dir - Mailing list pgsql-hackers

From Japin Li
Subject Re: Memory leak in adjust_data_dir
Date
Msg-id MEYP282MB1669C1B39D1C5AD1F9B97F9CB6079@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM
Whole thread Raw
List pgsql-hackers
On Wed, 16 Nov 2022 at 12:19, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Japin Li <japinli@hotmail.com> writes:
>> Hi, hackers,
>
> ITYM pgsql-hackers, this is off-topic here.
>

Sorry for typo the email address.

>> When I'm reviewing patch [1], I find there is a memory leak in
>> adjust_data_dir(), the cmd was allocated by psprintf(), but forget
>> releasing.
>
> Can't get excited about it in pg_ctl; that program won't run
> long enough for anybody to notice.
>

Yeah, it won't run a long time.  I find that the memory of my_exec_path
was released, so I think we also should do release on cmd.  IMO, this is
a bit confused when should we do release the memory of variables for
short lifetime?

[Here is the origin contents which I send a wrong mail-list]

Hi, hackers,

When I'm reviewing patch [1], I find there is a memory leak in
adjust_data_dir(), the cmd was allocated by psprintf(), but forget
releasing.

[1] https://www.postgresql.org/message-id/CALte62y3yZpHNFnYVz1uACaFbmb6go9fyeRaO5uHF5XaxtarbA%40mail.gmail.com

diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index ceab603c47..ace2d676fc 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -2159,6 +2159,7 @@ adjust_data_dir(void)
         write_stderr(_("%s: could not determine the data directory using command \"%s\"\n"), progname, cmd);
         exit(1);
     }
+    free(cmd);
     free(my_exec_path);
 
     /* strip trailing newline and carriage return */

-- 
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.



pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: New strategies for freezing, advancing relfrozenxid early
Next
From: Julien Rouhaud
Date:
Subject: Re: Allow file inclusion in pg_hba and pg_ident files