Skip to content

Fix heap-buffer-overflow in HNSW visited bitmap - #83

Merged
adsharma merged 1 commit into
mainfrom
fix-hnsw-visited-overflow
Sep 9, 2026
Merged

Fix heap-buffer-overflow in HNSW visited bitmap#83
adsharma merged 1 commit into
mainfrom
fix-hnsw-visited-overflow

Conversation

@adsharma

@adsharma adsharma commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Fixes LadybugDB/ladybug#940.

ASan reported a heap-buffer-overflow READ in VisitedState::contains (via OnDiskHNSWIndex::oneHopSearch): the visited bitmap was sized from TableStats::getTableCard(), an estimated (possibly stale) cardinality, and then indexed without bounds checks by graph neighbor offsets.

Changes:

  • initQueryHNSWSharedState now sizes the bitmap from the exact NodeTable::getNumTotalRows() instead of the estimated table cardinality.
  • VisitedState is now bounds-safe and auto-growing: contains() returns false for out-of-range offsets, add() grows the bitmap geometrically while preserving existing marks.
  • initLayerSearchState proactively resizes the bitmap to the current row count, covering table growth between query init and search.

The visited bitmap was sized from TableStats::getTableCard(), an
estimated (possibly stale) cardinality, and indexed without bounds
checks by graph neighbor offsets in oneHopSearch and related paths.
When the estimate was smaller than the actual max node offset this
caused an ASan heap-buffer-overflow READ in
VisitedState::contains (LadybugDB/ladybug#940).

- Size the bitmap from the exact NodeTable::getNumTotalRows() instead
  of the estimated table cardinality.
- Make VisitedState bounds-safe and auto-growing: contains() returns
  false for out-of-range offsets, add() grows geometrically while
  preserving existing marks.
- Proactively resize the bitmap to the current row count in
  initLayerSearchState to cover growth between query init and search.
@adsharma
adsharma merged commit 7824ead into main Sep 9, 2026
2 checks passed
@adsharma
adsharma deleted the fix-hnsw-visited-overflow branch September 9, 2026 16:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant