cayleygraph/cayley · error

varint: overflow

Error message

varint: overflow

What it means

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'.

Source

Thrown at graph/kv/indexing.go:768

			continue
		}
		ind, err := decodeIndex(v)
		if err != nil {
			return out, err
		}
		out[i] = ind
	}
	return out, nil
}

func countIndex(b []byte) (int64, error) {
	var cnt int64
	for len(b) > 0 {
		_, n := binary.Uvarint(b)
		if n == 0 {
			return 0, io.ErrUnexpectedEOF
		} else if n < 0 {
			return 0, errors.New("varint: overflow")
		}
		cnt++
		b = b[n:]
	}
	return cnt, nil
}

func decodeIndex(b []byte) ([]uint64, error) {
	var out []uint64
	for len(b) > 0 {
		v, n := binary.Uvarint(b)
		if n == 0 {
			return out, io.ErrUnexpectedEOF
		} else if n < 0 {
			return out, errors.New("varint: overflow")
		}
		out = append(out, v)
		b = b[n:]

View on GitHub (pinned to 81dcd7d73e)

Solutions

  1. Verify/repair the underlying kv database; the index blob is likely corrupt.
  2. Rebuild affected indexes (drop and re-run the indexer) so the blob is rewritten from quad data.
  3. Check that the reading process and the data were written by compatible cayleygraph versions.

Example fix

// before
cnt, err := countIndex(b) // fails: corrupt varint
// after
if err != nil && strings.Contains(err.Error(), "varint: overflow") {
    log.Warn("corrupt index blob, rebuilding")
    err = rebuildIndexes(ctx, bucket)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if len(b) == 0 { return 0, nil } // avoid decoding empty blobs

Try / catch

cnt, err := countIndex(b)
if err != nil {
    if strings.Contains(err.Error(), "varint: overflow") || err == io.ErrUnexpectedEOF {
        return 0, fmt.Errorf("index blob corrupt, rebuild required: %w", err)
    }
    return 0, err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of cayleygraph/cayley@81dcd7d73e (2026-09-06). Data as JSON: /api/errors/680b45e2ca119abd. Report an issue: GitHub.