dgraph-io/badger · error

Invalid range: %s

Error message

Invalid range: %s

What it means

parseIgnoreBytes parses the IgnoreBytes field of a pb.Match (a comma-separated list of byte indices, optionally with dash ranges, e.g. "3, 5-8") into a boolean mask. This error is returned when a comma-separated element splits into more than two dash-separated parts (e.g. "1-2-3") or an empty element produces zero parts, so it cannot be interpreted as a single index or a start-end range.

Source

Thrown at trie/trie.go:57

// NewTrie returns Trie.
func NewTrie() *Trie {
	return &Trie{
		root: newNode(),
	}
}

// parseIgnoreBytes would parse the ignore string, and convert it into a list of bools, where
// bool[idx] = true implies that key[idx] can be ignored during comparison.
func parseIgnoreBytes(ig string) ([]bool, error) {
	var out []bool
	if ig == "" {
		return out, nil
	}

	for _, each := range strings.Split(strings.TrimSpace(ig), ",") {
		r := strings.Split(strings.TrimSpace(each), "-")
		if len(r) == 0 || len(r) > 2 {
			return out, fmt.Errorf("Invalid range: %s", each)
		}
		start, end := -1, -1 //nolint:ineffassign
		if len(r) == 2 {
			idx, err := strconv.Atoi(strings.TrimSpace(r[1]))
			if err != nil {
				return out, err
			}
			end = idx
		}
		{
			// Always consider r[0]
			idx, err := strconv.Atoi(strings.TrimSpace(r[0]))
			if err != nil {
				return out, err
			}
			start = idx
		}
		if start == -1 {

View on GitHub (pinned to 2a001d466f)

Solutions

  1. Fix the IgnoreBytes string so each comma-separated element is either a single index ("3") or exactly one dash range ("5-8").
  2. Replace multiple dashes in a range spec with commas for individual indices, e.g. "1-2-3" becomes "1-2,3" or "1,2,3".
  3. Trim stray separators: build the string with strings.Join(indices, ",") instead of manual concatenation to avoid empty or malformed elements.
  4. Validate the format with a regex like ^\d+(-\d+)?$ per element before calling AddMatch.

Example fix

// before
m := pb.Match{Prefix: []byte("aaaa"), IgnoreBytes: "0-1-2"}
trie.AddMatch(m, 1) // Invalid range: 0-1-2
// after
m := pb.Match{Prefix: []byte("aaaa"), IgnoreBytes: "0-1,2"}
trie.AddMatch(m, 1)
Defensive patterns

Strategy: validation

Validate before calling

func validIgnoreBytes(ig string) bool {
	if ig == "" { return true }
	for _, each := range strings.Split(ig, ",") {
		parts := strings.Split(strings.TrimSpace(each), "-")
		if len(parts) < 1 || len(parts) > 2 { return false }
	}
	return true
}
// if !validIgnoreBytes(m.IgnoreBytes) { /* reject before AddMatch */ }

Prevention

When it happens

Trigger: Calling Trie.AddMatch (or DeleteMatch, which both go through fix) with m.IgnoreBytes containing an element with two or more dashes such as "0-1-2", or an element like ",," that trims to an empty string yielding zero split parts.

Common situations: Hand-written IgnoreBytes strings with typos, programmatically generated range strings that were formatted with more than one dash, or concatenating ranges with dashes instead of commas. Note: an element like "abc" (non-numeric) does NOT hit this line; it fails later at strconv.Atoi.

Related errors


AI-assisted analysis of dgraph-io/badger@2a001d466f (2026-09-05). Data as JSON: /api/errors/4abaa543d8fc0263. Report an issue: GitHub.