From 345b64f148ae5297781081636ec9ccb117de92ac Mon Sep 17 00:00:00 2001 From: Colin McDonnell <3084745+colinhacks@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:57:40 -0800 Subject: [PATCH] libsql-sqlite3: Fix vector_top_k crash with k = 0 diskAnnSearch passed k as the size of the top-candidates buffers, so k = 0 made sqlite3_malloc(0) return NULL. The init then freed its buffers and returned SQLITE_NOMEM, and the error path freed them again. Return no rows for k = 0 up front, and return straight after a failed init. Also return the arguments for the hidden idx, vector and k columns, which read the output rows with a negative column index before. --- .../SQLite3MultipleCiphers/src/sqlite3.c | 33 ++++++++++++++++++- libsql-ffi/bundled/src/sqlite3.c | 33 ++++++++++++++++++- libsql-sqlite3/src/vectordiskann.c | 8 ++++- libsql-sqlite3/src/vectorvtab.c | 25 ++++++++++++++ libsql-sqlite3/test/libsql_vector_index.test | 16 +++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) diff --git a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c index 62a917fb9b..9ee7490ae8 100644 --- a/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c +++ b/libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c @@ -217469,6 +217469,11 @@ int diskAnnSearch( *pzErrMsg = sqlite3_mprintf("vector index(search): vector type differs from column type: %d != %d", pVector->type, pIndex->nNodeVectorType); return SQLITE_ERROR; } + if( k == 0 ){ + pRows->nRows = 0; + pRows->nCols = pKey->nKeyColumns; + return SQLITE_OK; + } rc = diskAnnSelectRandomShadowRow(pIndex, &nStartRowid); if( rc == SQLITE_DONE ){ @@ -217482,8 +217487,9 @@ int diskAnnSearch( } rc = diskAnnSearchCtxInit(pIndex, &ctx, pVector, pIndex->searchL, k, DISKANN_BLOB_READONLY); if( rc != SQLITE_OK ){ + // on failure the init has already freed what it allocated, so ctx must not be deinitialized *pzErrMsg = sqlite3_mprintf("vector index(search): failed to initialize search context"); - goto out; + return rc; } rc = diskAnnSearchInternal(pIndex, &ctx, nStartRowid, pzErrMsg); if( rc != SQLITE_OK ){ @@ -220050,6 +220056,7 @@ struct vectorVtab_cursor { int nWrites; /* Number of row written to the storage backing virtual table */ VectorOutRows rows; int iRow; + sqlite3_value *apArgs[3]; /* Arguments of the last xFilter, returned as the hidden columns */ }; /* Column numbers */ @@ -220058,6 +220065,14 @@ struct vectorVtab_cursor { #define VECTOR_COLUMN_K 2 #define VECTOR_COLUMN_OFFSET 3 +static void vectorVtabFreeArgs(vectorVtab_cursor *pCur){ + int i; + for(i = 0; i < VECTOR_COLUMN_OFFSET; i++){ + sqlite3_value_free(pCur->apArgs[i]); + pCur->apArgs[i] = NULL; + } +} + static int vectorVtabConnect( sqlite3 *db, void *pAux, @@ -220110,6 +220125,7 @@ static int vectorVtabClose(sqlite3_vtab_cursor *cur){ vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur; vectorVtab *pVTab = (vectorVtab *)cur->pVtab; vectorOutRowsFree(pVTab->db, &pCur->rows); + vectorVtabFreeArgs(pCur); sqlite3_free(pCur); return SQLITE_OK; } @@ -220131,6 +220147,12 @@ static int vectorVtabColumn( int iCol /* Which column to return */ ){ vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur; + if( iCol < VECTOR_COLUMN_OFFSET ){ + if( pCur->apArgs[iCol] != NULL ){ + sqlite3_result_value(context, pCur->apArgs[iCol]); + } + return SQLITE_OK; + } vectorOutRowsGet(context, &pCur->rows, pCur->iRow, iCol - VECTOR_COLUMN_OFFSET); return SQLITE_OK; } @@ -220153,9 +220175,18 @@ static int vectorVtabFilter( ){ vectorVtab_cursor *pCur = (vectorVtab_cursor *)pVtabCursor; vectorVtab *pVTab = (vectorVtab *)pVtabCursor->pVtab; + int i; pCur->rows.aIntValues = NULL; pCur->rows.ppValues = NULL; + vectorVtabFreeArgs(pCur); + for(i = 0; i < argc && i < VECTOR_COLUMN_OFFSET; i++){ + pCur->apArgs[i] = sqlite3_value_dup(argv[i]); + if( pCur->apArgs[i] == NULL ){ + return SQLITE_NOMEM_BKPT; + } + } + if( vectorIndexSearch(pVTab->db, argc, argv, &pCur->rows, &pCur->nReads, &pCur->nWrites, &pVTab->base.zErrMsg) != 0 ){ return SQLITE_ERROR; } diff --git a/libsql-ffi/bundled/src/sqlite3.c b/libsql-ffi/bundled/src/sqlite3.c index 62a917fb9b..9ee7490ae8 100644 --- a/libsql-ffi/bundled/src/sqlite3.c +++ b/libsql-ffi/bundled/src/sqlite3.c @@ -217469,6 +217469,11 @@ int diskAnnSearch( *pzErrMsg = sqlite3_mprintf("vector index(search): vector type differs from column type: %d != %d", pVector->type, pIndex->nNodeVectorType); return SQLITE_ERROR; } + if( k == 0 ){ + pRows->nRows = 0; + pRows->nCols = pKey->nKeyColumns; + return SQLITE_OK; + } rc = diskAnnSelectRandomShadowRow(pIndex, &nStartRowid); if( rc == SQLITE_DONE ){ @@ -217482,8 +217487,9 @@ int diskAnnSearch( } rc = diskAnnSearchCtxInit(pIndex, &ctx, pVector, pIndex->searchL, k, DISKANN_BLOB_READONLY); if( rc != SQLITE_OK ){ + // on failure the init has already freed what it allocated, so ctx must not be deinitialized *pzErrMsg = sqlite3_mprintf("vector index(search): failed to initialize search context"); - goto out; + return rc; } rc = diskAnnSearchInternal(pIndex, &ctx, nStartRowid, pzErrMsg); if( rc != SQLITE_OK ){ @@ -220050,6 +220056,7 @@ struct vectorVtab_cursor { int nWrites; /* Number of row written to the storage backing virtual table */ VectorOutRows rows; int iRow; + sqlite3_value *apArgs[3]; /* Arguments of the last xFilter, returned as the hidden columns */ }; /* Column numbers */ @@ -220058,6 +220065,14 @@ struct vectorVtab_cursor { #define VECTOR_COLUMN_K 2 #define VECTOR_COLUMN_OFFSET 3 +static void vectorVtabFreeArgs(vectorVtab_cursor *pCur){ + int i; + for(i = 0; i < VECTOR_COLUMN_OFFSET; i++){ + sqlite3_value_free(pCur->apArgs[i]); + pCur->apArgs[i] = NULL; + } +} + static int vectorVtabConnect( sqlite3 *db, void *pAux, @@ -220110,6 +220125,7 @@ static int vectorVtabClose(sqlite3_vtab_cursor *cur){ vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur; vectorVtab *pVTab = (vectorVtab *)cur->pVtab; vectorOutRowsFree(pVTab->db, &pCur->rows); + vectorVtabFreeArgs(pCur); sqlite3_free(pCur); return SQLITE_OK; } @@ -220131,6 +220147,12 @@ static int vectorVtabColumn( int iCol /* Which column to return */ ){ vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur; + if( iCol < VECTOR_COLUMN_OFFSET ){ + if( pCur->apArgs[iCol] != NULL ){ + sqlite3_result_value(context, pCur->apArgs[iCol]); + } + return SQLITE_OK; + } vectorOutRowsGet(context, &pCur->rows, pCur->iRow, iCol - VECTOR_COLUMN_OFFSET); return SQLITE_OK; } @@ -220153,9 +220175,18 @@ static int vectorVtabFilter( ){ vectorVtab_cursor *pCur = (vectorVtab_cursor *)pVtabCursor; vectorVtab *pVTab = (vectorVtab *)pVtabCursor->pVtab; + int i; pCur->rows.aIntValues = NULL; pCur->rows.ppValues = NULL; + vectorVtabFreeArgs(pCur); + for(i = 0; i < argc && i < VECTOR_COLUMN_OFFSET; i++){ + pCur->apArgs[i] = sqlite3_value_dup(argv[i]); + if( pCur->apArgs[i] == NULL ){ + return SQLITE_NOMEM_BKPT; + } + } + if( vectorIndexSearch(pVTab->db, argc, argv, &pCur->rows, &pCur->nReads, &pCur->nWrites, &pVTab->base.zErrMsg) != 0 ){ return SQLITE_ERROR; } diff --git a/libsql-sqlite3/src/vectordiskann.c b/libsql-sqlite3/src/vectordiskann.c index 6af07528ba..c7e9a5e634 100644 --- a/libsql-sqlite3/src/vectordiskann.c +++ b/libsql-sqlite3/src/vectordiskann.c @@ -1478,6 +1478,11 @@ int diskAnnSearch( *pzErrMsg = sqlite3_mprintf("vector index(search): vector type differs from column type: %d != %d", pVector->type, pIndex->nNodeVectorType); return SQLITE_ERROR; } + if( k == 0 ){ + pRows->nRows = 0; + pRows->nCols = pKey->nKeyColumns; + return SQLITE_OK; + } rc = diskAnnSelectRandomShadowRow(pIndex, &nStartRowid); if( rc == SQLITE_DONE ){ @@ -1491,8 +1496,9 @@ int diskAnnSearch( } rc = diskAnnSearchCtxInit(pIndex, &ctx, pVector, pIndex->searchL, k, DISKANN_BLOB_READONLY); if( rc != SQLITE_OK ){ + // on failure the init has already freed what it allocated, so ctx must not be deinitialized *pzErrMsg = sqlite3_mprintf("vector index(search): failed to initialize search context"); - goto out; + return rc; } rc = diskAnnSearchInternal(pIndex, &ctx, nStartRowid, pzErrMsg); if( rc != SQLITE_OK ){ diff --git a/libsql-sqlite3/src/vectorvtab.c b/libsql-sqlite3/src/vectorvtab.c index cbe6f2b6e4..c292131bb6 100644 --- a/libsql-sqlite3/src/vectorvtab.c +++ b/libsql-sqlite3/src/vectorvtab.c @@ -43,6 +43,7 @@ struct vectorVtab_cursor { int nWrites; /* Number of row written to the storage backing virtual table */ VectorOutRows rows; int iRow; + sqlite3_value *apArgs[3]; /* Arguments of the last xFilter, returned as the hidden columns */ }; /* Column numbers */ @@ -51,6 +52,14 @@ struct vectorVtab_cursor { #define VECTOR_COLUMN_K 2 #define VECTOR_COLUMN_OFFSET 3 +static void vectorVtabFreeArgs(vectorVtab_cursor *pCur){ + int i; + for(i = 0; i < VECTOR_COLUMN_OFFSET; i++){ + sqlite3_value_free(pCur->apArgs[i]); + pCur->apArgs[i] = NULL; + } +} + static int vectorVtabConnect( sqlite3 *db, void *pAux, @@ -103,6 +112,7 @@ static int vectorVtabClose(sqlite3_vtab_cursor *cur){ vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur; vectorVtab *pVTab = (vectorVtab *)cur->pVtab; vectorOutRowsFree(pVTab->db, &pCur->rows); + vectorVtabFreeArgs(pCur); sqlite3_free(pCur); return SQLITE_OK; } @@ -124,6 +134,12 @@ static int vectorVtabColumn( int iCol /* Which column to return */ ){ vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur; + if( iCol < VECTOR_COLUMN_OFFSET ){ + if( pCur->apArgs[iCol] != NULL ){ + sqlite3_result_value(context, pCur->apArgs[iCol]); + } + return SQLITE_OK; + } vectorOutRowsGet(context, &pCur->rows, pCur->iRow, iCol - VECTOR_COLUMN_OFFSET); return SQLITE_OK; } @@ -146,9 +162,18 @@ static int vectorVtabFilter( ){ vectorVtab_cursor *pCur = (vectorVtab_cursor *)pVtabCursor; vectorVtab *pVTab = (vectorVtab *)pVtabCursor->pVtab; + int i; pCur->rows.aIntValues = NULL; pCur->rows.ppValues = NULL; + vectorVtabFreeArgs(pCur); + for(i = 0; i < argc && i < VECTOR_COLUMN_OFFSET; i++){ + pCur->apArgs[i] = sqlite3_value_dup(argv[i]); + if( pCur->apArgs[i] == NULL ){ + return SQLITE_NOMEM_BKPT; + } + } + if( vectorIndexSearch(pVTab->db, argc, argv, &pCur->rows, &pCur->nReads, &pCur->nWrites, &pVTab->base.zErrMsg) != 0 ){ return SQLITE_ERROR; } diff --git a/libsql-sqlite3/test/libsql_vector_index.test b/libsql-sqlite3/test/libsql_vector_index.test index c3cd148577..1d46f168b3 100644 --- a/libsql-sqlite3/test/libsql_vector_index.test +++ b/libsql-sqlite3/test/libsql_vector_index.test @@ -108,6 +108,22 @@ do_execsql_test vector-update { SELECT rowid FROM vector_top_k('t_update_idx', vector('[1,1,1]'), 2); } {20 10} +do_execsql_test vector-top-k-zero { + CREATE TABLE t_top_k_zero( id INTEGER PRIMARY KEY, v FLOAT32(3) ); + CREATE INDEX t_top_k_zero_idx ON t_top_k_zero( libsql_vector_idx(v) ); + INSERT INTO t_top_k_zero VALUES (1, vector('[1,1,1]')); + INSERT INTO t_top_k_zero VALUES (2, vector('[-1,-1,-1]')); + SELECT count(*) FROM vector_top_k('t_top_k_zero_idx', vector('[1,1,1]'), 0); +} {0} + +do_execsql_test vector-hidden-columns { + CREATE TABLE t_hidden( id INTEGER PRIMARY KEY, v FLOAT32(3) ); + CREATE INDEX t_hidden_idx ON t_hidden( libsql_vector_idx(v) ); + INSERT INTO t_hidden VALUES (10, vector('[1,1,1]')); + INSERT INTO t_hidden VALUES (20, vector('[-1,-1,-1]')); + SELECT idx, vector, k, id FROM vector_top_k('t_hidden_idx', '[1,1,1]', 2); +} {t_hidden_idx {[1,1,1]} 2 10 t_hidden_idx {[1,1,1]} 2 20} + do_execsql_test vector-simple { CREATE TABLE t_simple( v FLOAT32(3)); CREATE INDEX t_simple_idx ON t_simple( libsql_vector_idx(v) );