{"record":{"id":"44b34c106b386dcb","repo":"apache/hadoop","slug":"address-should-be-host-port-but-it-is","errorCode":null,"errorMessage":"Address should be <host>:<port>, but it is {}","messagePattern":"Address should be <host>:<port>, but it is (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java","lineNumber":784,"sourceCode":"   * @return hort port string.\n   */\n  public static String getHostPortString(InetSocketAddress addr) {\n    return addr.getHostName() + \":\" + addr.getPort();\n  }\n\n  /**\n   * Get port as integer from host port string like host:port.\n   *\n   * @param addr host + port string like host:port.\n   * @return an integer value representing the port.\n   * @throws IllegalArgumentException if the input is not in the correct format.\n   */\n  public static int getPortFromHostPortString(String addr)\n      throws IllegalArgumentException {\n    String[] hostport = addr.split(\":\");\n    if (hostport.length != 2) {\n      String errorMsg = \"Address should be <host>:<port>, but it is \" + addr;\n      throw new IllegalArgumentException(errorMsg);\n    }\n    return Integer.parseInt(hostport[1]);\n  }\n  \n  /**\n   * Checks if {@code host} is a local host name and return {@link InetAddress}\n   * corresponding to that address.\n   * \n   * @param host the specified host\n   * @return a valid local {@link InetAddress} or null\n   * @throws SocketException if an I/O error occurs\n   */\n  public static InetAddress getLocalInetAddress(String host)\n      throws SocketException {\n    if (host == null) {\n      return null;\n    }\n    InetAddress addr = null;","sourceCodeStart":766,"sourceCodeEnd":802,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/net/NetUtils.java#L766-L802","documentation":"IllegalArgumentException from NetUtils.getPortFromHostPortString when splitting the input on ':' does not produce exactly two tokens. Anything other than a single-colon 'host:port' fails: a bare host, 'host:port:extra', empty string, or an IPv6 literal (whose colons yield many tokens). The message echoes the offending input. Note the port part is then parsed by Integer.parseInt, so a non-numeric port throws NumberFormatException instead.","triggerScenarios":"getPortFromHostPortString(\"myhost\") (no port); getPortFromHostPortString(\"http://myhost:8080\") (scheme adds a colon); getPortFromHostPortString(\"::1:8080\") or any unbracketed IPv6 address.","commonSituations":"Passing URLs where a host:port is expected; forgetting to strip the scheme; IPv6 addresses not bracketed (even bracketed '[::1]:8080' fails because split(':') sees 3+ tokens — this helper is effectively IPv4/hostname only).","solutions":["Pass exactly 'host:port' — strip any 'scheme://' prefix first","For IPv6, use a URI-based parse (URI.create(\"dummyscheme://[::1]:8080\").getPort()) instead of this helper","Pre-check with addr.split(\":\").length == 2 (and a numeric last token) before calling, to give a clearer error"],"exampleFix":"// before\nint port = NetUtils.getPortFromHostPortString(\"http://host:8080\"); // 3 tokens -> IllegalArgumentException\n\n// after\nString hp = \"http://host:8080\".replaceFirst(\"^\\\\w+://\", \"\"); // \"host:8080\"\nint port = NetUtils.getPortFromHostPortString(hp);","handlingStrategy":"validation","validationCode":"String[] parts = addr.split(\":\");\nif (parts.length != 2 || !parts[1].matches(\"\\\\d+\")) {\n  throw new IllegalStateException(\"expected host:port, got: \" + addr);\n}","typeGuard":"static boolean isHostPort(String addr) {\n  String[] p = addr.split(\":\");\n  return p.length == 2 && p[1].matches(\"\\\\d+\");\n}","tryCatchPattern":null,"preventionTips":["Strip scheme:// prefixes before calling this helper","Do not use this helper for IPv6 — parse with URI instead","Include the numeric-port check yourself to get a clearer error than NumberFormatException"],"tags":["host-port","string-parsing","validation","hadoop-common"],"backgroundTag":"invalid-host-port-format","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}