{"record":{"id":"d44425e9f10a6baf","repo":"lionsoul2014/ip2region","slug":"invalid-ip-address-s-expected","errorCode":null,"errorMessage":"invalid ip address(%s expected)","messagePattern":"invalid ip address\\((.+?) expected\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/searcher.go","lineNumber":128,"sourceCode":"// Search the region for the specified string or bytes ip address\nfunc (s *Searcher) Search(ip any) (string, error) {\n\tvar err error\n\tvar ipBytes []byte\n\tswitch v := ip.(type) {\n\tcase string:\n\t\tipBytes, err = ParseIP(v)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"parse ip %s: %w\", v, err)\n\t\t}\n\tcase []byte:\n\t\tipBytes = v\n\tdefault:\n\t\treturn \"\", fmt.Errorf(\"invalid ip value type %s\", v)\n\t}\n\n\t// ip version check\n\tif len(ipBytes) != s.version.Bytes {\n\t\treturn \"\", fmt.Errorf(\"invalid ip address(%s expected)\", s.version.Name)\n\t}\n\n\t// reset the global ioCount\n\ts.ioCount = 0\n\n\t// locate the segment index block based on the vector index\n\tvar il0, il1 = int(ipBytes[0]), int(ipBytes[1])\n\tvar idx = il0*VectorIndexCols*VectorIndexSize + il1*VectorIndexSize\n\tvar sPtr, ePtr = uint32(0), uint32(0)\n\tif s.vectorIndex != nil {\n\t\tsPtr = binary.LittleEndian.Uint32(s.vectorIndex[idx:])\n\t\tePtr = binary.LittleEndian.Uint32(s.vectorIndex[idx+4:])\n\t} else if s.contentBuff != nil {\n\t\tsPtr = binary.LittleEndian.Uint32(s.contentBuff[HeaderInfoLength+idx:])\n\t\tePtr = binary.LittleEndian.Uint32(s.contentBuff[HeaderInfoLength+idx+4:])\n\t} else {\n\t\t// read the vector index block\n\t\tvar buff = make([]byte, VectorIndexSize)","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/searcher.go#L110-L146","documentation":"After parsing, Search verifies the byte length of the IP matches the searcher's xdb version (4 bytes for IPv4, 16 for IPv6). A mismatch — e.g. an IPv4 address searched against an IPv6 xdb — returns this error naming the expected version.","triggerScenarios":"Searching \"1.2.3.4\" against an xdb_v6.xdb (or an IPv6 literal against an xdb_v4.xdb); passing a 4-byte slice where 16 bytes are required, or vice versa; passing an unexpanded IPv4-mapped form with wrong length.","commonSituations":"Mixing v4 and v6 xdb files in one app and reusing one searcher for all inputs; log data mixing IPv4 and IPv6 addresses; using To4() output on an IPv6 searcher.","solutions":["Load the matching xdb file version for your input (xdb_v4 for IPv4 IPs, xdb_v6 for IPv6).","Detect the IP family first (len of net.ParseIP(ip).To4()==4) and route to the appropriate searcher.","Normalize with To4()/To16() so the byte length always matches the searcher's version.","Use IPVersion from the header (via LoadHeader) to pick the right searcher automatically."],"exampleFix":"// before\nregion, err := v6Searcher.Search(ctx, \"1.2.3.4\") // wrong family\n// after\nip := net.ParseIP(\"1.2.3.4\")\nif ip.To4() != nil {\n    region, err = v4Searcher.Search(ctx, ip.String())\n} else {\n    region, err = v6Searcher.Search(ctx, ip.String())\n}","handlingStrategy":"validation","validationCode":"ip := net.ParseIP(raw)\nipBytes := ip.To4()\nif ipBytes == nil { ipBytes = ip.To16() }\nwant := 16\nif header.IPVersion == xdb.IPv4 { want = 4 }\nif len(ipBytes) != want {\n    return fmt.Errorf(\"ip family mismatch: got %d bytes, xdb expects %d\", len(ipBytes), want)\n}","typeGuard":"func matchesXDBVersion(raw string, ver xdb.IPVersion) bool {\n    ip := net.ParseIP(raw)\n    if ip == nil { return false }\n    if ver == xdb.IPv4 { return ip.To4() != nil }\n    return ip.To4() == nil\n}","tryCatchPattern":"region, err := searcher.Search(ctx, raw)\nif err != nil && strings.Contains(err.Error(), \"invalid ip address(\") {\n    // route to the other-version searcher or drop the record\n    return handleVersionMismatch(raw, err)\n}","preventionTips":["Load both xdb_v4 and xdb_v6 and route by IP family.","Check the header's IPVersion at startup and log the expected family.","Normalize with To4()/To16() before searching by bytes.","Test with both an IPv4 and IPv6 sample in CI."],"tags":["golang","ip-version","mismatch","ipv6"],"backgroundTag":"ip-version-mismatch","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}