Fix tiny memory leaks

Started by Tofig Alievabout 1 year ago6 messages
#1Tofig Aliev
t.aliev@postgrespro.ru
1 attachment(s)

Hi, hackers!

There is a memory leak in functions check_application_name() and
check_cluster_name().
Functions are located in src/backend/commands/variable.c

The leak can be triggered using SQL command: SET
application_name=new_name;
You can run pgbench with this command for a long time by passing this
command in file via '-f' flag.

regards,
Tofig

Attachments:

0001-Fix-tiny-memory-leaks.patchtext/x-diff; name=0001-Fix-tiny-memory-leaks.patchDownload
From a801d03c57819d3de181967d72260cb42bbcf6a3 Mon Sep 17 00:00:00 2001
From: Tofig Aliev <t.aliev@postgrespro.ru>
Date: Fri, 6 Dec 2024 14:58:38 +0700
Subject: [PATCH] Fix tiny memory leaks

Memory for (*newval) allocated earlier
in the parse_and_validate_value() method.
Since this method re-allocates memory,
it is necessary to free the previously allocated memory.
---
 src/backend/commands/variable.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 0ecff94d0e..2c8059e8d8 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -1092,6 +1092,8 @@ check_application_name(char **newval, void **extra, GucSource source)
 		return false;
 	}
 
+	guc_free(*newval);
+
 	pfree(clean);
 	*newval = ret;
 	return true;
@@ -1128,6 +1130,8 @@ check_cluster_name(char **newval, void **extra, GucSource source)
 		return false;
 	}
 
+	guc_free(*newval);
+
 	pfree(clean);
 	*newval = ret;
 	return true;
-- 
2.43.0

#2Daniel Gustafsson
daniel@yesql.se
In reply to: Tofig Aliev (#1)
Re: Fix tiny memory leaks

On 6 Dec 2024, at 09:38, Tofig Aliev <t.aliev@postgrespro.ru> wrote:

There is a memory leak in functions check_application_name() and check_cluster_name().
Functions are located in src/backend/commands/variable.c

LGTM.

--
Daniel Gustafsson

#3Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Daniel Gustafsson (#2)
Re: Fix tiny memory leaks

On Fri, Dec 6, 2024 at 12:49 AM Daniel Gustafsson <daniel@yesql.se> wrote:

On 6 Dec 2024, at 09:38, Tofig Aliev <t.aliev@postgrespro.ru> wrote:

There is a memory leak in functions check_application_name() and check_cluster_name().
Functions are located in src/backend/commands/variable.c

LGTM.

LGTM. It seems commit a9d58bfe8a3a missed to fix this memory leak.

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com

#4Daniel Gustafsson
daniel@yesql.se
In reply to: Masahiko Sawada (#3)
Re: Fix tiny memory leaks

On 9 Dec 2024, at 19:52, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Fri, Dec 6, 2024 at 12:49 AM Daniel Gustafsson <daniel@yesql.se> wrote:

On 6 Dec 2024, at 09:38, Tofig Aliev <t.aliev@postgrespro.ru> wrote:

There is a memory leak in functions check_application_name() and check_cluster_name().
Functions are located in src/backend/commands/variable.c

LGTM.

LGTM. It seems commit a9d58bfe8a3a missed to fix this memory leak.

Yeah. While fairly insignificant in nature, and there has been no complaints
until now, I'm preparing a backpatch down to REL_15_STABLE to keep the code in
sync for other backpatches to apply clean.

--
Daniel Gustafsson

#5Daniel Gustafsson
daniel@yesql.se
In reply to: Daniel Gustafsson (#4)
Re: Fix tiny memory leaks

On 9 Dec 2024, at 20:03, Daniel Gustafsson <daniel@yesql.se> wrote:

On 9 Dec 2024, at 19:52, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Fri, Dec 6, 2024 at 12:49 AM Daniel Gustafsson <daniel@yesql.se> wrote:

On 6 Dec 2024, at 09:38, Tofig Aliev <t.aliev@postgrespro.ru> wrote:

There is a memory leak in functions check_application_name() and check_cluster_name().
Functions are located in src/backend/commands/variable.c

LGTM.

LGTM. It seems commit a9d58bfe8a3a missed to fix this memory leak.

Yeah. While fairly insignificant in nature, and there has been no complaints
until now, I'm preparing a backpatch down to REL_15_STABLE to keep the code in
sync for other backpatches to apply clean.

Done, but to 16 and not 15 which was a typo in the above email.

--
Daniel Gustafsson

#6Masahiko Sawada
sawada.mshk@gmail.com
In reply to: Daniel Gustafsson (#5)
Re: Fix tiny memory leaks

On Mon, Dec 9, 2024 at 12:04 PM Daniel Gustafsson <daniel@yesql.se> wrote:

On 9 Dec 2024, at 20:03, Daniel Gustafsson <daniel@yesql.se> wrote:

On 9 Dec 2024, at 19:52, Masahiko Sawada <sawada.mshk@gmail.com> wrote:

On Fri, Dec 6, 2024 at 12:49 AM Daniel Gustafsson <daniel@yesql.se> wrote:

On 6 Dec 2024, at 09:38, Tofig Aliev <t.aliev@postgrespro.ru> wrote:

There is a memory leak in functions check_application_name() and check_cluster_name().
Functions are located in src/backend/commands/variable.c

LGTM.

LGTM. It seems commit a9d58bfe8a3a missed to fix this memory leak.

Yeah. While fairly insignificant in nature, and there has been no complaints
until now, I'm preparing a backpatch down to REL_15_STABLE to keep the code in
sync for other backpatches to apply clean.

Done, but to 16 and not 15 which was a typo in the above email.

Thank you!

Regards,

--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com