fix: reject invalid graphs in Import instead of panicking later - #25
Open
Tyagiquamar wants to merge 1 commit into
Open
fix: reject invalid graphs in Import instead of panicking later#25Tyagiquamar wants to merge 1 commit into
Tyagiquamar wants to merge 1 commit into
Conversation
Import trusted the encoded payload structurally: - a neighbor key absent from its layer was stored as a nil *layerNode, so the first Search over the imported graph panicked with a nil pointer dereference - vectors of mismatched dimensionality were accepted (within one file, or against vectors already in the graph), making Search panic inside the distance function depending on which node became the entry point - negative layer/node/neighbor/vector counts reached make() and panicked during decoding All four are rejected with errors now, matching the existing version and distance-function validation. Valid Export output never triggers any of the new checks: neighbors are always intra-layer and assertDims keeps vector dimensions uniform.
Author
|
Hi @ammario, gentle review ping when you have a chance. This is a focused Import validation fix with regression coverage, and the full Go test suite passes. Happy to adjust anything you'd prefer. |
Author
|
Hi @suykerbuyk, could you please review when you have a chance? Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Graph.Importtrusted the encoded payload structurally. Three classes of malformed input decoded successfully and produced a graph that panics later, far from the cause:1. Dangling neighbor reference -> nil-pointer panic on Search
A node whose neighbor list references a key absent from its layer got a
nil*layerNodestored in its neighbor map (encode.go filled pointers withnodes[key], which silently yieldsnilon a miss). The firstSearchover the graph panics:Reproduced against current main by importing a hand-crafted payload (one layer, one node, one neighbor key not present) and then searching.
2. Inconsistent vector dimensionality -> panic inside the distance function
Import enforced no dimensionality constraint, although the doc comment carves out dimensionality as the one thing that must match, and
assertDimsenforces uniformity forAdd. A file containing vectors of different lengths imports cleanly; afterwardsSearchpanics inside vek32 depending on which node map iteration picks as entry point:The same applies to importing a differently-dimensional file into a populated graph.
3. Negative counts -> panic during decoding
Negative layer/node/neighbor/vector-length varints reach
make(...)(make([]*layer[K], -1),make([]K, -3),make([]float32, -4)) and panic insideImportinstead of returning an error.Fix
Validate while decoding and return errors, consistent with the existing version / distance-function checks:
node %v has neighbor %v, but it is not present in its layer)makebinaryReadValid
Exportoutput never trips any of the new checks: neighbors are always intra-layer by construction, andassertDimskeeps dimensions uniform across a graph.Verification
New tests (
encode_test.go), all passing locally on Go 1.26:TestGraph_ImportRejectsDanglingNeighbor- failed with the nil-pointer panic above on unpatched main; nowImporterrorsTestGraph_ImportRejectsInconsistentDims/WithinFile- panicked in vek32 on unpatched main; now errorsTestGraph_ImportRejectsInconsistentDims/AgainstGraph- same for import into a populated graphTestGraph_ImportRejectsInvalidCounts/{negativeLayers,negativeNodes,negativeNeighbors,negativeVectorLength}- panicked inmakebefore; now errorgo test ./..., includesTestGraph_ExportImportroundtrip andTestSavedGraph) passes unchangedPerformance (
BenchmarkGraph_Import, 100 nodes x 256 dims): allocations are byte-identical before/after (3016 allocs/op, ~491 KiB/op). Timings on my noisy Windows laptop were inconclusive run-to-run, but the change adds only anokcheck to a map lookup that already existed plus integer comparisons per node - no new allocations or passes over the data.