postgres.git / summary / log / commit / refs

commit    cce758797f75c19080801ddad213f46d7900ec2b
Author:   David Rowley <drowley@postgresql.org>
Date:     Tue Aug 04 04:10:18 2026 +0000
First released in: 16.15, 17.11, 18.6

    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 <li.evan.chao@gmail.com>
    Discussion: https://postgr.es/m/07DAC4C3-120D-4F3C-8FEE-BA236F7E9C1D@gmail.com
    Backpatch-through: 16


src/backend/utils/mmgr/mcxt.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/backend/utils/mmgr/mcxt.c b/src/backend/utils/mmgr/mcxt.c index 4542a1b1cd1..cba94d9e24d 100644 --- a/src/backend/utils/mmgr/mcxt.c +++ b/src/backend/utils/mmgr/mcxt.c @@ -1478,6 +1478,13 @@ MemoryContextAllocAligned(MemoryContext context, /* perform the actual allocation */ unaligned = MemoryContextAllocExtended(context, alloc_size, flags); + if (unlikely(unaligned == NULL)) + { + /* NULL can be returned only when using MCXT_ALLOC_NO_OOM */ + Assert(flags & MCXT_ALLOC_NO_OOM); + return NULL; + } + /* set the aligned pointer */ aligned = (void *) TYPEALIGN(alignto, (char *) unaligned + sizeof(MemoryChunk)); [parent: 25e54cec72ae]