GetRelationPublicationActions. - Remove unreachable code

Started by Peter Smithalmost 4 years ago3 messages
#1Peter Smith
smithpb2250@gmail.com
1 attachment(s)

Hi hackers.

There appears to be some unreachable code in the relcache function
GetRelationPublicationActions.

When the 'relation->rd_pubactions' is not NULL then the function
unconditionally returns early (near the top).

Therefore, the following code (near the bottom) seems to have no
purpose because IIUC the 'rd_pubactions' can never be not NULL here.

if (relation->rd_pubactions)
{
pfree(relation->rd_pubactions);
relation->rd_pubactions = NULL;
}

The latest Code Coverage report [1]https://coverage.postgresql.org/src/backend/utils/cache/relcache.c.gcov.html also shows that this code is never executed.

5601 3556 : if (relation->rd_pubactions)
5602 : {
5603 0 : pfree(relation->rd_pubactions);
5604 0 : relation->rd_pubactions = NULL;
5605 : }

~~

PSA a patch to remove this unreachable code.

------
[1]: https://coverage.postgresql.org/src/backend/utils/cache/relcache.c.gcov.html

Kind Regards,
Peter Smith.
Fujitsu Australia

Attachments:

v1-0001-Remove-unreachable-code-in-GetRelationPublication.patchapplication/octet-stream; name=v1-0001-Remove-unreachable-code-in-GetRelationPublication.patchDownload
From 8bb3354f1868ceb945cbfa7f6713c2bbfbf871d6 Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Thu, 10 Feb 2022 10:59:35 +1100
Subject: [PATCH v1] Remove unreachable code in GetRelationPublicationActions.

When the 'relation->rd_pubactions' is not NULL then the function unconditionally
returns early (near the top). Therefore, some later code seems unreachable because
'rd_pubactions' can never be not NULL here.

This patch removes the unreachable code.
---
 src/backend/utils/cache/relcache.c | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 2707fed..a585aca 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -5598,12 +5598,6 @@ GetRelationPublicationActions(Relation relation)
 			break;
 	}
 
-	if (relation->rd_pubactions)
-	{
-		pfree(relation->rd_pubactions);
-		relation->rd_pubactions = NULL;
-	}
-
 	/* Now save copy of the actions in the relcache entry. */
 	oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
 	relation->rd_pubactions = palloc(sizeof(PublicationActions));
-- 
1.8.3.1

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Smith (#1)
Re: GetRelationPublicationActions. - Remove unreachable code

Peter Smith <smithpb2250@gmail.com> writes:

There appears to be some unreachable code in the relcache function
GetRelationPublicationActions.
When the 'relation->rd_pubactions' is not NULL then the function
unconditionally returns early (near the top).
Therefore, the following code (near the bottom) seems to have no
purpose because IIUC the 'rd_pubactions' can never be not NULL here.

I'm not sure it's as unreachable as all that. What you're not
accounting for is the possibility of recursive cache loading,
ie something down inside the catalog fetches we have to do here
could already have made the field valid.

Admittedly, that's probably not very likely given expected usage
patterns (to wit, that system catalogs shouldn't really have
publication actions). But there are other places in relcache.c where
a coding pattern similar to this is demonstrably necessary to avoid
memory leakage. I'd rather leave the code as-is, maybe add a comment
along the lines of "rd_pubactions is probably still NULL, but just in
case something already made it valid, avoid leaking memory".

regards, tom lane

#3Peter Smith
smithpb2250@gmail.com
In reply to: Tom Lane (#2)
1 attachment(s)
Re: GetRelationPublicationActions. - Remove unreachable code

On Thu, Feb 10, 2022 at 11:34 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Peter Smith <smithpb2250@gmail.com> writes:

There appears to be some unreachable code in the relcache function
GetRelationPublicationActions.
When the 'relation->rd_pubactions' is not NULL then the function
unconditionally returns early (near the top).
Therefore, the following code (near the bottom) seems to have no
purpose because IIUC the 'rd_pubactions' can never be not NULL here.

I'm not sure it's as unreachable as all that. What you're not
accounting for is the possibility of recursive cache loading,
ie something down inside the catalog fetches we have to do here
could already have made the field valid.

Admittedly, that's probably not very likely given expected usage
patterns (to wit, that system catalogs shouldn't really have
publication actions). But there are other places in relcache.c where
a coding pattern similar to this is demonstrably necessary to avoid
memory leakage. I'd rather leave the code as-is, maybe add a comment
along the lines of "rd_pubactions is probably still NULL, but just in
case something already made it valid, avoid leaking memory".

OK. Thanks for your explanation and advice.

PSA another patch to just a comment as suggested.

------
Kind Regards,
Peter Smith.
Fujitsu Australia

Attachments:

v2-0001-GetRelationPublicationActions-comment-code.patchapplication/octet-stream; name=v2-0001-GetRelationPublicationActions-comment-code.patchDownload
From 693a591c34af23757a6f74f5b01fb06e12b9e28f Mon Sep 17 00:00:00 2001
From: Peter Smith <peter.b.smith@fujitsu.com>
Date: Thu, 10 Feb 2022 11:54:27 +1100
Subject: [PATCH v2] GetRelationPublicationActions - comment code.

Added a comment to clarify why code that on brief inspection appears unreachable, might be necessary.
---
 src/backend/utils/cache/relcache.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 2707fed..616ac5d 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -5598,6 +5598,10 @@ GetRelationPublicationActions(Relation relation)
 			break;
 	}
 
+	/*
+	 * The rd_pubactions is probably still NULL, but just in case something
+	 * already made it valid, avoid leaking memory.
+	 */
 	if (relation->rd_pubactions)
 	{
 		pfree(relation->rd_pubactions);
-- 
1.8.3.1