Avoid redudant initialization and possible memory leak (src/backend/parser/parse_relation.c)

Started by Ranier Vilelaover 3 years ago5 messages
#1Ranier Vilela
ranier.vf@gmail.com
1 attachment(s)

Hi,

The commit: Revert SQL/JSON features
https://github.com/postgres/postgres/commit/2f2b18bd3f554e96a8cc885b177211be12288e4a

Left a little oversight.
I believe it will be properly corrected when it is applied again.
However, for Postgres 15 this may can cause a small memory leak.

Attached a fix patch.

regards,
Ranier Vilela

Attachments:

avoid-redundant-initialization-and-possible-memory-leak.patchapplication/octet-stream; name=avoid-redundant-initialization-and-possible-memory-leak.patchDownload
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c
index f44937a8bb..81f9ae2f02 100644
--- a/src/backend/parser/parse_relation.c
+++ b/src/backend/parser/parse_relation.c
@@ -2017,7 +2017,7 @@ addRangeTableEntryForTableFunc(ParseState *pstate,
 							   bool inFromCl)
 {
 	RangeTblEntry *rte = makeNode(RangeTblEntry);
-	char	   *refname = alias ? alias->aliasname : pstrdup("xmltable");
+	char	   *refname;
 	Alias	   *eref;
 	int			numaliases;
 
@@ -2035,7 +2035,7 @@ addRangeTableEntryForTableFunc(ParseState *pstate,
 	Assert(list_length(tf->coltypmods) == list_length(tf->colnames));
 	Assert(list_length(tf->colcollations) == list_length(tf->colnames));
 
-	refname = alias ? alias->aliasname :  pstrdup("xmltable");
+	refname = alias ? alias->aliasname : pstrdup("xmltable");
 
 	rte->rtekind = RTE_TABLEFUNC;
 	rte->relid = InvalidOid;
#2Julien Rouhaud
rjuju123@gmail.com
In reply to: Ranier Vilela (#1)
Re: Avoid redudant initialization and possible memory leak (src/backend/parser/parse_relation.c)

On Tue, Sep 13, 2022 at 10:21:22AM -0300, Ranier Vilela wrote:

Hi,

The commit: Revert SQL/JSON features
https://github.com/postgres/postgres/commit/2f2b18bd3f554e96a8cc885b177211be12288e4a

Left a little oversight.
I believe it will be properly corrected when it is applied again.
However, for Postgres 15 this may can cause a small memory leak.

It's not a memory leak, the chunk will be freed eventually when the owning
memory context is reset, but I agree that one of the two identical
initializations should be removed.

#3Alvaro Herrera
alvherre@alvh.no-ip.org
In reply to: Ranier Vilela (#1)
Re: Avoid redudant initialization and possible memory leak (src/backend/parser/parse_relation.c)

On 2022-Sep-13, Ranier Vilela wrote:

However, for Postgres 15 this may can cause a small memory leak.

What memory leak? There's no leak here.

--
Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/
Bob [Floyd] used to say that he was planning to get a Ph.D. by the "green
stamp method," namely by saving envelopes addressed to him as 'Dr. Floyd'.
After collecting 500 such letters, he mused, a university somewhere in
Arizona would probably grant him a degree. (Don Knuth)

#4Ranier Vilela
ranier.vf@gmail.com
In reply to: Alvaro Herrera (#3)
Re: Avoid redudant initialization and possible memory leak (src/backend/parser/parse_relation.c)

Em ter., 13 de set. de 2022 às 11:09, Alvaro Herrera <
alvherre@alvh.no-ip.org> escreveu:

On 2022-Sep-13, Ranier Vilela wrote:

However, for Postgres 15 this may can cause a small memory leak.

What memory leak? There's no leak here.

Yeah, as per Julien's answer, there is really no memory leak, but just
unnecessary double execution of pstrdup.
But for Postgres 15, I believe it's worth avoiding this, because it's
wasted cycles.
For Postgres 16, I believe this will be fixed as well, but for robustness,
better fix soon, IMO.

regards,
Ranier Vilela

#5Alvaro Herrera
alvherre@alvh.no-ip.org
In reply to: Ranier Vilela (#4)
Re: Avoid redudant initialization and possible memory leak (src/backend/parser/parse_relation.c)

On 2022-Sep-13, Ranier Vilela wrote:

Yeah, as per Julien's answer, there is really no memory leak, but just
unnecessary double execution of pstrdup.
But for Postgres 15, I believe it's worth avoiding this, because it's
wasted cycles.

Yeah, this is a merge mistake. Fix applied, thanks.

--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/