Repository navigation
Conversation
checkVectorIsSane already rejects non-finite vector elements and, for i8 quantization, zero-magnitude vectors. It did not reject vectors whose squared magnitude overflows to infinity. Such a vector (e.g. an element around 1e300) has finite elements and a non-zero magnitude, so it passed the guard, but usearch's cast_to_i8_gt scales each element by 127 / sqrt(magnitude_squared); with an infinite magnitude the scaled value becomes nan and the subsequent static_cast<int8_t>(nan) is undefined behavior (UBSan: nan is outside the range of representable values of type 'signed char', index_plugins.hpp:1216). Reject non-finite magnitude_squared for i8 in the same place, covering both the reference vector (search) and the indexed vector (insert) paths. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-1:20260720-141400 |
Internal second-model review (click to expand)Independent second-model (codex) review of the diff before opening this PR. Result: 0 findings on the final fix diff (2 files, +31/-2). ❌ Blockers: none.
💡 Nits: none. |
|
cc @rschu1ze — follow-up to #104079: |
|
Workflow [PR], commit [fbab68b] Summary: ✅
AI ReviewSummaryThis PR closes the obvious forward Findings❌ Blockers
Tests
Final VerdictStatus: ❌ Block |
CI finish ledger — 0f66431CI finished on this head. None of the reds relate to this PR's change (the usearch i8 magnitude-overflow guard in
Session id: cron:our-pr-ci-monitor:20260721-013000 |
…overflow-magnitude-8146-795a
| -- There are other zero-magnitude vectors, but we can't test the other ones reliably because of rounding errors | ||
| -- There are other zero-magnitude squared vectors, but we can't test the other ones reliably because of rounding errors | ||
| INSERT INTO tab VALUES (12, [0.0, 0.0, 0.0]); -- { serverError INCORRECT_DATA } | ||
| INSERT INTO tab VALUES (12, [1e300, 1.0, 1.0]); -- { serverError INCORRECT_DATA } |
There was a problem hiding this comment.
This insert does not actually exercise the new overflowed-magnitude_squared path on the indexed-vector side. updateImpl validates the materialized column value, and this table is Array(Float32), so by the time checkVectorIsSane runs the 1e300 literal has already narrowed to a non-finite Float32 and is rejected by the pre-existing per-element !std::isfinite(casted) check. The bug fixed here on inserts only exists for Array(Float64), where a finite value can survive input conversion and overflow only when magnitude_squared is accumulated. Can we move the insert-side case to a separate Array(Float64) table so the new branch is actually covered?
…overflow-magnitude-8146-795a
CI finish ledger - b06e7ccAll four reds are unrelated trunk/infra failures with owners; none is caused by this PR (its diff touches only
Session id: cron:our-pr-ci-monitor:20260723-213000 |
…overflow-magnitude-8146-795a
CI finish ledger - c51fc91Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
The Fast-test red is the arm_darwin runner losing the server, not this PR's diff: Session id: cron:our-pr-ci-monitor:20260726-163000 |
…overflow-magnitude-8146-795a
|
Owner update for the That failure is a hang on the macOS native runner, not a fault in this pull request's diff. It was No fixing pull request exists yet, and I am deliberately not opening a speculative one: the stuck |
CI finish ledger — 5ada62eEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task This head is a plain master merge on top of the previously ledgered
Session id: cron:our-pr-ci-monitor:20260727-043000 |
…overflow-magnitude-8146-795a
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code |
CI finish ledger - fbab68bCI has fully finished on this head with no failures: 174 check-runs, all Head Session id: cron:our-pr-ci-monitor:20260728-163000 |
Resolves #111621
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Reject vectors whose squared magnitude overflows to infinity when using a
vector_similarityindex withi8quantization (previously such vectors triggered undefined behavior in usearch and produced silent garbage).Description
Follow-up to #104079, which added
checkVectorIsSaneto reject non-finite elements and, fori8quantization, zero-magnitude vectors. That guard missed one more class of undefined behavior.usearch'scast_to_i8_gt(index_plugins.hpp:1216) quantizes a vector by scaling each element by127 / sqrt(magnitude_squared). A vector with finite elements but a very large one (e.g. an element around1e300) has a squared magnitude that overflows to+inf, soelement * 127 / infbecomesnan, and the subsequentstatic_cast<std::int8_t>(nan)is undefined behavior:Such a vector has finite elements and a non-zero magnitude, so it passed
checkVectorIsSaneand reached thei8cast both as a reference vector (MergeTreeIndexConditionVectorSimilarity::calculateApproximateNearestNeighbors->index->search) and as an indexed vector (updateImpl). In non-sanitizer builds this silently indexes/searches with implementation-definedint8values.Fix: reject non-finite
magnitude_squaredfori8incheckVectorIsSane, next to the existing zero-magnitude check. This covers both the search and insert paths in one place. Non-i8quantizations are unaffected (they do not divide by the magnitude).Found by the AST fuzzer, STID 8146-795a,
Stress test (arm_asan_ubsan):https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109261&sha=e532419f9209dd0e350462cfcc698dcbe4c1f31d&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29
Test: extended
02354_vector_search_undefined_behavior.sqlwith overflow-magnitude cases for both the reference vector and the indexed vector (the latter needs anArray(Float64)column, sinceFloat32/BFloat16elements are too small to overflow theFloat64sum of squares). The queries fail on master (succeed silently) and pass with the fix.Version info
26.8.1.291(included in26.8and later)