{"record":{"id":"81edc4dadc08c4cf","repo":"alibaba/nacos","slug":"is-invalid-ip","errorCode":null,"errorMessage":"{} is invalid IP","messagePattern":"(.+?) is invalid IP","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"common/src/main/java/com/alibaba/nacos/common/utils/InternetAddressUtil.java","lineNumber":259,"sourceCode":"            return false;\n        }\n        if (Objects.equals(str, LOCAL_HOST)) {\n            return true;\n        }\n        return DOMAIN_PATTERN.matcher(str).matches();\n    }\n    \n    /**\n     * convert ip address to int.\n     *\n     * @param ip ip address.\n     * @return int\n     */\n    public static int ipToInt(String ip) {\n        try {\n            return bytesToInt(ipToBytesByInet(ip));\n        } catch (Exception e) {\n            throw new IllegalArgumentException(ip + \" is invalid IP\");\n        }\n    }\n    \n    /**\n     * convert byte array to int.\n     *\n     * @param bytes byte array.\n     * @return int\n     */\n    public static int bytesToInt(byte[] bytes) {\n        int addr = bytes[3] & 0xFF;\n        addr |= ((bytes[2] << 8) & 0xFF00);\n        addr |= ((bytes[1] << 16) & 0xFF0000);\n        addr |= ((bytes[0] << 24) & 0xFF000000);\n        return addr;\n    }\n    \n    /**","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/common/src/main/java/com/alibaba/nacos/common/utils/InternetAddressUtil.java#L241-L277","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate the string with InetAddressUtils.isIPv4 / isIPv6 before calling ipToInt.","Do not pass hostnames to ipToInt — resolve and extract a literal IPv4 first.","Remember ipToInt only fits a 32-bit IPv4 address; use the byte[] form (ipToBytesByInet) for IPv6."],"exampleFix":"// before\nint packed = InternetAddressUtil.ipToInt(ipStr);\n\n// after\nif (!InternetAddressUtil.isIPv4(ipStr)) {\n    throw new IllegalArgumentException(\"ipToInt requires an IPv4 literal: \" + ipStr);\n}\nint packed = InternetAddressUtil.ipToInt(ipStr);","handlingStrategy":"validation","validationCode":"if (!InternetAddressUtil.isIPv4(ipStr)) {\n    throw new IllegalArgumentException(\"ipToInt requires an IPv4 literal: \" + ipStr);\n}\nint packed = InternetAddressUtil.ipToInt(ipStr);","typeGuard":null,"tryCatchPattern":"try {\n    return InternetAddressUtil.ipToInt(ipStr);\n} catch (IllegalArgumentException e) {\n    throw new InvalidAddressException(ipStr, e);\n}","preventionTips":["Pre-validate IPv4 literals before ipToInt; it only fits 32-bit addresses.","Never pass hostnames to ipToInt; resolve them first.","Use ipToBytesByInet for IPv6."],"tags":["network","ip-parsing","validation","ipv4","ipv6"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}