From: David Rowley Date: Tue, 4 Aug 2026 04:09:29 +0000 (+1200) Subject: Fix missing MCXT_ALLOC_NO_OOM handling in MemoryContextAllocAligned X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2FREL_19_STABLE;p=thirdparty%2Fpostgresql.git Fix missing MCXT_ALLOC_NO_OOM handling in MemoryContextAllocAligned Fix missing NULL check in MemoryContextAllocAligned(). The underlying call to MemoryContextAllocExtended() could return NULL when flags contains MCXT_ALLOC_NO_OOM and the underlying malloc fails. There are no current callers using MemoryContextAllocAligned() that pass the MCXT_ALLOC_NO_OOM in core, so no live bug fix in core here. However, an extension might use this pattern, so we'd better fix. Fix this so we correctly pass the NULL to the caller rather than trying to write to a NULL memory address. This also fixes the same bug in AlignedAllocRealloc(), which is also unused in core. Backpatch to v16, where these functions first appeared. Author: Chao Li Discussion: https://postgr.es/m/07DAC4C3-120D-4F3C-8FEE-BA236F7E9C1D@gmail.com Backpatch-through: 16 --- diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index 930fc457328..594c7a93bba 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -1541,6 +1541,13 @@ MemoryContextAllocAligned(MemoryContext context, unaligned = MemoryContextAllocExtended(context, alloc_size, flags & ~MCXT_ALLOC_ZERO); + if (unlikely(unaligned == NULL)) + { + /* NULL can be returned only when using MCXT_ALLOC_NO_OOM */ + Assert(flags & MCXT_ALLOC_NO_OOM); + return NULL; + } + /* compute the aligned pointer */ aligned = (void *) TYPEALIGN(alignto, (char *) unaligned + sizeof(MemoryChunk));