Fix NO_OOM handling in MemoryContextAllocAligned

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Fix NO_OOM handling in MemoryContextAllocAligned
Date: 2026-08-03 08:49:48
Message-ID: 07DAC4C3-120D-4F3C-8FEE-BA236F7E9C1D@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/

Attachment Content-Type Size
v1-0001-Fix-NO_OOM-handling-in-MemoryContextAllocAligned.patch application/octet-stream 1.5 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message 张鹏超 2026-08-03 08:59:02 Re: to_date()/to_timestamp() silently accept month=0 and day=0
Previous Message Peter Smith 2026-08-03 08:44:08 Re: A new C function `get_partition_root`.