lionsoul2014/ip2region · error

invalid byte ip address with len=%d

Error message

invalid byte ip address with len=%d

What it means

Go service Search: the supplied []byte IP is neither 4 bytes (IPv4) nor 16 bytes (IPv6), so its family cannot be routed to a searcher; the length is echoed in the message.

Source

Thrown at binding/golang/service/ip2region.go:141

	var ipBytes []byte
	switch v := ip.(type) {
	case string:
		ipBytes, err = xdb.ParseIP(v)
		if err != nil {
			return "", fmt.Errorf("parse ip %s: %w", v, err)
		}
	case []byte:
		ipBytes = v
	default:
		return "", fmt.Errorf("invalid ip value type %s", v)
	}

	if l := len(ipBytes); l == 4 {
		return ip2r.v4Search(ipBytes)
	} else if l == 16 {
		return ip2r.v6Search(ipBytes)
	} else {
		return "", fmt.Errorf("invalid byte ip address with len=%d", l)
	}
}

func (ip2r *Ip2Region) v4Search(ipBytes []byte) (string, error) {
	if ip2r.v4InMemSearcher != nil {
		return ip2r.v4InMemSearcher.Search(ipBytes)
	}

	// v4 search is disabled
	if ip2r.v4Pool == nil {
		return "", nil
	}

	v4Searcher := ip2r.v4Pool.BorrowSearcher()
	if v4Searcher == nil {
		return "", fmt.Errorf("v4 searcher pool already closed")
	}
	defer ip2r.v4Pool.ReturnSearcher(v4Searcher)

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Pass exactly 4 bytes for IPv4 or 16 bytes for IPv6
  2. Use net.IP.To4()/To16() to normalize byte slices before searching
  3. Validate len(ip) at the call site
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at binding/golang/service/ip2region.go:141 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/e14c41d2860f0e7a. Report an issue: GitHub.