0002-Change-pfree-to-accept-NULL-argument.patch

text/plain

Filename: 0002-Change-pfree-to-accept-NULL-argument.patch
Type: text/plain
Part: 1
Message: Change pfree to accept NULL argument

Patch

Format: format-patch
Series: patch 0002
Subject: Change pfree to accept NULL argument
File+
src/backend/utils/mmgr/mcxt.c 6 1
src/backend/utils/mmgr/README 5 1
From f5eea6d2f1e8a8ed81cf177cef320e041bbff90b Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Mon, 22 Aug 2022 16:51:52 +0200
Subject: [PATCH 2/5] Change pfree to accept NULL argument

---
 src/backend/utils/mmgr/README | 6 +++++-
 src/backend/utils/mmgr/mcxt.c | 7 ++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/backend/utils/mmgr/README b/src/backend/utils/mmgr/README
index 221b4bd343..e552c6ada6 100644
--- a/src/backend/utils/mmgr/README
+++ b/src/backend/utils/mmgr/README
@@ -66,7 +66,11 @@ pointer, but a valid chunk of which no bytes may be used.  However, the
 chunk might later be repalloc'd larger; it can also be pfree'd without
 error.  Similarly, repalloc allows realloc'ing to zero size.
 
-* pfree and repalloc do not accept a NULL pointer.  This is intentional.
+* repalloc does not accept a NULL pointer.  This is intentional.  (As
+mentioned above, repalloc does not depend on the current memory
+context.  But then it needs to know which memory context to do the
+allocation in.  So the first allocation has to be done outside
+repalloc.)
 
 
 The Current Memory Context
diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c
index e12be1b9bd..d85e961c1e 100644
--- a/src/backend/utils/mmgr/mcxt.c
+++ b/src/backend/utils/mmgr/mcxt.c
@@ -1174,7 +1174,12 @@ palloc_extended(Size size, int flags)
 void
 pfree(void *pointer)
 {
-	MemoryContext context = GetMemoryChunkContext(pointer);
+	MemoryContext context;
+
+	if (!pointer)
+		return;
+
+	context = GetMemoryChunkContext(pointer);
 
 	context->methods->free_p(context, pointer);
 	VALGRIND_MEMPOOL_FREE(context, pointer);
-- 
2.37.1