lionsoul2014/ip2region · error
invalid ip address: %s
Error message
invalid ip address: %s
What it means
ParseIP validates its input with net.ParseIP; when the string cannot be parsed as an IP at all, it returns this error. It is the entry-point validation for all IP strings passed into search APIs and benchmarks.
Source
Thrown at binding/golang/xdb/util.go:23
// ---
// @Author Lion <chenxin619315@gmail.com>
// @Date 2022/06/16
package xdb
import (
"bytes"
"embed"
"fmt"
"io"
"net"
"os"
)
func ParseIP(ip string) ([]byte, error) {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return nil, fmt.Errorf("invalid ip address: %s", ip)
}
v4 := parsedIP.To4()
if v4 != nil {
return v4, nil
}
v6 := parsedIP.To16()
if v6 != nil {
return v6, nil
}
return nil, fmt.Errorf("invalid ip address: %s", ip)
}
func IP2String(ip []byte) string {
return net.IP(ip[:]).String()
}View on GitHub (pinned to c1a1fc7d59)
Solutions
- Validate/trim the input string before calling ParseIP; strip any port or CIDR suffix
- Resolve hostnames to IPs first with net.LookupHost before searching
- Check that the value from config/env is non-empty and matches an IPv4/IPv6 pattern
- Use net.ParseIP yourself for an early check and better error messages
Example fix
// before
ip, err := xdb.ParseIP("example.com:80") // invalid ip address
// after
host, _, _ := net.SplitHostPort("example.com:80")
addrs, err := net.LookupHost(host)
ip, err = xdb.ParseIP(addrs[0]) Defensive patterns
Strategy: validation
Validate before calling
func validIP(s string) bool {
s = strings.TrimSpace(s)
if i := strings.LastIndexByte(s, ':'); i > strings.LastIndexByte(s, ']') && net.ParseIP(s) == nil {
// may contain a port
if h, _, err := net.SplitHostPort(s); err == nil {
return net.ParseIP(h) != nil
}
}
return net.ParseIP(s) != nil
} Type guard
func isIPString(s string) bool {
return net.ParseIP(strings.TrimSpace(s)) != nil
} Try / catch
ip, err := xdb.ParseIP(input)
if err != nil {
return fmt.Errorf("cannot search %q: %w", input, err)
} Prevention
- Trim and strip ports/CIDR suffixes from user-supplied strings before ParseIP
- Resolve hostnames with net.LookupHost instead of passing them to search
- Validate config/env IP values at startup
- Log the raw offending value in the error to speed triage
When it happens
Trigger: Calling xdb.ParseIP("not-an-ip") or Search with a hostname (e.g. "example.com"), an empty string, an IP with a port ("1.2.3.4:80"), or whitespace-padded input.
Common situations: Users passing hostnames instead of IPs from logs or webhooks; including ports or CIDR suffixes from config files; empty/blank values from environment variables; leading/trailing spaces in CSV inputs.
Related errors
- invalid byte ip address with length=${ipBytes.length}
- invalid ip address `${ip}`
- invalid ip address ({$this->version->name} expected)
- invalid ip address `{}`
- length of the two ips are not the same
AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02).
Data as JSON: /api/errors/e011d53c86cb9093.
Report an issue: GitHub.