Hi Hackers

I have identified a potential memory leak in the `addRangeTableEntryForJoin()` function. The second parameter of `addRangeTableEntryForJoin()`, `colnames`, is a `List*` that is concatenated with another `List*`, `eref->colnames`, using `list_concat()`. We need to pass only the last `numaliases` elements of the list, for which we use `list_copy_tail`. This function creates a copy of the `colnames` list, resulting in `colnames` pointing to the current list that will not be freed. Consequently, a new list is already concatenated.

To address this issue, I have invoked `list_free(colnames)` afterwards. If anyone is aware of where the list is being freed or has any suggestions for improvement, I would greatly appreciate your input.

Best Regards,

Ilia Evdokimov,

TantorLabs LCC
From cd7aa7ac5904501085a980944dc3c18f42721c06 Mon Sep 17 00:00:00 2001
From: Ilia Evdokimov <ilya.evdoki...@tantorlabs.com>
Date: Mon, 10 Jun 2024 13:10:31 +0300
Subject: [PATCH] After concatenation two lists where the second one is from
 list_copy_tail do not free it

---
 src/backend/parser/parse_relation.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c
index 2f64eaf0e3..8230cf27cc 100644
--- a/src/backend/parser/parse_relation.c
+++ b/src/backend/parser/parse_relation.c
@@ -2258,8 +2258,11 @@ addRangeTableEntryForJoin(ParseState *pstate,
 
 	/* fill in any unspecified alias columns */
 	if (numaliases < list_length(colnames))
+	{
 		eref->colnames = list_concat(eref->colnames,
 									 list_copy_tail(colnames, numaliases));
+		list_free(colnames);
+	}
 
 	if (numaliases > list_length(colnames))
 		ereport(ERROR,
-- 
2.34.1

Reply via email to