lionsoul2014/ip2region · error · InetAddressException
invalid byte ip address with length=${ipBytes.length}
Error message
invalid byte ip address with length=${ipBytes.length} What it means
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.
Source
Thrown at binding/java/src/main/java/org/lionsoul/ip2region/service/Ip2Region.java:123
if (v6Pool != null) {
v6Pool.init();
}
return this;
}
public String search(String ipString) throws InetAddressException, IOException, InterruptedException {
return search(Util.parseIP(ipString));
}
public String search(byte[] ipBytes) throws InetAddressException, IOException, InterruptedException {
if (ipBytes.length == 4) {
return v4Search(ipBytes);
} else if (ipBytes.length == 16) {
return v6Search(ipBytes);
} else {
throw new InetAddressException("invalid byte ip address with length=" + ipBytes.length);
}
}
protected String v4Search(final byte[] ipBytes) throws IOException, InetAddressException, InterruptedException {
if (v4InMemSearcher != null) {
return v4InMemSearcher.search(ipBytes);
}
// IPv4 search is disabled
if (v4Pool == null) {
return null;
}
final Searcher searcher = v4Pool.borrowSearcher();
try {
return searcher.search(ipBytes);
} finally {
v4Pool.returnSearcher(searcher);View on GitHub (pinned to c1a1fc7d59)
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
Example fix
// before
String region = ip2Region.search(rawBytes); // rawBytes.length == 6
// after
byte[] ipBytes = Util.parseIP("1.2.3.4"); // byte[4] or byte[16]
String region = ip2Region.search(ipBytes); Defensive patterns
Strategy: validation
Validate before calling
if (ipBytes == null || (ipBytes.length != 4 && ipBytes.length != 16)) {
throw new IllegalArgumentException("expected byte[4] or byte[16] ip, got " + (ipBytes == null ? "null" : ipBytes.length));
} Type guard
static boolean isValidIpBytes(byte[] b) {
return b != null && (b.length == 4 || b.length == 16);
} Try / catch
try {
return ip2Region.search(ipBytes);
} catch (InetAddressException e) {
log.warn("bad ip bytes: {}", Arrays.toString(ipBytes));
return null;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- invalid ip address `${ip}`
- invalid ip address (${version.name} expected)
- invalid ip address ({$this->version->name} expected)
- invalid ip address `{}`
- invalid ip address: %s
AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02).
Data as JSON: /api/errors/145c530ba99631cf.
Report an issue: GitHub.