From 9e91af5989df693976bee78a5eadf737f0e18965 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 10 Jun 2024 21:20:54 -0700 Subject: [PATCH] Added checks for invalid indexes - #591 --- src/hnswutils.c | 3 +++ src/ivfinsert.c | 3 +++ src/ivfutils.c | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/hnswutils.c b/src/hnswutils.c index d3ba911..093e7ef 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -300,6 +300,9 @@ HnswGetMetaPageInfo(Relation index, int *m, HnswElement * entryPoint) page = BufferGetPage(buf); metap = HnswPageGetMeta(page); + if (unlikely(metap->magicNumber != HNSW_MAGIC_NUMBER)) + elog(ERROR, "hnsw index is not valid"); + if (m != NULL) *m = metap->m; diff --git a/src/ivfinsert.c b/src/ivfinsert.c index fd1ea17..1da4822 100644 --- a/src/ivfinsert.c +++ b/src/ivfinsert.c @@ -19,6 +19,9 @@ FindInsertPage(Relation index, Datum *values, BlockNumber *insertPage, ListInfo FmgrInfo *procinfo; Oid collation; + /* Ensure index is valid */ + IvfflatGetMetaPageInfo(index, NULL, NULL); + /* Avoid compiler warning */ listInfo->blkno = nextblkno; listInfo->offno = FirstOffsetNumber; diff --git a/src/ivfutils.c b/src/ivfutils.c index 74a4159..2098a64 100644 --- a/src/ivfutils.c +++ b/src/ivfutils.c @@ -170,7 +170,11 @@ IvfflatGetMetaPageInfo(Relation index, int *lists, int *dimensions) page = BufferGetPage(buf); metap = IvfflatPageGetMeta(page); - *lists = metap->lists; + if (unlikely(metap->magicNumber != IVFFLAT_MAGIC_NUMBER)) + elog(ERROR, "ivfflat index is not valid"); + + if (lists != NULL) + *lists = metap->lists; if (dimensions != NULL) *dimensions = metap->dimensions;