From d7f10f39ca2d84220a96dba473371c6de0cc41d7 Mon Sep 17 00:00:00 2001
From: David Rowley <drowley@postgresql.org>
Date: Tue, 13 Jul 2021 13:31:29 +1200
Subject: [PATCH] Robustify tuplesort's free_sort_tuple function

41469253e went to the trouble of removing a theoretical bug from
free_sort_tuple by checking if the tuple was NULL before freeing it. Let's
make this a little more robust by also setting the tuple to NULL so that
should we be called again we won't end up doing a pfree on the already
pfree'd tuple. Per advice from Tom Lane.

Discussion: https://postgr.es/m/3188192.1626136953@sss.pgh.pa.us
Backpatch-through: 9.6, same as 41469253e
---
 src/backend/utils/sort/tuplesort.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c
index 513b2d933f8..80bc67bc9e5 100644
--- a/src/backend/utils/sort/tuplesort.c
+++ b/src/backend/utils/sort/tuplesort.c
@@ -4462,5 +4462,6 @@ free_sort_tuple(Tuplesortstate *state, SortTuple *stup)
 	{
 		FREEMEM(state, GetMemoryChunkSpace(stup->tuple));
 		pfree(stup->tuple);
+		stup->tuple = NULL;
 	}
 }
-- 
2.39.5