pgvector.git / summary / log / commit / refs

commit    5c36d4dfa2a77c021a14ff0c5ca3f43b18b98f3d
Author:   Andrew Kane <andrew@ankane.org>
Date:     Tue Jul 28 22:18:40 2026 +0000

    Hardened HnswSharedMemoryAlloc [skip ci]


src/hnswbuild.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/hnswbuild.c b/src/hnswbuild.c index 7d19146a..b9494958 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -527,7 +527,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn * Check that we have enough memory available for the new element now that * we have the allocator lock, and flush pages if needed. */ - if (graph->memoryUsed + memoryMargin >= graph->memoryTotal) + if (add_size(graph->memoryUsed, memoryMargin) >= graph->memoryTotal) { LWLockRelease(&graph->allocatorLock); @@ -662,16 +662,18 @@ HnswSharedMemoryAlloc(Size size, void *state) { HnswBuildState *buildstate = (HnswBuildState *) state; Size alignedSize = MAXALIGN(size); + Size newMemoryUsed; void *chunk; if (alignedSize > 1024 * 1024) elog(ERROR, "hnsw allocation too large"); - if (buildstate->graph->memoryUsed + alignedSize > buildstate->graph->memoryTotal) + newMemoryUsed = add_size(buildstate->graph->memoryUsed, alignedSize); + if (newMemoryUsed > buildstate->graph->memoryTotal) elog(ERROR, "hnsw allocator out of memory"); chunk = buildstate->hnswarea + buildstate->graph->memoryUsed; - buildstate->graph->memoryUsed += alignedSize; + buildstate->graph->memoryUsed = newMemoryUsed; return chunk; } [parent: ec0d8ddb104f]