{"record":{"id":"680b45e2ca119abd","repo":"cayleygraph/cayley","slug":"varint-overflow","errorCode":null,"errorMessage":"varint: overflow","messagePattern":"varint: overflow","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"graph/kv/indexing.go","lineNumber":768,"sourceCode":"\t\t\tcontinue\n\t\t}\n\t\tind, err := decodeIndex(v)\n\t\tif err != nil {\n\t\t\treturn out, err\n\t\t}\n\t\tout[i] = ind\n\t}\n\treturn out, nil\n}\n\nfunc countIndex(b []byte) (int64, error) {\n\tvar cnt int64\n\tfor len(b) > 0 {\n\t\t_, n := binary.Uvarint(b)\n\t\tif n == 0 {\n\t\t\treturn 0, io.ErrUnexpectedEOF\n\t\t} else if n < 0 {\n\t\t\treturn 0, errors.New(\"varint: overflow\")\n\t\t}\n\t\tcnt++\n\t\tb = b[n:]\n\t}\n\treturn cnt, nil\n}\n\nfunc decodeIndex(b []byte) ([]uint64, error) {\n\tvar out []uint64\n\tfor len(b) > 0 {\n\t\tv, n := binary.Uvarint(b)\n\t\tif n == 0 {\n\t\t\treturn out, io.ErrUnexpectedEOF\n\t\t} else if n < 0 {\n\t\t\treturn out, errors.New(\"varint: overflow\")\n\t\t}\n\t\tout = append(out, v)\n\t\tb = b[n:]","sourceCodeStart":750,"sourceCodeEnd":786,"githubUrl":"https://github.com/cayleygraph/cayley/blob/81dcd7d73e45136bc0d01802a8ba4685d8a533eb/graph/kv/indexing.go#L750-L786","documentation":"countIndex decodes a stored index blob as a sequence of uvarint-encoded counts, one per entry. binary.Uvarint returning n < 0 means the encoded integer does not fit in 64 bits (malformed or corrupted index data), so counting aborts with 'varint: overflow'.","triggerScenarios":"Calling countIndex on index bytes where one uvarint is encoded with more bytes than 64 bits can represent, or the blob is corrupt/truncated in a way that yields an overlong varint. Reached via getBucketIndexes during index listing/counting.","commonSituations":"Corrupted or hand-edited database files, writes from an incompatible cayleygraph version, bit-flip corruption on disk, or decoding a byte slice that is not actually a cayley index list.","solutions":["Verify/repair the underlying kv database; the index blob is likely corrupt.","Rebuild affected indexes (drop and re-run the indexer) so the blob is rewritten from quad data.","Check that the reading process and the data were written by compatible cayleygraph versions."],"exampleFix":"// before\ncnt, err := countIndex(b) // fails: corrupt varint\n// after\nif err != nil && strings.Contains(err.Error(), \"varint: overflow\") {\n    log.Warn(\"corrupt index blob, rebuilding\")\n    err = rebuildIndexes(ctx, bucket)\n}","handlingStrategy":"try-catch","validationCode":"if len(b) == 0 { return 0, nil } // avoid decoding empty blobs","typeGuard":null,"tryCatchPattern":"cnt, err := countIndex(b)\nif err != nil {\n    if strings.Contains(err.Error(), \"varint: overflow\") || err == io.ErrUnexpectedEOF {\n        return 0, fmt.Errorf(\"index blob corrupt, rebuild required: %w\", err)\n    }\n    return 0, err\n}","preventionTips":["Never hand-edit kv database files.","Keep cayleygraph versions consistent between writers and readers.","Enable periodic integrity checks/backups of the database directory."],"tags":["go","encoding","corruption","varint"],"backgroundTag":"corrupt-index-data","analyzedSha":"81dcd7d73e45136bc0d01802a8ba4685d8a533eb","analyzedAt":"2026-09-06T06:14:12.358Z","contentChangedAt":"2026-09-06T06:14:12.358Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}