lionsoul2014/ip2region · warning · InetAddressException

invalid ip address `${ip}`

Error message

invalid ip address `${ip}`

What it means

Util.parseIP delegates to InetAddress.getByName and wraps UnknownHostException into InetAddressException. It throws when the string is not a resolvable literal IP address (IPv4 or IPv6) — note getByName also performs DNS lookup for hostnames, so unresolvable names also fail here.

Source

Thrown at binding/java/src/main/java/org/lionsoul/ip2region/xdb/Util.java:22

//
// @Author Lion <chenxin619315@gmail.com>
// @Date   2022/07/14

package org.lionsoul.ip2region.xdb;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class Util
{

    // parse the specified IP address and return its bytes.
    // returns: byte[4] for IPv4 and byte[16] for IPv6 and the bytes should be in Big endian order.
    public static byte[] parseIP(String ip) throws InetAddressException {
        try {
            return InetAddress.getByName(ip).getAddress();
        } catch (UnknownHostException e) {
            throw new InetAddressException("invalid ip address `"+ip+"`");
        }
    }

    // convert the byte[] ip to string ip address
    public static String ipToString(final byte[] ip) {
        if (ip.length != 4 && ip.length != 16) {
            return String.format("invalid-ip-address-length: %d", ip.length);
        }

        try {
            return InetAddress.getByAddress(ip).getHostAddress();
        } catch (UnknownHostException e) {
            return String.format("invalid-ip-address `%s`", ipJoin(ip));
        }
    }

    // implode the byte[] ip with its byte value.
    public static String ipJoin(byte[] ip) {

View on GitHub (pinned to c1a1fc7d59)

Solutions

  1. Validate/trim the input and ensure it is a literal IPv4/IPv6 address before calling parseIP
  2. Strip port/CIDR suffixes and surrounding whitespace from the string first
  3. Catch InetAddressException at the call site and treat it as invalid user input
  4. Prefer a strict regex or validation library to reject hostnames if DNS resolution is unwanted

Example fix

// before
String r = searcher.search(request.getParameter("ip")); // "1.2.3.4/24"
// after
String ip = request.getParameter("ip").trim().split("/")[0];
try {
    String r = searcher.search(Util.parseIP(ip));
} catch (InetAddressException e) { /* invalid ip input */ }
Defensive patterns

Strategy: try-catch

Validate before calling

private static final Pattern IP_PATTERN = Pattern.compile("^((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d)$|^([0-9a-fA-F:]+)$");
boolean valid = ip != null && IP_PATTERN.matcher(ip.trim()).matches();

Type guard

static boolean isLiteralIp(String s) {
    if (s == null) return false;
    try { return Util.parseIP(s.trim()).length == 4 || Util.parseIP(s.trim()).length == 16; }
    catch (Exception e) { return false; }
}

Try / catch

try {
    byte[] ip = Util.parseIP(input.trim());
    return searcher.search(ip);
} catch (InetAddressException e) {
    log.warn("invalid ip input: {}", input);
    return null;
}

Prevention

When it happens

Trigger: Passing a malformed string to Util.parseIP (used by Searcher.search(String)): e.g. "256.1.1.1", "", "localhost" without DNS, whitespace, or a CIDR like "1.2.3.0/24".

Common situations: User-supplied IP inputs (HTTP params, logs) not sanitized before lookup; reading IP fields from CSVs with quotes/spaces; accidentally passing hostnames expecting resolution.

Related errors


AI-assisted analysis of lionsoul2014/ip2region@c1a1fc7d59 (2026-09-02). Data as JSON: /api/errors/9343b78e85faf314. Report an issue: GitHub.