From 674b354dc120e3fd938c2b9f6f8a8dbb364c8ed7 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Tue, 10 Mar 2026 17:42:46 +1300
Subject: [PATCH v2 13/19] Use stack buffer in fmgr.c.

Mechanical C string conversions.
---
 src/backend/utils/fmgr/fmgr.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/backend/utils/fmgr/fmgr.c b/src/backend/utils/fmgr/fmgr.c
index 4e26df7c63a..1614a683877 100644
--- a/src/backend/utils/fmgr/fmgr.c
+++ b/src/backend/utils/fmgr/fmgr.c
@@ -32,6 +32,7 @@
 #include "utils/fmgrtab.h"
 #include "utils/guc.h"
 #include "utils/lsyscache.h"
+#include "utils/stack_buffer.h"
 #include "utils/syscache.h"
 
 /*
@@ -154,6 +155,8 @@ fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
 	Datum		prosrcdatum;
 	char	   *prosrc;
 
+	DECLARE_STACK_BUFFER();
+
 	/*
 	 * fn_oid *must* be filled in last.  Some code assumes that if fn_oid is
 	 * valid, the whole struct is valid.  Some FmgrInfo struct's do survive
@@ -229,14 +232,14 @@ fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
 			 */
 			prosrcdatum = SysCacheGetAttrNotNull(PROCOID, procedureTuple,
 												 Anum_pg_proc_prosrc);
-			prosrc = TextDatumGetCString(prosrcdatum);
+			prosrc = stack_buffer_text_datum_to_cstring(prosrcdatum);
 			fbp = fmgr_lookupByName(prosrc);
 			if (fbp == NULL)
 				ereport(ERROR,
 						(errcode(ERRCODE_UNDEFINED_FUNCTION),
 						 errmsg("internal function \"%s\" is not in internal lookup table",
 								prosrc)));
-			pfree(prosrc);
+			stack_buffer_free(prosrc);
 			/* Should we check that nargs, strict, retset match the table? */
 			finfo->fn_addr = fbp->func;
 			/* note this policy is also assumed in fast path above */
@@ -370,6 +373,8 @@ fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
 				   *probinstring;
 		void	   *libraryhandle;
 
+		DECLARE_STACK_BUFFER();
+
 		/*
 		 * Get prosrc and probin strings (link symbol and library filename).
 		 * While in general these columns might be null, that's not allowed
@@ -377,11 +382,11 @@ fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
 		 */
 		prosrcattr = SysCacheGetAttrNotNull(PROCOID, procedureTuple,
 											Anum_pg_proc_prosrc);
-		prosrcstring = TextDatumGetCString(prosrcattr);
+		prosrcstring = stack_buffer_text_datum_to_cstring(prosrcattr);
 
 		probinattr = SysCacheGetAttrNotNull(PROCOID, procedureTuple,
 											Anum_pg_proc_probin);
-		probinstring = TextDatumGetCString(probinattr);
+		probinstring = stack_buffer_text_datum_to_cstring(probinattr);
 
 		/* Look up the function itself */
 		user_fn = load_external_function(probinstring, prosrcstring, true,
@@ -393,8 +398,8 @@ fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
 		/* Cache the addresses for later calls */
 		record_C_func(procedureTuple, user_fn, inforec);
 
-		pfree(prosrcstring);
-		pfree(probinstring);
+		stack_buffer_free(prosrcstring);
+		stack_buffer_free(probinstring);
 	}
 
 	switch (inforec->api_version)
-- 
2.53.0

