From: m.korotkov@postgrespro.ru
To: pgsql-hackers@postgresql.org
Subject: [PATCH] dynahash: add memory allocation failure check
Date: Wed, 23 Apr 2025 11:32:42 +0300
Message-ID: <219bdccd460510efaccf90b57e5e5ef2@postgrespro.ru> (raw)
Hi all,
I found a case of potential NULL pointer dereference.
In src/backend/utils/hash/dynahash.c in function HTAB *hash_create() the
result of the DynaHashAlloc() is used unsafely.
The function DynaHashAlloc() calls MemoryContextAllocExtended() with
MCXT_ALLOC_NO_OOM and can return a NULL pointer.
Added the pointer check for avoiding a potential problem.
---
Best regards, Korotkov Maksim
PostgresPro
m.korotkov@postgrespro.ru
Attachments:0001-dynahash-add-memory-allocation-failure-check.patch (text/x-diff, 1.2K) download
From 75916a6855bb9e96c7b34b76c0380edc157c150c Mon Sep 17 00:00:00 2001
From: Maksim Korotkov <m.korotkov@postgrespro.ru>
Date: Tue, 22 Apr 2025 12:20:58 +0300
Subject: [PATCH] dynahash: add memory allocation failure check
The function DynaHashAlloc() calls MemoryContextAllocExtended() with MCXT_ALLOC_NO_OOM
and can return a NULL pointer.
Fixes: e3860ffa4dd ("Initial pgindent run with pg_bsd_indent version 2.0.")
Signed-off-by: Maksim Korotkov <m.korotkov@postgrespro.ru>
---
src/backend/utils/hash/dynahash.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.cindex 3f25929f2d8..0cfee50dc21 100644--- a/src/backend/utils/hash/dynahash.c+++ b/src/backend/utils/hash/dynahash.c@@ -391,6 +391,12 @@ hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
/* Initialize the hash header, plus a copy of the table name */
hashp = (HTAB *) DynaHashAlloc(sizeof(HTAB) + strlen(tabname) + 1);
+ if (unlikely(hashp == NULL))+ {+ ereport(ERROR,+ (errcode(ERRCODE_OUT_OF_MEMORY),+ errmsg("out of memory")));+ }
MemSet(hashp, 0, sizeof(HTAB));
hashp->tabname = (char *) (hashp + 1);
--
2.34.1
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: m.korotkov@postgrespro.ru
Subject: Re: [PATCH] dynahash: add memory allocation failure check
In-Reply-To: <219bdccd460510efaccf90b57e5e5ef2@postgrespro.ru>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox