{"record":{"id":"e011d53c86cb9093","repo":"lionsoul2014/ip2region","slug":"invalid-ip-address-s","errorCode":null,"errorMessage":"invalid ip address: %s","messagePattern":"invalid ip address: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/util.go","lineNumber":23,"sourceCode":"// ---\n// @Author Lion <chenxin619315@gmail.com>\n// @Date   2022/06/16\n\npackage xdb\n\nimport (\n\t\"bytes\"\n\t\"embed\"\n\t\"fmt\"\n\t\"io\"\n\t\"net\"\n\t\"os\"\n)\n\nfunc ParseIP(ip string) ([]byte, error) {\n\tparsedIP := net.ParseIP(ip)\n\tif parsedIP == nil {\n\t\treturn nil, fmt.Errorf(\"invalid ip address: %s\", ip)\n\t}\n\n\tv4 := parsedIP.To4()\n\tif v4 != nil {\n\t\treturn v4, nil\n\t}\n\n\tv6 := parsedIP.To16()\n\tif v6 != nil {\n\t\treturn v6, nil\n\t}\n\n\treturn nil, fmt.Errorf(\"invalid ip address: %s\", ip)\n}\n\nfunc IP2String(ip []byte) string {\n\treturn net.IP(ip[:]).String()\n}","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/util.go#L5-L41","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nip, err := xdb.ParseIP(\"example.com:80\") // invalid ip address\n// after\nhost, _, _ := net.SplitHostPort(\"example.com:80\")\naddrs, err := net.LookupHost(host)\nip, err = xdb.ParseIP(addrs[0])","handlingStrategy":"validation","validationCode":"func validIP(s string) bool {\n    s = strings.TrimSpace(s)\n    if i := strings.LastIndexByte(s, ':'); i > strings.LastIndexByte(s, ']') && net.ParseIP(s) == nil {\n        // may contain a port\n        if h, _, err := net.SplitHostPort(s); err == nil {\n            return net.ParseIP(h) != nil\n        }\n    }\n    return net.ParseIP(s) != nil\n}","typeGuard":"func isIPString(s string) bool {\n    return net.ParseIP(strings.TrimSpace(s)) != nil\n}","tryCatchPattern":"ip, err := xdb.ParseIP(input)\nif err != nil {\n    return fmt.Errorf(\"cannot search %q: %w\", input, err)\n}","preventionTips":["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"],"tags":["go","input-validation","ip-address"],"backgroundTag":"invalid-ip-address","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}