{"record":{"id":"145c530ba99631cf","repo":"lionsoul2014/ip2region","slug":"invalid-byte-ip-address-with-length-ipbytes-leng","errorCode":null,"errorMessage":"invalid byte ip address with length=${ipBytes.length}","messagePattern":"invalid byte ip address with length=(.+?)","errorType":"validation","errorClass":"InetAddressException","httpStatus":null,"severity":"error","filePath":"binding/java/src/main/java/org/lionsoul/ip2region/service/Ip2Region.java","lineNumber":123,"sourceCode":"\n        if (v6Pool != null) {\n            v6Pool.init();\n        }\n\n        return this;\n    }\n\n    public String search(String ipString) throws InetAddressException, IOException, InterruptedException {\n        return search(Util.parseIP(ipString));\n    }\n\n    public String search(byte[] ipBytes) throws InetAddressException, IOException, InterruptedException {\n        if (ipBytes.length == 4) {\n            return v4Search(ipBytes);\n        } else if (ipBytes.length == 16) {\n            return v6Search(ipBytes);\n        } else {\n            throw new InetAddressException(\"invalid byte ip address with length=\" + ipBytes.length);\n        }\n    }\n\n    protected String v4Search(final byte[] ipBytes) throws IOException, InetAddressException, InterruptedException {\n        if (v4InMemSearcher != null) {\n            return v4InMemSearcher.search(ipBytes);\n        }\n\n        // IPv4 search is disabled\n        if (v4Pool == null) {\n            return null;\n        }\n\n        final Searcher searcher = v4Pool.borrowSearcher();\n        try {\n            return searcher.search(ipBytes);\n        } finally {\n            v4Pool.returnSearcher(searcher);","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/java/src/main/java/org/lionsoul/ip2region/service/Ip2Region.java#L105-L141","documentation":"Ip2Region.search(byte[]) dispatches by array length: 4 bytes for IPv4 and 16 bytes for IPv6. Any other length means the byte array is not a valid raw IP address, so an InetAddressException is thrown instead of guessing a version.","triggerScenarios":"Passing a byte[] whose length is neither 4 nor 16 to Ip2Region.search — e.g. a 0-length array, a truncated buffer, or 6 bytes (a MAC address) mistakenly used as an IP.","commonSituations":"Decoding IPs from binary logs/packets with wrong offsets; passing UTF-8 bytes of a dotted string instead of parsed address bytes; mixing IPv4/IPv6 parsing helpers.","solutions":["Parse string IPs with Util.parseIP(ip) to get a correctly sized byte[4]/byte[16] before calling search","Verify ipBytes.length is 4 or 16 before calling search","If you have an InetAddress, use its getAddress() output directly"],"exampleFix":"// before\nString region = ip2Region.search(rawBytes); // rawBytes.length == 6\n// after\nbyte[] ipBytes = Util.parseIP(\"1.2.3.4\"); // byte[4] or byte[16]\nString region = ip2Region.search(ipBytes);","handlingStrategy":"validation","validationCode":"if (ipBytes == null || (ipBytes.length != 4 && ipBytes.length != 16)) {\n    throw new IllegalArgumentException(\"expected byte[4] or byte[16] ip, got \" + (ipBytes == null ? \"null\" : ipBytes.length));\n}","typeGuard":"static boolean isValidIpBytes(byte[] b) {\n    return b != null && (b.length == 4 || b.length == 16);\n}","tryCatchPattern":"try {\n    return ip2Region.search(ipBytes);\n} catch (InetAddressException e) {\n    log.warn(\"bad ip bytes: {}\", Arrays.toString(ipBytes));\n    return null;\n}","preventionTips":["Produce ip bytes only via Util.parseIP or InetAddress.getAddress()","Never pass raw string bytes (getBytes()) as an IP","Dispatch on length 4 vs 16 before calling version-specific searchers"],"tags":["java","ip-address","input-validation"],"backgroundTag":"invalid-ip-address","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}