From 31fbef4e654946f3513b81ddf437cabf41ae3e74 Mon Sep 17 00:00:00 2001
From: reshke kirill <reshke@double.cloud>
Date: Fri, 18 Oct 2024 12:39:48 +0000
Subject: [PATCH v28] Fix memory leak in `gist_check_page`

---
 contrib/amcheck/verify_gist.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/contrib/amcheck/verify_gist.c b/contrib/amcheck/verify_gist.c
index 63f6175e17c..3241f84cba7 100644
--- a/contrib/amcheck/verify_gist.c
+++ b/contrib/amcheck/verify_gist.c
@@ -391,6 +391,7 @@ gist_check_page(GistCheckState * check_state, GistScanItem * stack,
 	{
 		ItemId		iid = PageGetItemIdCareful(check_state->rel, stack->blkno, page, i);
 		IndexTuple	idxtuple = (IndexTuple) PageGetItem(page, iid);
+		IndexTuple  tmpTuple = NULL;
 
 		/*
 		 * Check that it's not a leftover invalid tuple from pre-9.1 See also
@@ -414,8 +415,10 @@ gist_check_page(GistCheckState * check_state, GistScanItem * stack,
 		/*
 		 * Check if this tuple is consistent with the downlink in the parent.
 		 */
-		if (stack->parenttup &&
-			gistgetadjusted(check_state->rel, stack->parenttup, idxtuple, check_state->state))
+		if (stack->parenttup)
+			tmpTuple = gistgetadjusted(check_state->rel, stack->parenttup, idxtuple, check_state->state);
+
+		if (tmpTuple)
 		{
 			/*
 			 * There was a discrepancy between parent and child tuples. We
@@ -428,6 +431,8 @@ gist_check_page(GistCheckState * check_state, GistScanItem * stack,
 			 * parent and child buffers. Thus parent tuple must include
 			 * keyspace of the child.
 			 */
+
+			pfree(tmpTuple);
 			pfree(stack->parenttup);
 			stack->parenttup = gist_refind_parent(check_state->rel, stack->parentblk,
 												  stack->blkno, strategy);
-- 
2.34.1

