grafana/k6 · error

negative IP range: {s}

Error message

negative IP range: {s}

What it means

Returned by ipBlockFromRange (lib/types/ipblock.go:54-56) when the range's start IP is strictly greater than its end IP, i.e. ipBlockFromTwoIPs computes a non-positive count (big.Int count.Sign() <= 0). Ranges are ordered: the block's size is end-start (+1), so a reversed range yields zero or negative capacity and is rejected instead of silently producing an empty block.

Source

Thrown at lib/types/ipblock.go:55

			return nil, fmt.Errorf("%s is not a valid IP, IP range or CIDR", s)
		}
		return ipBlockFromRange(s + "-" + s)
	}
}

func ipBlockFromRange(s string) (*ipBlock, error) {
	ip0Str, ip1Str, _ := strings.Cut(s, "-")
	ip0, ip1 := net.ParseIP(ip0Str), net.ParseIP(ip1Str)
	if ip0 == nil || ip1 == nil {
		return nil, errors.New("wrong IP range format: " + s)
	}
	if (ip0.To4() == nil) != (ip1.To4() == nil) { // XOR
		return nil, errors.New("mixed IP range format: " + s)
	}
	block := ipBlockFromTwoIPs(ip0, ip1)

	if block.count.Sign() <= 0 {
		return nil, errors.New("negative IP range: " + s)
	}
	return block, nil
}

func ipBlockFromTwoIPs(ip0, ip1 net.IP) *ipBlock {
	// This code doesn't do any checks on the validity of the arguments, that should be
	// done before and/or after it is called
	var block ipBlock
	block.firstIP = new(big.Int)
	block.count = new(big.Int)
	block.ipv6 = ip0.To4() == nil
	if block.ipv6 {
		block.firstIP.SetBytes(ip0.To16())
		block.count.SetBytes(ip1.To16())
	} else {
		block.firstIP.SetBytes(ip0.To4())
		block.count.SetBytes(ip1.To4())
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Swap the endpoints so the smaller IP comes first: '192.168.0.1-192.168.0.100'
  2. If generating programmatically, compare with ip0.Compare(ip1) (Go 1.22+) before formatting the range

Example fix

// before
block, err := getIPBlock("192.168.0.100-192.168.0.1")

// after
block, err := getIPBlock("192.168.0.1-192.168.0.100")
Defensive patterns

Strategy: validation

Validate before calling

// Go: order endpoints before building the range string
func orderedRange(a, b net.IP) string {
    if a.Compare(b) > 0 {
        a, b = b, a
    }
    return a.String() + "-" + b.String()
}

Prevention

When it happens

Trigger: Calling getIPBlock with '192.168.0.100-192.168.0.1' or '::ff-::1' — both endpoints valid, same family, but descending order.

Common situations: Sorting bugs in generated configs (start/end fields swapped); UIs or scripts that let users enter 'from'/'to' IPs without ordering validation.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/54fb9a9c061eb72c. Report an issue: GitHub.