Skip to content

libsql-sqlite3: Fix vector_top_k crash with k = 0 - #2332

Open
colinhacks wants to merge 1 commit into
tursodatabase:mainfrom
colinhacks:fix-vector-top-k-zero
Open

colinhacks wants to merge 1 commit into
tursodatabase:mainfrom
colinhacks:fix-vector-top-k-zero

Conversation

@colinhacks

Copy link
Copy Markdown

vector_top_k with k = 0 on an index that has rows crashed the process with a double free. diskAnnSearch passed k as the size of the top-candidates buffers, so sqlite3_malloc(0) returned NULL. diskAnnSearchCtxInit then freed the buffers it did get, deinitialized the query vectors and returned SQLITE_NOMEM, and the out: path in diskAnnSearch freed all of it again.

  • diskAnnSearch returns no rows for k = 0 before the init, the same way it does for an empty index.
  • A failed init returns straight away instead of going through diskAnnSearchCtxDeinit, as diskAnnInsert already does.
  • The hidden idx, vector and k columns return the arguments the query passed. vectorVtabColumn read the output rows at iCol - 3 for them, a negative index, so all three came back as the row's id.

The bundled amalgamations in libsql-ffi/bundled (plain and SQLite3MultipleCiphers) are regenerated from the patched source, so the diff there is the same hunks.

Testing

Added vector-top-k-zero and vector-hidden-columns to test/libsql_vector_index.test. Without the change testfixture aborts at the first one, and the query in the second returns the id in all four columns. With it both pass, along with the rest of libsql_vector_index.test. libsql_vector.test passes except vector-1-conversion-f8, which fails the same way without the change (a float rounding difference, built on macOS arm64). I didn't run the Rust crates' tests.

The k truncation from the issue isn't covered here.

Fixes #2331

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.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 17:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vector_top_k with k = 0 crashes the process on a non-empty index

2 participants