From 39b2d5f3d949dbc1f9edea76d38d8813b4e40696 Mon Sep 17 00:00:00 2001
From: Tristan Partin <tristan@partin.io>
Date: Thu, 12 Nov 2026 19:30:14 +0000
Subject: [PATCH v2 2/2] Mark some memory allocation functions with
 pg_attribute_malloc

This list is going to be incomplete, but it aims to first tackle
functions that look like malloc, sprintf, calloc, strdup, etc.

Author: Tristan Partin <tristan@partin.io>
Signed-off-by: Tristan Partin <tristan@partin.io>
---
 src/backend/storage/ipc/shmem.c               |  2 +-
 src/include/common/fe_memutils.h              | 32 ++++++++--------
 src/include/executor/spi.h                    |  2 +-
 src/include/storage/shm_toc.h                 |  2 +-
 src/include/storage/shmem.h                   |  4 +-
 src/include/utils/guc.h                       |  4 +-
 src/include/utils/palloc.h                    | 38 +++++++++----------
 src/interfaces/ecpg/ecpglib/ecpglib_extern.h  |  6 +--
 .../ecpg/pgtypeslib/pgtypeslib_extern.h       |  4 +-
 src/interfaces/ecpg/preproc/preproc_extern.h  |  8 ++--
 10 files changed, 51 insertions(+), 51 deletions(-)

diff --git a/src/backend/storage/ipc/shmem.c b/src/backend/storage/ipc/shmem.c
index 8d81893b470..66f25333e1c 100644
--- a/src/backend/storage/ipc/shmem.c
+++ b/src/backend/storage/ipc/shmem.c
@@ -243,7 +243,7 @@ typedef struct ShmemAllocatorData
 
 #define ShmemIndexLock (&ShmemAllocator->index_lock)
 
-static void *ShmemAllocRaw(Size size, Size alignment, Size *allocated_size);
+pg_attribute_malloc static void *ShmemAllocRaw(Size size, Size alignment, Size *allocated_size);
 
 /* shared memory global variables */
 
diff --git a/src/include/common/fe_memutils.h b/src/include/common/fe_memutils.h
index 0db64ab4389..4f640f1340d 100644
--- a/src/include/common/fe_memutils.h
+++ b/src/include/common/fe_memutils.h
@@ -34,10 +34,10 @@
  * "Safe" memory allocation functions --- these exit(1) on failure
  * (except pg_malloc_extended with MCXT_ALLOC_NO_OOM)
  */
-pg_attribute_returns_nonnull extern char *pg_strdup(const char *in);
-pg_attribute_returns_nonnull extern void *pg_malloc(size_t size);
-pg_attribute_returns_nonnull extern void *pg_malloc0(size_t size);
-extern void *pg_malloc_extended(size_t size, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pg_strdup(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc(size_t size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc0(size_t size);
+pg_attribute_malloc extern void *pg_malloc_extended(size_t size, int flags);
 pg_attribute_returns_nonnull extern void *pg_realloc(void *ptr, size_t size);
 extern void pg_free(void *ptr);
 
@@ -46,9 +46,9 @@ extern void pg_free(void *ptr);
  */
 extern Size add_size(Size s1, Size s2);
 extern Size mul_size(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *pg_malloc_mul(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *pg_malloc0_mul(Size s1, Size s2);
-extern void *pg_malloc_mul_extended(Size s1, Size s2, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc_mul(Size s1, Size s2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *pg_malloc0_mul(Size s1, Size s2);
+pg_attribute_malloc extern void *pg_malloc_mul_extended(Size s1, Size s2, int flags);
 pg_attribute_returns_nonnull extern void *pg_realloc_mul(void *p, Size s1, Size s2);
 
 /*
@@ -75,16 +75,16 @@ pg_attribute_returns_nonnull extern void *pg_realloc_mul(void *p, Size s1, Size
 #define pg_realloc_array(pointer, type, count) ((type *) pg_realloc_mul(pointer, sizeof(type), count))
 
 /* Equivalent functions, deliberately named the same as backend functions */
-pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
-pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size size);
-pg_attribute_returns_nonnull extern void *palloc(Size size);
-pg_attribute_returns_nonnull extern void *palloc0(Size size);
-extern void *palloc_extended(Size size, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc(Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0(Size size);
+pg_attribute_malloc extern void *palloc_extended(Size size, int flags);
 pg_attribute_returns_nonnull extern void *repalloc(void *pointer, Size size);
 extern void pfree(void *pointer);
-pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
-extern void *palloc_mul_extended(Size s1, Size s2, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
+pg_attribute_malloc extern void *palloc_mul_extended(Size s1, Size s2, int flags);
 pg_attribute_returns_nonnull extern void *repalloc_mul(void *p, Size s1, Size s2);
 
 #define palloc_object(type) ((type *) palloc(sizeof(type)))
@@ -95,7 +95,7 @@ pg_attribute_returns_nonnull extern void *repalloc_mul(void *p, Size s1, Size s2
 #define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, sizeof(type), count))
 
 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
-pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
 extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
 
 #endif							/* FE_MEMUTILS_H */
diff --git a/src/include/executor/spi.h b/src/include/executor/spi.h
index c7a3b7abb26..1ddb553b90a 100644
--- a/src/include/executor/spi.h
+++ b/src/include/executor/spi.h
@@ -164,7 +164,7 @@ extern char *SPI_gettype(TupleDesc tupdesc, int fnumber);
 extern Oid	SPI_gettypeid(TupleDesc tupdesc, int fnumber);
 extern char *SPI_getrelname(Relation rel);
 extern char *SPI_getnspname(Relation rel);
-extern void *SPI_palloc(Size size);
+pg_attribute_malloc extern void *SPI_palloc(Size size);
 extern void *SPI_repalloc(void *pointer, Size size);
 extern void SPI_pfree(void *pointer);
 extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen);
diff --git a/src/include/storage/shm_toc.h b/src/include/storage/shm_toc.h
index 65e4f16eee9..3f597166a1a 100644
--- a/src/include/storage/shm_toc.h
+++ b/src/include/storage/shm_toc.h
@@ -29,7 +29,7 @@ typedef struct shm_toc shm_toc;
 
 extern shm_toc *shm_toc_create(uint64 magic, void *address, Size nbytes);
 extern shm_toc *shm_toc_attach(uint64 magic, void *address);
-extern void *shm_toc_allocate(shm_toc *toc, Size nbytes);
+pg_attribute_malloc extern void *shm_toc_allocate(shm_toc *toc, Size nbytes);
 extern Size shm_toc_freespace(shm_toc *toc);
 extern void shm_toc_insert(shm_toc *toc, uint64 key, void *address);
 extern void *shm_toc_lookup(shm_toc *toc, uint64 key, bool noError);
diff --git a/src/include/storage/shmem.h b/src/include/storage/shmem.h
index 43b636868c7..a5ab0e7a364 100644
--- a/src/include/storage/shmem.h
+++ b/src/include/storage/shmem.h
@@ -186,8 +186,8 @@ extern void ShmemRequestHashWithOpts(const ShmemHashOpts *options);
 extern void *ShmemInitStruct(const char *name, Size size, bool *foundPtr);
 extern HTAB *ShmemInitHash(const char *name, int64 nelems,
 						   HASHCTL *infoP, int hash_flags);
-extern void *ShmemAlloc(Size size);
-extern void *ShmemAllocNoError(Size size);
+pg_attribute_malloc extern void *ShmemAlloc(Size size);
+pg_attribute_malloc extern void *ShmemAllocNoError(Size size);
 
 extern PGDLLIMPORT Size pg_get_shmem_pagesize(void);
 
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index 164efba6b51..328d4d99a0c 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -472,9 +472,9 @@ extern ArrayType *GUCArrayAdd(ArrayType *array, const char *name, const char *va
 extern ArrayType *GUCArrayDelete(ArrayType *array, const char *name);
 extern ArrayType *GUCArrayReset(ArrayType *array);
 
-extern void *guc_malloc(int elevel, size_t size);
+pg_attribute_malloc extern void *guc_malloc(int elevel, size_t size);
 pg_nodiscard extern void *guc_realloc(int elevel, void *old, size_t size);
-extern char *guc_strdup(int elevel, const char *src);
+pg_attribute_malloc extern char *guc_strdup(int elevel, const char *src);
 extern void guc_free(void *ptr);
 
 #ifdef EXEC_BACKEND
diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h
index 0c7452fe30d..aa8243fa5b8 100644
--- a/src/include/utils/palloc.h
+++ b/src/include/utils/palloc.h
@@ -68,17 +68,17 @@ extern PGDLLIMPORT MemoryContext CurrentMemoryContext;
 /*
  * Fundamental memory-allocation operations (more are in utils/memutils.h)
  */
-pg_attribute_returns_nonnull extern void *MemoryContextAlloc(MemoryContext context, Size size);
-pg_attribute_returns_nonnull extern void *MemoryContextAllocZero(MemoryContext context, Size size);
-extern void *MemoryContextAllocExtended(MemoryContext context,
-										Size size, int flags);
-extern void *MemoryContextAllocAligned(MemoryContext context,
-									   Size size, Size alignto, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *MemoryContextAlloc(MemoryContext context, Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *MemoryContextAllocZero(MemoryContext context, Size size);
+pg_attribute_malloc extern void *MemoryContextAllocExtended(MemoryContext context,
+															Size size, int flags);
+pg_attribute_malloc extern void *MemoryContextAllocAligned(MemoryContext context,
+														   Size size, Size alignto, int flags);
 
-pg_attribute_returns_nonnull extern void *palloc(Size size);
-pg_attribute_returns_nonnull extern void *palloc0(Size size);
-extern void *palloc_extended(Size size, int flags);
-extern void *palloc_aligned(Size size, Size alignto, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc(Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0(Size size);
+pg_attribute_malloc extern void *palloc_extended(Size size, int flags);
+pg_attribute_malloc extern void *palloc_aligned(Size size, Size alignto, int flags);
 pg_nodiscard pg_attribute_returns_nonnull extern void *repalloc(void *pointer, Size size);
 pg_nodiscard extern void *repalloc_extended(void *pointer,
 											Size size, int flags);
@@ -90,9 +90,9 @@ extern void pfree(void *pointer);
  */
 extern Size add_size(Size s1, Size s2);
 extern Size mul_size(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
-pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
-extern void *palloc_mul_extended(Size s1, Size s2, int flags);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc_mul(Size s1, Size s2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *palloc0_mul(Size s1, Size s2);
+pg_attribute_malloc extern void *palloc_mul_extended(Size s1, Size s2, int flags);
 pg_nodiscard pg_attribute_returns_nonnull extern void *repalloc_mul(void *p, Size s1, Size s2);
 pg_nodiscard extern void *repalloc_mul_extended(void *p, Size s1, Size s2,
 												int flags);
@@ -123,7 +123,7 @@ pg_nodiscard extern void *repalloc_mul_extended(void *p, Size s1, Size s2,
 #define repalloc_array_extended(pointer, type, count, flags) ((type *) repalloc_mul_extended(pointer, sizeof(type), count, flags))
 
 /* Higher-limit allocators. */
-pg_attribute_returns_nonnull extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
+pg_attribute_malloc pg_attribute_returns_nonnull extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
 pg_nodiscard pg_attribute_returns_nonnull extern void *repalloc_huge(void *pointer, Size size);
 
 /*
@@ -154,14 +154,14 @@ extern void MemoryContextUnregisterResetCallback(MemoryContext context,
  * These are like standard strdup() except the copied string is
  * allocated in a context, not with malloc().
  */
-pg_attribute_returns_nonnull extern char *MemoryContextStrdup(MemoryContext context, const char *string);
-pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
-pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size len);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *MemoryContextStrdup(MemoryContext context, const char *string);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pstrdup(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pnstrdup(const char *in, Size len);
 
-pg_attribute_returns_nonnull extern char *pchomp(const char *in);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *pchomp(const char *in);
 
 /* sprintf into a palloc'd buffer --- these are in psprintf.c */
-pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
+pg_attribute_malloc pg_attribute_returns_nonnull extern char *psprintf(const char *fmt, ...) pg_attribute_printf(1, 2);
 extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
 
 #endif							/* PALLOC_H */
diff --git a/src/interfaces/ecpg/ecpglib/ecpglib_extern.h b/src/interfaces/ecpg/ecpglib/ecpglib_extern.h
index 0b8cea6196d..c54ec6407cb 100644
--- a/src/interfaces/ecpg/ecpglib/ecpglib_extern.h
+++ b/src/interfaces/ecpg/ecpglib/ecpglib_extern.h
@@ -168,14 +168,14 @@ bool		ecpg_get_data(const PGresult *, int, int, int, enum ECPGttype type,
 
 void		ecpg_pthreads_init(void);
 struct connection *ecpg_get_connection(const char *connection_name);
-char	   *ecpg_alloc(long size, int lineno);
-char	   *ecpg_auto_alloc(long size, int lineno);
+pg_attribute_malloc char *ecpg_alloc(long size, int lineno);
+pg_attribute_malloc char *ecpg_auto_alloc(long size, int lineno);
 char	   *ecpg_realloc(void *ptr, long size, int lineno);
 void		ecpg_free(void *ptr);
 bool		ecpg_init(const struct connection *con,
 					  const char *connection_name,
 					  const int lineno);
-char	   *ecpg_strdup(const char *string, int lineno, bool *alloc_failed);
+pg_attribute_malloc char *ecpg_strdup(const char *string, int lineno, bool *alloc_failed);
 const char *ecpg_type_name(enum ECPGttype typ);
 int			ecpg_dynamic_type(Oid type);
 int			sqlda_dynamic_type(Oid type, enum COMPAT_MODE compat);
diff --git a/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h b/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h
index ce2f14fab76..5e63c8a0b6e 100644
--- a/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h
+++ b/src/interfaces/ecpg/pgtypeslib/pgtypeslib_extern.h
@@ -39,7 +39,7 @@ int			pgtypes_fmt_replace(union un_fmt_comb replace_val,
 								int replace_type, char **output,
 								int *pstr_len);
 
-char	   *pgtypes_alloc(long size);
-char	   *pgtypes_strdup(const char *str);
+pg_attribute_malloc char *pgtypes_alloc(long size);
+pg_attribute_malloc char *pgtypes_strdup(const char *str);
 
 #endif							/* _ECPG_PGTYPESLIB_EXTERN_H */
diff --git a/src/interfaces/ecpg/preproc/preproc_extern.h b/src/interfaces/ecpg/preproc/preproc_extern.h
index c61f054873b..521cb0005ea 100644
--- a/src/interfaces/ecpg/preproc/preproc_extern.h
+++ b/src/interfaces/ecpg/preproc/preproc_extern.h
@@ -79,10 +79,10 @@ extern char *hashline_number(void);
 extern int	base_yyparse(void);
 extern int	base_yylex(void);
 extern void base_yyerror(const char *error);
-extern void *mm_alloc(size_t size);
-extern char *mm_strdup(const char *string);
-extern void *loc_alloc(size_t size);
-extern char *loc_strdup(const char *string);
+pg_attribute_malloc extern void *mm_alloc(size_t size);
+pg_attribute_malloc extern char *mm_strdup(const char *string);
+pg_attribute_malloc extern void *loc_alloc(size_t size);
+pg_attribute_malloc extern char *loc_strdup(const char *string);
 extern void reclaim_local_storage(void);
 extern char *cat2_str(const char *str1, const char *str2);
 extern char *cat_str(int count, ...);
-- 
Tristan Partin
https://tristan.partin.io

