Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wqoNU-002QCr-0r for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Aug 2026 08:50:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wqoNT-004qf6-0J for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Aug 2026 08:50:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wqoNS-004qey-2d for pgsql-hackers@lists.postgresql.org; Mon, 03 Aug 2026 08:50:30 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wqoNQ-00000001kKV-1IEY for pgsql-hackers@lists.postgresql.org; Mon, 03 Aug 2026 08:50:30 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-2d004f135b1so30386345ad.3 for ; Mon, 03 Aug 2026 01:50:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785747025; x=1786351825; darn=lists.postgresql.org; h=to:date:message-id:subject:mime-version:content-type:from:from:to :cc:subject:date:message-id:reply-to:content-type; bh=L3TLtN45MjRuEnzb/ReWWn4VeWuBCksJx8/QfNd4eJg=; b=jR0y9k1xpDaJtEGYejbjbKaEHaw82grznQ3MeEj9Z9Q676e5VA9BHorBGc295EruwW +3IqUmm1Thf2YXhTPtB+Utcvr+tnjRPSfNpbVp8s9BAVdgcnWZP6zKJDtakfcPEzX4v3 oysRPTML7oGTfzwhttnvV3PWaQY/CmIgVh4JByWd2MwYGR1Ti5fqmnPl3LDVrTM07dnv XblkinlvdphSYh6u035SAqpFuFuPAMWxcps0xCMX1FOXdGQkRmQy0w15+JymK9Hl/n4+ L2XQKSA+YsxuMX1oomTlRdsfH6Gs70lssjuRPgCOBpk3xU9nqHrJoVp/URdcPpN5ipGE EjTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785747025; x=1786351825; h=to:date:message-id:subject:mime-version:content-type:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=L3TLtN45MjRuEnzb/ReWWn4VeWuBCksJx8/QfNd4eJg=; b=UhYo8Oc5xkrs7ANjkSXWNu9YFnpv8NzjhY6xiky3QvSB5okwP1+ikI37x+9UhzZQgS k1jNlE9sZL0qmuOz0f674eQOwR1k/jQ0k7CqsaRrAnTvbbbk5Gvqc8UPWKlY50OKNtO8 PP7EsQb6gCvBNUGBlQQDTb0TVRHOqe88dNy2TtqPG05uu92it/NHAqOvg+92923siBWC mjIirYZPT0ZH9ZODiFSBwApKphBH9etQNtr2mJKvAxTKPFsv0v6qNcV0wQ31+p8no3+5 hqAAM/7tIL2F75TfCR0KcC97suAG6meHcLg+Q2MIf9XjdrLK2EaFQdPWxSkzWoMCQCLY VqoA== X-Gm-Message-State: AOJu0Yy3emAIm7ASHvCoHwje9DqpPB6ZwjcgA+D/zwEul//i0RXbhssQ /hhd4FdzC72W2NsZB08jEVhNnkqAZ/q5oIXa1sVv5hcOqdNyKSVlq4NFtqEHcg== X-Gm-Gg: AR+sD11OTbtBYUb8o5j2JEKo9/m+3f6Y/VlnCQok7oQRSSw+0p395HiWcHUCj89EV+s i3DP7WGkfcV4TDO0DT7dX9oQ0S3DZk5iYflaqpu7+ZhzUfbcI/DyU1L7V/EyeT/2jpWaiw/LCzH EXJkL3N0qecX44bN3HJcTbXet6yGopuSxRbSd/f3vPAd8GqB1I4v8GE9WTf1prTXanlQSH6TggI lN34MMWnVy1TbLuNScV0qvhYUt8Sl5iO+98XiXS0BTHt6Z3xF1STpZZUkZqSL4T09XcREtSrmvD aG3TQrznaSynEFw/4OVPx2neAqKHaDK6cfSZeY0NotjV5COMnObjSQLbOLn3qap7+NtIjisarAu MzZ8QQ1pkc4mFRh6gzRV/FhytZXf6x+YEOTfr6skF8fUQAvUuzR9zVylxMKBO34yEaT1WFWaWC7 BDTjQNoC674Q2RjU4/Imx9HjdxJDxuR8bzaL/Auz3WuJazzv2EmvHbvk1rmJ2lRBHd2EJrrQ== X-Received: by 2002:a17:902:cccc:b0:2ca:304:f93e with SMTP id d9443c01a7336-2d052411e56mr93452965ad.22.1785747024762; Mon, 03 Aug 2026 01:50:24 -0700 (PDT) Received: from smtpclient.apple ([185.135.79.125]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2d04b1216bdsm34714575ad.69.2026.08.03.01.50.22 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 03 Aug 2026 01:50:24 -0700 (PDT) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_2B383A20-221B-4189-9E95-17E53660CD4D" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Fix NO_OOM handling in MemoryContextAllocAligned Message-Id: <07DAC4C3-120D-4F3C-8FEE-BA236F7E9C1D@gmail.com> Date: Mon, 3 Aug 2026 16:49:48 +0800 To: PostgreSQL Hackers X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_2B383A20-221B-4189-9E95-17E53660CD4D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hi, This has been in my notes for a while. MemoryContextAllocAligned() calls = MemoryContextAllocExtended() and passes =E2=80=9Cflags" with only = MCXT_ALLOC_ZERO removed. When =E2=80=9Cflags" 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/ --Apple-Mail=_2B383A20-221B-4189-9E95-17E53660CD4D Content-Disposition: attachment; filename=v1-0001-Fix-NO_OOM-handling-in-MemoryContextAllocAligned.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-Fix-NO_OOM-handling-in-MemoryContextAllocAligned.patch" Content-Transfer-Encoding: quoted-printable =46rom=20e9e7bf36bf8122277cee7b6b74a9cba8bc10b17f=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Mon,=203=20Aug=202026=2016:21:23=20+0800=0ASubject:=20[PATCH=20= v1]=20Fix=20NO_OOM=20handling=20in=20MemoryContextAllocAligned=0A=0A= MemoryContextAllocAligned()=20passes=20its=20flags=20through=20to=0A= MemoryContextAllocExtended(),=20except=20for=20MCXT_ALLOC_ZERO.=20= Therefore,=20when=0AMCXT_ALLOC_NO_OOM=20is=20specified,=20the=20= underlying=20allocation=20can=20return=20NULL.=0A=0AThe=20= aligned-allocation=20path=20did=20not=20check=20for=20that=20case=20= before=20computing=20the=0Aaligned=20pointer=20and=20setting=20up=20the=20= redirection=20MemoryChunk.=20That=20could=20perform=0Apointer=20= arithmetic=20from=20NULL=20and=20then=20access=20an=20invalid=20chunk=20= header,=20instead=0Aof=20returning=20NULL=20to=20the=20caller.=0A=0A= Return=20NULL=20immediately=20when=20the=20underlying=20allocation=20= fails.=20This=20also=20lets=0AAlignedAllocRealloc()=20handle=20the=20= failure=20as=20intended=20when=20repalloc_extended()=0Ais=20used=20on=20= an=20aligned=20allocation=20with=20MCXT_ALLOC_NO_OOM.=0A=0AAuthor:=20= Chao=20Li=20=0A---=0A=20src/backend/utils/mmgr/mcxt.c=20= |=202=20++=0A=201=20file=20changed,=202=20insertions(+)=0A=0Adiff=20= --git=20a/src/backend/utils/mmgr/mcxt.c=20= b/src/backend/utils/mmgr/mcxt.c=0Aindex=20930fc457328..fe5c2d8e01b=20= 100644=0A---=20a/src/backend/utils/mmgr/mcxt.c=0A+++=20= b/src/backend/utils/mmgr/mcxt.c=0A@@=20-1540,6=20+1540,8=20@@=20= MemoryContextAllocAligned(MemoryContext=20context,=0A=20=09=20*/=0A=20=09= unaligned=20=3D=20MemoryContextAllocExtended(context,=20alloc_size,=0A=20= =09=09=09=09=09=09=09=09=09=09=20=20=20flags=20&=20~MCXT_ALLOC_ZERO);=0A= +=09if=20(unlikely(unaligned=20=3D=3D=20NULL))=0A+=09=09return=20NULL;=0A= =20=0A=20=09/*=20compute=20the=20aligned=20pointer=20*/=0A=20=09aligned=20= =3D=20(void=20*)=20TYPEALIGN(alignto,=20(char=20*)=20unaligned=20+=0A--=20= =0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_2B383A20-221B-4189-9E95-17E53660CD4D--