alibaba/nacos · warning · IllegalArgumentException

{} is invalid IP

Error message

{} is invalid IP

What it means

Thrown by InternetAddressUtil.ipToInt(String) when the IP cannot be converted. ipToInt delegates to ipToBytesByInet (which uses InetAddress.getByName) and then bytesToInt; any exception during that chain is wrapped as IllegalArgumentException with the offending IP string in the message. Note: InetAddress.getByName performs DNS resolution, so a hostname (not an IP) can also land here if resolution yields an unexpected form.

Source

Thrown at common/src/main/java/com/alibaba/nacos/common/utils/InternetAddressUtil.java:259

            return false;
        }
        if (Objects.equals(str, LOCAL_HOST)) {
            return true;
        }
        return DOMAIN_PATTERN.matcher(str).matches();
    }
    
    /**
     * convert ip address to int.
     *
     * @param ip ip address.
     * @return int
     */
    public static int ipToInt(String ip) {
        try {
            return bytesToInt(ipToBytesByInet(ip));
        } catch (Exception e) {
            throw new IllegalArgumentException(ip + " is invalid IP");
        }
    }
    
    /**
     * convert byte array to int.
     *
     * @param bytes byte array.
     * @return int
     */
    public static int bytesToInt(byte[] bytes) {
        int addr = bytes[3] & 0xFF;
        addr |= ((bytes[2] << 8) & 0xFF00);
        addr |= ((bytes[1] << 16) & 0xFF0000);
        addr |= ((bytes[0] << 24) & 0xFF000000);
        return addr;
    }
    
    /**

View on GitHub (pinned to 9b989acdf1)

Solutions

  1. Validate the string with InetAddressUtils.isIPv4 / isIPv6 before calling ipToInt.
  2. Do not pass hostnames to ipToInt — resolve and extract a literal IPv4 first.
  3. Remember ipToInt only fits a 32-bit IPv4 address; use the byte[] form (ipToBytesByInet) for IPv6.

Example fix

// before
int packed = InternetAddressUtil.ipToInt(ipStr);

// after
if (!InternetAddressUtil.isIPv4(ipStr)) {
    throw new IllegalArgumentException("ipToInt requires an IPv4 literal: " + ipStr);
}
int packed = InternetAddressUtil.ipToInt(ipStr);
Defensive patterns

Strategy: validation

Validate before calling

if (!InternetAddressUtil.isIPv4(ipStr)) {
    throw new IllegalArgumentException("ipToInt requires an IPv4 literal: " + ipStr);
}
int packed = InternetAddressUtil.ipToInt(ipStr);

Try / catch

try {
    return InternetAddressUtil.ipToInt(ipStr);
} catch (IllegalArgumentException e) {
    throw new InvalidAddressException(ipStr, e);
}

Prevention

When it happens

Trigger: Calling ipToInt on a malformed IP string (e.g., '999.1.1.1', 'abc', '1.2.3'), an IPv6 address that overflows the 4-byte int representation, or a value that is actually a hostname.

Common situations: User-supplied IP from config or registration that was not validated; an IPv6 address passed to a method that only fits IPv4 into an int; a log/field that contained a hostname rather than a literal IP.

Related errors


AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14). Data as JSON: /api/errors/81edc4dadc08c4cf. Report an issue: GitHub.