Skip to content

Commit bc19f63

Browse files
committed
Avoid possibly-theoretical OOM crash hazard in hash_create().
One place in hash_create() used DynaHashAlloc() as a convenient shorthand for MemoryContextAlloc(). That was fine when it was written, but it stopped being fine when 9c911ec changed DynaHashAlloc() to use MCXT_ALLOC_NO_OOM (mea culpa). Change the code to call plain MemoryContextAlloc() as intended. I think that this bug may be unreachable in practice, since we now always create AllocSets with some space already allocated, so that an OOM failure here for a non-shared hash table should be impossible (with a hash table name of reasonable length anyway). And there aren't enough shared hash tables to make a crash for one of those probable. Nonetheless it's clearly not operating as designed, so back-patch to v16 where 9c911ec came in. Reported-by: Maksim Korotkov <[email protected]> Author: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected] Backpatch-through: 16
1 parent 005ccae commit bc19f63

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/backend/utils/hash/dynahash.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
390390
}
391391

392392
/* Initialize the hash header, plus a copy of the table name */
393-
hashp = (HTAB *) DynaHashAlloc(sizeof(HTAB) + strlen(tabname) + 1);
393+
hashp = (HTAB *) MemoryContextAlloc(CurrentDynaHashCxt,
394+
sizeof(HTAB) + strlen(tabname) + 1);
394395
MemSet(hashp, 0, sizeof(HTAB));
395396

396397
hashp->tabname = (char *) (hashp + 1);

0 commit comments

Comments
 (0)