From 9ed39fb94b08447b1769e530ae0938afe12e1257 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 25 Feb 2026 10:39:01 -0800 Subject: [PATCH] Hardened shared memory allocator Co-authored-by: chungkn from OneMount Group --- src/hnswbuild.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/hnswbuild.c b/src/hnswbuild.c index a6f4cd4..b15c144 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -658,9 +658,17 @@ static void * HnswSharedMemoryAlloc(Size size, void *state) { HnswBuildState *buildstate = (HnswBuildState *) state; - void *chunk = buildstate->hnswarea + buildstate->graph->memoryUsed; + Size alignedSize = MAXALIGN(size); + void *chunk; - buildstate->graph->memoryUsed += MAXALIGN(size); + if (alignedSize > 1024 * 1024) + elog(ERROR, "hnsw allocation too large"); + + if (buildstate->graph->memoryUsed + alignedSize > buildstate->graph->memoryTotal) + elog(ERROR, "hnsw allocator out of memory"); + + chunk = buildstate->hnswarea + buildstate->graph->memoryUsed; + buildstate->graph->memoryUsed += alignedSize; return chunk; }