Fix NO_OOM handling in MemoryContextAllocAligned

Started by Chao Li4 months ago3 messageshackers
Jump to latest
#1Chao Li
li.evan.chao@gmail.com

Hi,

This has been in my notes for a while. MemoryContextAllocAligned() calls MemoryContextAllocExtended() and passes “flags" with only MCXT_ALLOC_ZERO removed. When “flags" includes MCXT_ALLOC_NO_OOM, MemoryContextAllocExtended() may return NULL, but MemoryContextAllocAligned() doesn't handle the returned NULL, which may
lead to a crash.

AlignedAllocRealloc() wraps MemoryContextAllocAligned() and handles a NULL return from it, which I think further supports that this is a bug.

I didn't find an in-tree call site that passes MCXT_ALLOC_NO_OOM to MemoryContextAllocAligned() or palloc_aligned(), so this cannot be triggered from current core code. However, a third-party extension could hit the bug.

The attached patch is a quick fix that adds a NULL check.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

Attachments:

t253287_1
v1-0001-Fix-NO_OOM-handling-in-MemoryContextAllocAligned.patchapplication/octet-stream; name=v1-0001-Fix-NO_OOM-handling-in-MemoryContextAllocAligned.patch; x-unix-mode=0644Download+2-1
#2David Rowley
dgrowleyml@gmail.com
In reply to: Chao Li (#1)
Re: Fix NO_OOM handling in MemoryContextAllocAligned

On Mon, 3 Aug 2026 at 20:50, Chao Li <li.evan.chao@gmail.com> wrote:

MemoryContextAllocAligned() calls MemoryContextAllocExtended() and passes “flags" with only MCXT_ALLOC_ZERO removed. When “flags" includes MCXT_ALLOC_NO_OOM, MemoryContextAllocExtended() may return NULL, but MemoryContextAllocAligned() doesn't handle the returned NULL, which may
lead to a crash.

Thanks. Pushed.

David

#3Chao Li
li.evan.chao@gmail.com
In reply to: David Rowley (#2)
Re: Fix NO_OOM handling in MemoryContextAllocAligned

On Aug 4, 2026, at 12:11, David Rowley <dgrowleyml@gmail.com> wrote:

On Mon, 3 Aug 2026 at 20:50, Chao Li <li.evan.chao@gmail.com> wrote:

MemoryContextAllocAligned() calls MemoryContextAllocExtended() and passes “flags" with only MCXT_ALLOC_ZERO removed. When “flags" includes MCXT_ALLOC_NO_OOM, MemoryContextAllocExtended() may return NULL, but MemoryContextAllocAligned() doesn't handle the returned NULL, which may
lead to a crash.

Thanks. Pushed.

David

Thanks for pushing.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/