On 16.11.26 18:49, Tristan Partin wrote:
- TRGM in contrib/pg_trgm/ is an instance of what [0] talks about: It
has a trailing flexible array member, but it also has trailing padding,
so allocating palloc(TRGMHDRSIZE) causes a warning, because it's less
than sizeof(TRGM).  In my patch I work around this by allocating like
palloc(Max(TRGMHDRSIZE, sizeof(TRGM))).

Maybe we should add an allocator function or macro for this? In the
counted_by discussion, I talked a bit about how core does a pretty good
job of providing allocator helpers for structs with flexible array
members. I realize this is contrib/, but it might still be valuable.

- There is a bit of a maze of different structs and unions that underlie
the numeric type.  The allocation (Numeric) palloc(NUMERIC_HDRSZ_SHORT)
gets flagged by the compiler.  In my patch I change this to (Numeric)
palloc(sizeof(struct NumericData)), which allocates a bit more but makes
the sizes and types match.  Maybe some more judicious reshuffling of the
underlying data structures could avoid this, or maybe it's okay like this.

Like the previous comment, I wonder if some helper allocator
functions/macros could be useful. But as you said, maybe some
reshuffling could avoid that all together, not sure.

In the attached patch series, I have an idea for a more generalized approach: A macro that computes the required size and then a wrapper macro around it for allocating. This then can be applied to any structure with a flexible array member, and it eliminates some duplication with the offsetof calls and gets you the kind of type safety that palloc_object and palloc_array provide.

I have also added a patch that makes some more uses of this macro. These are the same kinds of problems as the one I showed for pg_trgm and numeric, except that the size isn't constant and so the compiler won't flag it.

- As of commit 6a3da13d376 from this week, FormData_pg_attribute has 4
bytes of trailing padding, and so sizeof(FormData_pg_attribute) is now
different from ATTRIBUTE_FIXED_PART_SIZE.  This causes a warning in one
place.  We had a discussion about this some time ago [1], where the
opinions diverged between like, it's important to keep this distinction
in case we ever have padding bytes, and, it's already incorrect and
would be even more incorrect if we had padding bytes. ;-)  Well, we're
here now, so perhaps this should be revisited.

I have adjusted the description of this patch a bit. There are a lot of details to be figured out in this area, but at least we can fix this particular issue easily and in a consistent way. tupdesc.c already uses sizeof(FormData_pg_attribute) consistently for allocating pg_attribute structures. It only uses ATTRIBUTE_FIXED_PART_SIZE for things like memcpy(). With the proposed patch, we make bootstrap.c consistent in this regard, and we make the warning go away. The deeper issues can then be researched independent of this.

In [1], Tom said:

What I'm trying to figure out is if we need to put in place some
provisions to prevent there from being pad bytes at the end of any
catalog struct.

I figured I could propose at least one way to flag padding bytes in the
attached patch. I am currently not proposing a solution.

Two options proposed in the thread[1]:

- Placeholder fields exactly matching the size of the padding bytes.

        #ifdef CATALOG_VARLEN                   /* variable-length fields start 
here */
                pg_node_tree ev_qual;
                pg_node_tree ev_action;
        #else
                uint8       dummy;
        #endif

- Use different structs for on-media and in-memory representations.

To not rock the boat too much, maybe I would throw my hat in the ring
for option 1? It seems like the simplest and most immediate solution.

I think we could get the same check simpler and more portably by having genbki.pl generate some static assertions that check like

static_assert(sizeof(FormData_pg_attribute) == offsetof(FormData_pg_attribute, attcollation) + sizeof(FormData_pg_attribute.attcollation));

But this flags a bunch of issues.  Sorting that out might be a big project.
From 1f528c1d4b7e61486f4dfb7dd0af5e67d67cc8fd Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 13 Nov 2026 11:58:18 +0100
Subject: [PATCH v2 1/7] Apply alloc_size attribute to memory allocation
 functions

Add pg_attribute_alloc_size() and use it on palloc, pg_malloc, and
related allocation functions.  This tells the compiler the size of the
returned object, which enables __builtin_object_size() and
__builtin_dynamic_object_size() on such memory (and thus
_FORTIFY_SOURCE and similar checks) as well as compile-time bounds
warnings such as -Warray-bounds and -Wstringop-overflow.

For now we only adjust the general-purpose allocation functions in
palloc.h and fe_memutils.h.  There are a number of other more
narrow-purpose allocation functions that could be similarly annotated.

To avoid some new warnings, add pg_assume() at a few places where an
int count is passed to palloc_array() and the compiler cannot tell
that it is not negative.

Discussion: 
https://www.postgresql.org/message-id/flat/cadec43a-d031-41cb-b887-3b4a29720017%40eisentraut.org
---
 src/backend/commands/prepare.c      |  1 +
 src/backend/statistics/mcv.c        |  1 +
 src/backend/storage/buffer/bufmgr.c |  2 ++
 src/include/c.h                     | 13 ++++++++
 src/include/common/fe_memutils.h    | 32 ++++++++++----------
 src/include/utils/palloc.h          | 46 ++++++++++++++---------------
 6 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/src/backend/commands/prepare.c b/src/backend/commands/prepare.c
index 876aad2100a..8f54559ca8a 100644
--- a/src/backend/commands/prepare.c
+++ b/src/backend/commands/prepare.c
@@ -752,6 +752,7 @@ build_regtype_array(Oid *param_types, int num_params)
        ArrayType  *result;
        int                     i;
 
+       pg_assume(num_params >= 0);
        tmp_ary = palloc_array(Datum, num_params);
 
        for (i = 0; i < num_params; i++)
diff --git a/src/backend/statistics/mcv.c b/src/backend/statistics/mcv.c
index 10a91c97aeb..6b27b45029d 100644
--- a/src/backend/statistics/mcv.c
+++ b/src/backend/statistics/mcv.c
@@ -2283,6 +2283,7 @@ statext_mcv_import(int elevel, int numattrs,
         * VacAttrStats records, but only a few fields within those records have
         * to be filled out.
         */
+       pg_assume(numattrs >= 0);
        vastats = (VacAttrStats **) palloc0_array(VacAttrStats *, numattrs);
 
        for (int i = 0; i < numattrs; i++)
diff --git a/src/backend/storage/buffer/bufmgr.c 
b/src/backend/storage/buffer/bufmgr.c
index 4dbc25c6c50..bc0b5d706f9 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -4941,6 +4941,8 @@ DropRelationsAllBuffers(SMgrRelation *smgr_reln, int 
nlocators)
                        rels[n++] = smgr_reln[i];
        }
 
+       pg_assume(n >= 0);
+
        /*
         * If there are no non-local relations, then we're done. Release the
         * memory and return.
diff --git a/src/include/c.h b/src/include/c.h
index 4bbb0071289..0b9afcd7d3c 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -293,6 +293,19 @@ extern "C++"
 #define pg_attribute_no_sanitize_function()
 #endif
 
+/*
+ * pg_attribute_alloc_size means the function returns a pointer to an object
+ * whose size is given by the listed argument, or by the product of the two
+ * listed arguments.  This helps the compiler check object bounds.
+ *
+ * 
https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-alloc_005fsize
+ */
+#if __has_attribute (alloc_size)
+#define pg_attribute_alloc_size(...) __attribute__((alloc_size(__VA_ARGS__)))
+#else
+#define pg_attribute_alloc_size(...)
+#endif
+
 /*
  * pg_attribute_nonnull means the compiler should warn if the function is
  * called with the listed arguments set to NULL.  If no arguments are
diff --git a/src/include/common/fe_memutils.h b/src/include/common/fe_memutils.h
index 0db64ab4389..e701e38bec5 100644
--- a/src/include/common/fe_memutils.h
+++ b/src/include/common/fe_memutils.h
@@ -35,10 +35,10 @@
  * (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_returns_nonnull extern void *pg_realloc(void *ptr, size_t size);
+pg_attribute_alloc_size(1) pg_attribute_returns_nonnull extern void 
*pg_malloc(size_t size);
+pg_attribute_alloc_size(1) pg_attribute_returns_nonnull extern void 
*pg_malloc0(size_t size);
+pg_attribute_alloc_size(1) extern void *pg_malloc_extended(size_t size, int 
flags);
+pg_attribute_alloc_size(2) pg_attribute_returns_nonnull extern void 
*pg_realloc(void *ptr, size_t size);
 extern void pg_free(void *ptr);
 
 /*
@@ -46,10 +46,10 @@ 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_returns_nonnull extern void *pg_realloc_mul(void *p, Size s1, 
Size s2);
+pg_attribute_alloc_size(1, 2) pg_attribute_returns_nonnull extern void 
*pg_malloc_mul(Size s1, Size s2);
+pg_attribute_alloc_size(1, 2) pg_attribute_returns_nonnull extern void 
*pg_malloc0_mul(Size s1, Size s2);
+pg_attribute_alloc_size(1, 2) extern void *pg_malloc_mul_extended(Size s1, 
Size s2, int flags);
+pg_attribute_alloc_size(2, 3) pg_attribute_returns_nonnull extern void 
*pg_realloc_mul(void *p, Size s1, Size s2);
 
 /*
  * Variants with easier notation and more type safety
@@ -77,15 +77,15 @@ pg_attribute_returns_nonnull extern void 
*pg_realloc_mul(void *p, Size s1, Size
 /* 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_returns_nonnull extern void *repalloc(void *pointer, Size size);
+pg_attribute_alloc_size(1) pg_attribute_returns_nonnull extern void 
*palloc(Size size);
+pg_attribute_alloc_size(1) pg_attribute_returns_nonnull extern void 
*palloc0(Size size);
+pg_attribute_alloc_size(1) extern void *palloc_extended(Size size, int flags);
+pg_attribute_alloc_size(2) 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_returns_nonnull extern void *repalloc_mul(void *p, Size s1, Size 
s2);
+pg_attribute_alloc_size(1, 2) pg_attribute_returns_nonnull extern void 
*palloc_mul(Size s1, Size s2);
+pg_attribute_alloc_size(1, 2) pg_attribute_returns_nonnull extern void 
*palloc0_mul(Size s1, Size s2);
+pg_attribute_alloc_size(1, 2) extern void *palloc_mul_extended(Size s1, Size 
s2, int flags);
+pg_attribute_alloc_size(2, 3) pg_attribute_returns_nonnull extern void 
*repalloc_mul(void *p, Size s1, Size s2);
 
 #define palloc_object(type) ((type *) palloc(sizeof(type)))
 #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h
index 0c7452fe30d..e8a8eefd9c4 100644
--- a/src/include/utils/palloc.h
+++ b/src/include/utils/palloc.h
@@ -68,21 +68,21 @@ 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_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_nodiscard pg_attribute_returns_nonnull extern void *repalloc(void *pointer, 
Size size);
-pg_nodiscard extern void *repalloc_extended(void *pointer,
-                                                                               
        Size size, int flags);
-pg_nodiscard pg_attribute_returns_nonnull extern void *repalloc0(void 
*pointer, Size oldsize, Size size);
+pg_attribute_alloc_size(2) pg_attribute_returns_nonnull extern void 
*MemoryContextAlloc(MemoryContext context, Size size);
+pg_attribute_alloc_size(2) pg_attribute_returns_nonnull extern void 
*MemoryContextAllocZero(MemoryContext context, Size size);
+pg_attribute_alloc_size(2) extern void 
*MemoryContextAllocExtended(MemoryContext context,
+                                                                               
                                                   Size size, int flags);
+pg_attribute_alloc_size(2) extern void 
*MemoryContextAllocAligned(MemoryContext context,
+                                                                               
                                                  Size size, Size alignto, int 
flags);
+
+pg_attribute_alloc_size(1) pg_attribute_returns_nonnull extern void 
*palloc(Size size);
+pg_attribute_alloc_size(1) pg_attribute_returns_nonnull extern void 
*palloc0(Size size);
+pg_attribute_alloc_size(1) extern void *palloc_extended(Size size, int flags);
+pg_attribute_alloc_size(1) extern void *palloc_aligned(Size size, Size 
alignto, int flags);
+pg_nodiscard pg_attribute_alloc_size(2) pg_attribute_returns_nonnull extern 
void *repalloc(void *pointer, Size size);
+pg_nodiscard pg_attribute_alloc_size(2) extern void *repalloc_extended(void 
*pointer,
+                                                                               
                                                           Size size, int 
flags);
+pg_nodiscard pg_attribute_alloc_size(3) pg_attribute_returns_nonnull extern 
void *repalloc0(void *pointer, Size oldsize, Size size);
 extern void pfree(void *pointer);
 
 /*
@@ -90,12 +90,12 @@ 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_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);
+pg_attribute_alloc_size(1, 2) pg_attribute_returns_nonnull extern void 
*palloc_mul(Size s1, Size s2);
+pg_attribute_alloc_size(1, 2) pg_attribute_returns_nonnull extern void 
*palloc0_mul(Size s1, Size s2);
+pg_attribute_alloc_size(1, 2) extern void *palloc_mul_extended(Size s1, Size 
s2, int flags);
+pg_nodiscard pg_attribute_alloc_size(2, 3) pg_attribute_returns_nonnull extern 
void *repalloc_mul(void *p, Size s1, Size s2);
+pg_nodiscard pg_attribute_alloc_size(2, 3) extern void 
*repalloc_mul_extended(void *p, Size s1, Size s2,
+                                                                               
                                                                          int 
flags);
 
 /*
  * Variants with easier notation and more type safety
@@ -123,8 +123,8 @@ 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_nodiscard pg_attribute_returns_nonnull extern void *repalloc_huge(void 
*pointer, Size size);
+pg_attribute_alloc_size(2) pg_attribute_returns_nonnull extern void 
*MemoryContextAllocHuge(MemoryContext context, Size size);
+pg_nodiscard pg_attribute_alloc_size(2) pg_attribute_returns_nonnull extern 
void *repalloc_huge(void *pointer, Size size);
 
 /*
  * Although this header file is nominally backend-only, certain frontend
-- 
2.56.0

From 5ae1ab66300d378b24126badf8d4bbedaa2ccf01 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 13 Nov 2026 11:58:18 +0100
Subject: [PATCH v2 2/7] Fixes for alloc_size attribute [varatt]

varattrib_4b was a union of a normal varlena with a 4-byte header and
0 or more payload bytes, and a compressed format with a 4-byte header,
a 4-byte "tcinfo", followed by payload data.  The typical coding
pattern

    val = palloc(VARHDRSZ + len);
    SET_VARSIZE(val, VARHDRSZ + len);

could get flagged by gcc -Warray-bounds because the allocation could
be too short to cover both branches of the union, which is invalid C.

We don't actually need the union; no code makes real use of it.
Instead, create two separate structs varattrib_4b_normal and
varattrib_4b_compressed and make use of the appropriate one of these
in each place varattrib_4b was used.  This avoids the warnings from
SET_VARSIZE() and SET_VARSIZE_COMPRESSED().

Discussion: 
https://www.postgresql.org/message-id/flat/cadec43a-d031-41cb-b887-3b4a29720017%40eisentraut.org
---
 contrib/amcheck/verify_heapam.c   |  2 +-
 src/include/utils/expandeddatum.h |  2 +-
 src/include/varatt.h              | 55 +++++++++++++++++--------------
 src/tools/pgindent/typedefs.list  |  3 +-
 4 files changed, 34 insertions(+), 28 deletions(-)

diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c
index ea1ad402ea2..86219c6ca68 100644
--- a/contrib/amcheck/verify_heapam.c
+++ b/contrib/amcheck/verify_heapam.c
@@ -1637,7 +1637,7 @@ check_toast_tuple(HeapTuple toasttup, HeapCheckContext 
*ctx,
        else
        {
                /* should never happen */
-               uint32          header = ((varattrib_4b *) 
chunk)->va_4byte.va_header;
+               uint32          header = ((varattrib_4b_normal *) 
chunk)->va_header;
 
                report_toast_corruption(ctx, ta,
                                                                psprintf("toast 
value %" PRIu64 " chunk %d has invalid varlena header %0x",
diff --git a/src/include/utils/expandeddatum.h 
b/src/include/utils/expandeddatum.h
index 7c2d5bbc46c..f2b151dfefe 100644
--- a/src/include/utils/expandeddatum.h
+++ b/src/include/utils/expandeddatum.h
@@ -128,7 +128,7 @@ struct ExpandedObjectHeader
  */
 #define EOH_HEADER_MAGIC (-1)
 #define VARATT_IS_EXPANDED_HEADER(PTR) \
-       (((varattrib_4b *) (PTR))->va_4byte.va_header == (uint32) 
EOH_HEADER_MAGIC)
+       (((varattrib_4b_normal *) (PTR))->va_header == (uint32) 
EOH_HEADER_MAGIC)
 
 /*
  * Generic support functions for expanded objects.
diff --git a/src/include/varatt.h b/src/include/varatt.h
index c22575a3624..c73d1f94c91 100644
--- a/src/include/varatt.h
+++ b/src/include/varatt.h
@@ -170,21 +170,26 @@ VARTAG_SIZE(vartag_external tag)
  * compiler might otherwise think it could generate code that assumes
  * alignment while touching fields of a 1-byte-header varlena.
  */
-typedef union
+
+/*
+ * Normal varlena (4-byte length)
+ */
+typedef struct
 {
-       struct                                          /* Normal varlena 
(4-byte length) */
-       {
-               uint32          va_header;
-               char            va_data[FLEXIBLE_ARRAY_MEMBER];
-       }                       va_4byte;
-       struct                                          /* Compressed-in-line 
format */
-       {
-               uint32          va_header;
-               uint32          va_tcinfo;      /* Original data size (excludes 
header) and
+       uint32          va_header;
+       char            va_data[FLEXIBLE_ARRAY_MEMBER];
+} varattrib_4b_normal;
+
+/*
+ * Compressed-in-line format
+ */
+typedef struct
+{
+       uint32          va_header;
+       uint32          va_tcinfo;              /* Original data size (excludes 
header) and
                                                                 * compression 
method; see va_extinfo */
-               char            va_data[FLEXIBLE_ARRAY_MEMBER]; /* Compressed 
data */
-       }                       va_compressed;
-} varattrib_4b;
+       char            va_data[FLEXIBLE_ARRAY_MEMBER]; /* Compressed data */
+} varattrib_4b_compressed;
 
 typedef struct
 {
@@ -254,16 +259,16 @@ typedef struct
 
 /* VARSIZE_4B() should only be used on known-aligned data */
 #define VARSIZE_4B(PTR) \
-       (((const varattrib_4b *) (PTR))->va_4byte.va_header & 0x3FFFFFFF)
+       (((const varattrib_4b_normal *) (PTR))->va_header & 0x3FFFFFFF)
 #define VARSIZE_1B(PTR) \
        (((const varattrib_1b *) (PTR))->va_header & 0x7F)
 #define VARTAG_1B_E(PTR) \
        ((vartag_external) ((const varattrib_1b_e *) (PTR))->va_tag)
 
 #define SET_VARSIZE_4B(PTR,len) \
-       (((varattrib_4b *) (PTR))->va_4byte.va_header = (len) & 0x3FFFFFFF)
+       (((varattrib_4b_normal *) (PTR))->va_header = (len) & 0x3FFFFFFF)
 #define SET_VARSIZE_4B_C(PTR,len) \
-       (((varattrib_4b *) (PTR))->va_4byte.va_header = ((len) & 0x3FFFFFFF) | 
0x40000000)
+       (((varattrib_4b_normal *) (PTR))->va_header = ((len) & 0x3FFFFFFF) | 
0x40000000)
 #define SET_VARSIZE_1B(PTR,len) \
        (((varattrib_1b *) (PTR))->va_header = (len) | 0x80)
 #define SET_VARTAG_1B_E(PTR,tag) \
@@ -287,16 +292,16 @@ typedef struct
 
 /* VARSIZE_4B() should only be used on known-aligned data */
 #define VARSIZE_4B(PTR) \
-       ((((const varattrib_4b *) (PTR))->va_4byte.va_header >> 2) & 0x3FFFFFFF)
+       ((((const varattrib_4b_normal *) (PTR))->va_header >> 2) & 0x3FFFFFFF)
 #define VARSIZE_1B(PTR) \
        ((((const varattrib_1b *) (PTR))->va_header >> 1) & 0x7F)
 #define VARTAG_1B_E(PTR) \
        ((vartag_external) ((const varattrib_1b_e *) (PTR))->va_tag)
 
 #define SET_VARSIZE_4B(PTR,len) \
-       (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2))
+       (((varattrib_4b_normal *) (PTR))->va_header = (((uint32) (len)) << 2))
 #define SET_VARSIZE_4B_C(PTR,len) \
-       (((varattrib_4b *) (PTR))->va_4byte.va_header = (((uint32) (len)) << 2) 
| 0x02)
+       (((varattrib_4b_normal *) (PTR))->va_header = (((uint32) (len)) << 2) | 
0x02)
 #define SET_VARSIZE_1B(PTR,len) \
        (((varattrib_1b *) (PTR))->va_header = (((uint8) (len)) << 1) | 0x01)
 #define SET_VARTAG_1B_E(PTR,tag) \
@@ -305,8 +310,8 @@ typedef struct
 
 #endif                                                 /* WORDS_BIGENDIAN */
 
-#define VARDATA_4B(PTR)                (((varattrib_4b *) 
(PTR))->va_4byte.va_data)
-#define VARDATA_4B_C(PTR)      (((varattrib_4b *) 
(PTR))->va_compressed.va_data)
+#define VARDATA_4B(PTR)                (((varattrib_4b_normal *) 
(PTR))->va_data)
+#define VARDATA_4B_C(PTR)      (((varattrib_4b_compressed *) (PTR))->va_data)
 #define VARDATA_1B(PTR)                (((varattrib_1b *) (PTR))->va_data)
 #define VARDATA_1B_E(PTR)      (((varattrib_1b_e *) (PTR))->va_data)
 
@@ -321,7 +326,7 @@ typedef struct
  */
 
 #define VARHDRSZ_EXTERNAL              offsetof(varattrib_1b_e, va_data)
-#define VARHDRSZ_COMPRESSED            offsetof(varattrib_4b, 
va_compressed.va_data)
+#define VARHDRSZ_COMPRESSED            offsetof(varattrib_4b_compressed, 
va_data)
 #define VARHDRSZ_SHORT                 offsetof(varattrib_1b, va_data)
 #define VARATT_SHORT_MAX               0x7F
 
@@ -544,14 +549,14 @@ VARDATA_ANY(const void *PTR)
 static inline Size
 VARDATA_COMPRESSED_GET_EXTSIZE(const void *PTR)
 {
-       return ((const varattrib_4b *) PTR)->va_compressed.va_tcinfo & 
VARLENA_EXTSIZE_MASK;
+       return ((const varattrib_4b_compressed *) PTR)->va_tcinfo & 
VARLENA_EXTSIZE_MASK;
 }
 
 /* Compression method of a compressed-in-line varlena datum */
 static inline uint32
 VARDATA_COMPRESSED_GET_COMPRESS_METHOD(const void *PTR)
 {
-       return ((const varattrib_4b *) PTR)->va_compressed.va_tcinfo >> 
VARLENA_EXTSIZE_BITS;
+       return ((const varattrib_4b_compressed *) PTR)->va_tcinfo >> 
VARLENA_EXTSIZE_BITS;
 }
 
 /*
@@ -565,7 +570,7 @@ VARDATA_COMPRESSED_SET_TCINFO(void *PTR, uint32 extsize, 
uint32 cmethod)
        Assert(extsize > 0 && extsize <= VARLENA_EXTSIZE_MASK);
        Assert(cmethod < (1U << (32 - VARLENA_EXTSIZE_BITS)));
 
-       ((varattrib_4b *) PTR)->va_compressed.va_tcinfo =
+       ((varattrib_4b_compressed *) PTR)->va_tcinfo =
                extsize | (cmethod << VARLENA_EXTSIZE_BITS);
 }
 
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index f10eb469266..b4a06eea230 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -4423,7 +4423,8 @@ varatt_external_oid8
 varatt_indirect
 varattrib_1b
 varattrib_1b_e
-varattrib_4b
+varattrib_4b_compressed
+varattrib_4b_normal
 varlena
 vartag_external
 vbits
-- 
2.56.0

From b8c649bedee95ee5fb086845da5852124f814379 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 13 Nov 2026 11:58:18 +0100
Subject: [PATCH v2 3/7] Fixes for alloc_size attribute [pg_trgm]

Header-only pg_trgm values are now allocated with at least the size of
the struct, to avoid -Warray-bounds warnings.

Discussion: 
https://www.postgresql.org/message-id/flat/cadec43a-d031-41cb-b887-3b4a29720017%40eisentraut.org
---
 contrib/pg_trgm/trgm.h      | 5 +++++
 contrib/pg_trgm/trgm_gist.c | 5 ++++-
 contrib/pg_trgm/trgm_op.c   | 3 ++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/contrib/pg_trgm/trgm.h b/contrib/pg_trgm/trgm.h
index ca23aad4dd9..7728aea84d0 100644
--- a/contrib/pg_trgm/trgm.h
+++ b/contrib/pg_trgm/trgm.h
@@ -55,6 +55,11 @@ extern int   (*CMPTRGM) (const void *a, const void *b);
 #define ISWILDCARDCHAR(x) (*(x) == '_' || *(x) == '%') /* Wildcard
                                                                                
                                 * meta-character */
 
+/*
+ * Note: This structure has trailing padding. When allocating a header-only
+ * value, at least sizeof(TRGM) must be allocated, to avoid -Warray-bounds
+ * compiler warnings.
+ */
 typedef struct
 {
        int32           vl_len_;                /* varlena header (do not touch 
directly!) */
diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c
index dfa2f968d77..85c7da37125 100644
--- a/contrib/pg_trgm/trgm_gist.c
+++ b/contrib/pg_trgm/trgm_gist.c
@@ -78,7 +78,10 @@ gtrgm_alloc(bool isalltrue, int siglen, BITVECP sign)
 {
        int                     flag = SIGNKEY | (isalltrue ? ALLISTRUE : 0);
        int                     size = CALCGTSIZE(flag, siglen);
-       TRGM       *res = palloc(size);
+       TRGM       *res;
+
+       /* allocate at least sizeof(TRGM), to cover padding */
+       res = palloc(Max(size, sizeof(TRGM)));
 
        SET_VARSIZE(res, size);
        res->flag = flag;
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 22bcc3c3361..2655257c2ad 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -1097,7 +1097,8 @@ generate_wildcard_trgm(const char *str, int slen)
 
        if (slen + LPADDING + RPADDING < 3 || slen == 0)
        {
-               trg = (TRGM *) palloc(TRGMHDRSIZE);
+               /* allocate at least sizeof(TRGM), to cover padding */
+               trg = (TRGM *) palloc(Max(TRGMHDRSIZE, sizeof(TRGM)));
                trg->flag = ARRKEY;
                SET_VARSIZE(trg, TRGMHDRSIZE);
                return trg;
-- 
2.56.0

From 414c60ff16cd9d84028ce4df4580825fb59381e0 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 13 Nov 2026 11:58:18 +0100
Subject: [PATCH v2 4/7] Fixes for alloc_size attribute [numeric]

"Short" Numeric values are now allocated with at least the size of the
struct, so that accessing fields through the struct stays within the
allocation.

Discussion: 
https://www.postgresql.org/message-id/flat/cadec43a-d031-41cb-b887-3b4a29720017%40eisentraut.org
---
 src/backend/utils/adt/numeric.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index ff6dec42010..18a7877f194 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -7636,7 +7636,11 @@ make_result_safe(const NumericVar *var, Node *escontext)
                          sign == NUMERIC_NINF))
                        elog(ERROR, "invalid numeric sign value 0x%x", sign);
 
-               result = (Numeric) palloc(NUMERIC_HDRSZ_SHORT);
+               /*
+                * We must allocate at least the full struct size, even though 
the
+                * datum only uses NUMERIC_HDRSZ_SHORT bytes.
+                */
+               result = (Numeric) palloc(sizeof(struct NumericData));
 
                SET_VARSIZE(result, NUMERIC_HDRSZ_SHORT);
                result->choice.n_header = sign;
-- 
2.56.0

From dca7c05924a3bdda1173428a4bded22b09a8f6fa Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 20 Nov 2026 14:59:58 +0100
Subject: [PATCH v2 5/7] Fixes for alloc_size attribute [bootstrap]

As of commit 6a3da13d376, FormData_pg_attribute has 4 bytes of
trailing padding, and so sizeof(FormData_pg_attribute) is different
from ATTRIBUTE_FIXED_PART_SIZE.  The compiler flags it in this one
place.  Changing to sizeof(FormData_pg_attribute) also makes this code
consistent with tupdesc.c, which also allocates the
FormData_pg_attribute portions of the tuple descriptor that
way.  (ATTRIBUTE_FIXED_PART_SIZE is otherwise only used for
memcpy/memmove/memset, not for sizing allocations.)

Discussion: 
https://www.postgresql.org/message-id/flat/cadec43a-d031-41cb-b887-3b4a29720017%40eisentraut.org
---
 src/backend/bootstrap/bootstrap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/backend/bootstrap/bootstrap.c 
b/src/backend/bootstrap/bootstrap.c
index 00786d8c0fa..63cfc25bda0 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -1106,15 +1106,15 @@ boot_get_role_oid(const char *rolname)
 /* ----------------
  *             AllocateAttribute
  *
- * Note: bootstrap never sets any per-column ACLs, so we only need
- * ATTRIBUTE_FIXED_PART_SIZE space per attribute.
+ * Note: bootstrap never sets any variable-length fields in pg_attribute, so
+ * we only need to allocate the fixed portion.
  * ----------------
  */
 static Form_pg_attribute
 AllocateAttribute(void)
 {
        return (Form_pg_attribute)
-               MemoryContextAllocZero(TopMemoryContext, 
ATTRIBUTE_FIXED_PART_SIZE);
+               MemoryContextAllocZero(TopMemoryContext, 
sizeof(FormData_pg_attribute));
 }
 
 /*
-- 
2.56.0

From 40c426c7094fcfebe60e4f4ac22c7fd1998fd210 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 20 Nov 2026 15:25:10 +0100
Subject: [PATCH v2 6/7] WIP: flex_size

FIXME: consider multiplication overflow checks
---
 contrib/pg_trgm/trgm.h           |  5 ++---
 contrib/pg_trgm/trgm_gist.c      |  3 +--
 contrib/pg_trgm/trgm_op.c        |  3 +--
 src/backend/utils/adt/numeric.c  |  6 +-----
 src/include/c.h                  | 18 ++++++++++++++++++
 src/include/common/fe_memutils.h |  9 +++++++++
 src/include/utils/palloc.h       |  7 +++++++
 7 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/contrib/pg_trgm/trgm.h b/contrib/pg_trgm/trgm.h
index 7728aea84d0..48c954f5b81 100644
--- a/contrib/pg_trgm/trgm.h
+++ b/contrib/pg_trgm/trgm.h
@@ -56,9 +56,8 @@ extern int    (*CMPTRGM) (const void *a, const void *b);
                                                                                
                                 * meta-character */
 
 /*
- * Note: This structure has trailing padding. When allocating a header-only
- * value, at least sizeof(TRGM) must be allocated, to avoid -Warray-bounds
- * compiler warnings.
+ * Note: This structure has trailing padding, so palloc_flex_array() should
+ * be used.
  */
 typedef struct
 {
diff --git a/contrib/pg_trgm/trgm_gist.c b/contrib/pg_trgm/trgm_gist.c
index 85c7da37125..68075010e8f 100644
--- a/contrib/pg_trgm/trgm_gist.c
+++ b/contrib/pg_trgm/trgm_gist.c
@@ -80,8 +80,7 @@ gtrgm_alloc(bool isalltrue, int siglen, BITVECP sign)
        int                     size = CALCGTSIZE(flag, siglen);
        TRGM       *res;
 
-       /* allocate at least sizeof(TRGM), to cover padding */
-       res = palloc(Max(size, sizeof(TRGM)));
+       res = palloc_flex_array(TRGM, data, size - TRGMHDRSIZE);
 
        SET_VARSIZE(res, size);
        res->flag = flag;
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 2655257c2ad..01699dc1d46 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -1097,8 +1097,7 @@ generate_wildcard_trgm(const char *str, int slen)
 
        if (slen + LPADDING + RPADDING < 3 || slen == 0)
        {
-               /* allocate at least sizeof(TRGM), to cover padding */
-               trg = (TRGM *) palloc(Max(TRGMHDRSIZE, sizeof(TRGM)));
+               trg = palloc_flex_array(TRGM, data, 0);
                trg->flag = ARRKEY;
                SET_VARSIZE(trg, TRGMHDRSIZE);
                return trg;
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 18a7877f194..5f8583a7b91 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -7636,11 +7636,7 @@ make_result_safe(const NumericVar *var, Node *escontext)
                          sign == NUMERIC_NINF))
                        elog(ERROR, "invalid numeric sign value 0x%x", sign);
 
-               /*
-                * We must allocate at least the full struct size, even though 
the
-                * datum only uses NUMERIC_HDRSZ_SHORT bytes.
-                */
-               result = (Numeric) palloc(sizeof(struct NumericData));
+               result = palloc_flex_array(struct NumericData, 
choice.n_short.n_data, 0);
 
                SET_VARSIZE(result, NUMERIC_HDRSZ_SHORT);
                result->choice.n_header = sign;
diff --git a/src/include/c.h b/src/include/c.h
index 0b9afcd7d3c..5f66e9e3b1d 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -1226,6 +1226,24 @@ pg_noreturn extern void ExceptionalCondition(const char 
*conditionName,
  */
 #define Min(x, y)              ((x) < (y) ? (x) : (y))
 
+/*
+ * flex_size
+ *             Return the size of a struct "type" whose flexible array member
+ *             "member" holds "count" elements.
+ *
+ * The result is never less than sizeof(type), so that any trailing padding
+ * of the struct is covered.  "count" is evaluated only once.
+ */
+#define flex_size(type, member, count) \
+       flex_size_impl(sizeof(type), offsetof(type, member), \
+                                  sizeof(((type *) 0)->member[0]), (count))
+
+static inline size_t
+flex_size_impl(size_t structsz, size_t offset, size_t elemsz, size_t count)
+{
+       return Max(structsz, offset + count * elemsz);
+}
+
 
 /* Get a bit mask of the bits set in non-size_t aligned addresses */
 #define SIZE_T_ALIGN_MASK (sizeof(size_t) - 1)
diff --git a/src/include/common/fe_memutils.h b/src/include/common/fe_memutils.h
index e701e38bec5..5e45a44ba4c 100644
--- a/src/include/common/fe_memutils.h
+++ b/src/include/common/fe_memutils.h
@@ -61,6 +61,13 @@ pg_attribute_alloc_size(2, 3) pg_attribute_returns_nonnull 
extern void *pg_reall
 #define pg_malloc_object(type) ((type *) pg_malloc(sizeof(type)))
 #define pg_malloc0_object(type) ((type *) pg_malloc0(sizeof(type)))
 
+/*
+ * Allocate space for a struct of type "type" whose flexible array member
+ * "member" holds "count" elements.  See flex_size() for details.
+ */
+#define pg_malloc_flex_array(type, member, count) ((type *) 
pg_malloc(flex_size(type, member, count)))
+#define pg_malloc0_flex_array(type, member, count) ((type *) 
pg_malloc0(flex_size(type, member, count)))
+
 /*
  * Allocate space for "count" objects of type "type"
  */
@@ -91,6 +98,8 @@ pg_attribute_alloc_size(2, 3) pg_attribute_returns_nonnull 
extern void *repalloc
 #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
 #define palloc_array(type, count) ((type *) palloc_mul(sizeof(type), count))
 #define palloc0_array(type, count) ((type *) palloc0_mul(sizeof(type), count))
+#define palloc_flex_array(type, member, count) ((type *) 
palloc(flex_size(type, member, count)))
+#define palloc0_flex_array(type, member, count) ((type *) 
palloc0(flex_size(type, member, count)))
 #define palloc_array_extended(type, count, flags) ((type *) 
palloc_mul_extended(sizeof(type), count, flags))
 #define repalloc_array(pointer, type, count) ((type *) repalloc_mul(pointer, 
sizeof(type), count))
 
diff --git a/src/include/utils/palloc.h b/src/include/utils/palloc.h
index e8a8eefd9c4..265a3f1a727 100644
--- a/src/include/utils/palloc.h
+++ b/src/include/utils/palloc.h
@@ -107,6 +107,13 @@ pg_nodiscard pg_attribute_alloc_size(2, 3) extern void 
*repalloc_mul_extended(vo
 #define palloc_object(type) ((type *) palloc(sizeof(type)))
 #define palloc0_object(type) ((type *) palloc0(sizeof(type)))
 
+/*
+ * Allocate space for a struct of type "type" whose flexible array member
+ * "member" holds "count" elements.  See flex_size() for details.
+ */
+#define palloc_flex_array(type, member, count) ((type *) 
palloc(flex_size(type, member, count)))
+#define palloc0_flex_array(type, member, count) ((type *) 
palloc0(flex_size(type, member, count)))
+
 /*
  * Allocate space for "count" objects of type "type"
  */
-- 
2.56.0

From 635c518748465cacf3c12ec280f24e8d6229582f Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Fri, 20 Nov 2026 15:40:29 +0100
Subject: [PATCH v2 7/7] WIP: More flex_size candidates

These are the same issue as before, but the size is not constant, so
the compiler won't flag it.  (A static analyzer might?)
---
 src/backend/access/nbtree/nbtpage.c     | 4 ++--
 src/backend/access/nbtree/nbtree.c      | 4 ++--
 src/backend/access/nbtree/nbtxlog.c     | 4 ++--
 src/backend/statistics/dependencies.c   | 6 ++----
 src/backend/utils/adt/pg_dependencies.c | 2 +-
 src/backend/utils/adt/tsvector_op.c     | 5 ++---
 src/backend/utils/cache/typcache.c      | 4 +---
 src/fe_utils/simple_list.c              | 3 +--
 8 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/src/backend/access/nbtree/nbtpage.c 
b/src/backend/access/nbtree/nbtpage.c
index 83d6d367f81..a8256092930 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -1648,8 +1648,8 @@ _bt_delitems_delete_check(Relation rel, Buffer buf, 
Relation heapRel,
                        /* Exact match for deletable deltids entry -- ptid gets 
deleted */
                        if (vacposting == NULL)
                        {
-                               vacposting = 
palloc(offsetof(BTVacuumPostingData, deletetids) +
-                                                                       nitem * 
sizeof(uint16));
+                               vacposting = 
palloc_flex_array(BTVacuumPostingData, deletetids,
+                                                                               
           nitem);
                                vacposting->itup = itup;
                                vacposting->updatedoffset = idxoffnum;
                                vacposting->ndeletedtids = 0;
diff --git a/src/backend/access/nbtree/nbtree.c 
b/src/backend/access/nbtree/nbtree.c
index ef9ed290cae..2a5036aa3c1 100644
--- a/src/backend/access/nbtree/nbtree.c
+++ b/src/backend/access/nbtree/nbtree.c
@@ -1805,8 +1805,8 @@ btreevacuumposting(BTVacState *vstate, IndexTuple posting,
                         * TIDs, so start maintaining metadata describing how 
to update
                         * existing posting list tuple.
                         */
-                       vacposting = palloc(offsetof(BTVacuumPostingData, 
deletetids) +
-                                                               nitem * 
sizeof(uint16));
+                       vacposting = palloc_flex_array(BTVacuumPostingData, 
deletetids,
+                                                                               
   nitem);
 
                        vacposting->itup = posting;
                        vacposting->updatedoffset = updatedoffset;
diff --git a/src/backend/access/nbtree/nbtxlog.c 
b/src/backend/access/nbtree/nbtxlog.c
index dff7d286fc8..0bb87e07a20 100644
--- a/src/backend/access/nbtree/nbtxlog.c
+++ b/src/backend/access/nbtree/nbtxlog.c
@@ -556,8 +556,8 @@ btree_xlog_updates(Page page, OffsetNumber *updatedoffsets,
                itemid = PageGetItemId(page, updatedoffsets[i]);
                origtuple = (IndexTuple) PageGetItem(page, itemid);
 
-               vacposting = palloc(offsetof(BTVacuumPostingData, deletetids) +
-                                                       updates->ndeletedtids * 
sizeof(uint16));
+               vacposting = palloc_flex_array(BTVacuumPostingData, deletetids,
+                                                                          
updates->ndeletedtids);
                vacposting->updatedoffset = updatedoffsets[i];
                vacposting->itup = origtuple;
                vacposting->ndeletedtids = updates->ndeletedtids;
diff --git a/src/backend/statistics/dependencies.c 
b/src/backend/statistics/dependencies.c
index 612194193d9..7a7fc5cca00 100644
--- a/src/backend/statistics/dependencies.c
+++ b/src/backend/statistics/dependencies.c
@@ -391,8 +391,7 @@ statext_dependencies_build(StatsBuildData *data)
                        if (degree == 0.0)
                                continue;
 
-                       d = (MVDependency *) palloc0(offsetof(MVDependency, 
attributes)
-                                                                               
 + k * sizeof(AttrNumber));
+                       d = palloc0_flex_array(MVDependency, attributes, k);
 
                        /* copy the dependency (and keep the indexes into 
stxkeys) */
                        d->degree = degree;
@@ -556,8 +555,7 @@ statext_dependencies_deserialize(bytea *data)
                Assert((k >= 2) && (k <= STATS_MAX_DIMENSIONS));
 
                /* now that we know the number of attributes, allocate the 
dependency */
-               d = (MVDependency *) palloc0(offsetof(MVDependency, attributes)
-                                                                        + (k * 
sizeof(AttrNumber)));
+               d = palloc0_flex_array(MVDependency, attributes, k);
 
                d->degree = degree;
                d->nattributes = k;
diff --git a/src/backend/utils/adt/pg_dependencies.c 
b/src/backend/utils/adt/pg_dependencies.c
index 1b88c1dfc29..ab3861f59cd 100644
--- a/src/backend/utils/adt/pg_dependencies.c
+++ b/src/backend/utils/adt/pg_dependencies.c
@@ -202,7 +202,7 @@ dependencies_object_end(void *state)
         * Allocate enough space for the dependency, the attribute numbers in 
the
         * list and the final attribute number for the dependency.
         */
-       dep = palloc0(offsetof(MVDependency, attributes) + ((natts + 1) * 
sizeof(AttrNumber)));
+       dep = palloc0_flex_array(MVDependency, attributes, natts + 1);
        dep->nattributes = natts + 1;
 
        dep->attributes[natts] = parse->dependency;
diff --git a/src/backend/utils/adt/tsvector_op.c 
b/src/backend/utils/adt/tsvector_op.c
index bcb434c83e7..eaaa85759be 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -53,8 +53,6 @@ typedef struct StatEntry
        pg_attribute_counted_by(lenlexeme) char lexeme[FLEXIBLE_ARRAY_MEMBER];
 } StatEntry;
 
-#define STATENTRYHDRSZ (offsetof(StatEntry, lexeme))
-
 typedef struct
 {
        int32           weight;
@@ -2372,7 +2370,8 @@ insertStatEntry(MemoryContext persistentContext, 
TSVectorStat *stat, TSVector tx
 
        if (node == NULL)
        {
-               node = MemoryContextAlloc(persistentContext, STATENTRYHDRSZ + 
we->len);
+               node = MemoryContextAlloc(persistentContext,
+                                                                 
flex_size(StatEntry, lexeme, we->len));
                node->left = node->right = NULL;
                node->ndoc = 1;
                node->nentry = n;
diff --git a/src/backend/utils/cache/typcache.c 
b/src/backend/utils/cache/typcache.c
index f969b435cb7..7f1369a2276 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -2874,9 +2874,7 @@ load_enum_cache_data(TypeCacheEntry *tcache)
 
        /* OK, copy the data into CacheMemoryContext */
        oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
-       enumdata = (TypeCacheEnumData *)
-               palloc(offsetof(TypeCacheEnumData, enum_values) +
-                          numitems * sizeof(EnumItem));
+       enumdata = palloc_flex_array(TypeCacheEnumData, enum_values, numitems);
        enumdata->bitmap_base = bitmap_base;
        enumdata->sorted_values = bms_copy(bitmap);
        enumdata->num_values = numitems;
diff --git a/src/fe_utils/simple_list.c b/src/fe_utils/simple_list.c
index 03c11bae7c8..8db4eb0d839 100644
--- a/src/fe_utils/simple_list.c
+++ b/src/fe_utils/simple_list.c
@@ -64,8 +64,7 @@ simple_string_list_append(SimpleStringList *list, const char 
*val)
 {
        SimpleStringListCell *cell;
 
-       cell = (SimpleStringListCell *)
-               pg_malloc(offsetof(SimpleStringListCell, val) + strlen(val) + 
1);
+       cell = pg_malloc_flex_array(SimpleStringListCell, val, strlen(val) + 1);
 
        cell->next = NULL;
        cell->touched = false;
-- 
2.56.0

Reply via email to