Skip to content

fix: prevent dangling index_ after a failed index load#362

Closed
andreinknv wants to merge 1 commit into
yoshoku:mainfrom
andreinknv:fix/dangling-index-after-failed-load
Closed

fix: prevent dangling index_ after a failed index load#362
andreinknv wants to merge 1 commit into
yoshoku:mainfrom
andreinknv:fix/dangling-index-after-failed-load

Conversation

@andreinknv

Copy link
Copy Markdown

Problem

readIndexSync (both BruteforceSearch and HierarchicalNSW) and the
asynchronous Load*IndexWorker::Execute methods delete the existing
index_ before constructing its replacement:

if (index_) delete index_;
index_ = new hnswlib::HierarchicalNSW<float>(space_, filename, /* ... */);

If that constructor throws — a corrupt or unreadable index file —
index_ is left pointing at freed memory. The next method call on the
instance is a use-after-free, and the destructor later runs
delete index_ a second time (double-free).

Fix

Construct the replacement into a local pointer first; delete the old
index and reassign only after construction succeeds. On failure index_
keeps its previous valid value. This mirrors the pattern initIndex
already uses.

Verification

npm test — 100/100 passing; native addon builds clean.

🤖 Generated with Claude Code

readIndexSync (both classes) and the async Load*IndexWorker::Execute
methods deleted the existing index_ before constructing its
replacement. When the constructor threw — corrupt or unreadable file
— index_ was left pointing at freed memory: a use-after-free on the
next method call and a double-free in the destructor.

Construct the replacement into a local first; delete the old index
and reassign only after construction succeeds. On failure index_
keeps its previous valid value. Mirrors the pattern initIndex
already uses.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@andreinknv andreinknv closed this by deleting the head repository May 28, 2026
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.

1 participant