prestodb/presto · error · IllegalArgumentException

Invalid bracketed host/port:

Error message

Invalid bracketed host/port: 

What it means

HostAddress.fromString parses "host[:port]" strings. Strings starting with '[' are treated as bracketed IPv6 literals and must match the pattern ^\[(.*:.*)\](?::(\d*))?$ — brackets enclosing at least one colon, with an optional :port suffix. Anything else bracketed (missing closing bracket, no colon inside, junk after the port) is rejected as unparseable.

Source

Thrown at presto-spi/src/main/java/com/facebook/presto/spi/HostAddress.java:183

     * Note that the host-only formats will leave the port field undefined.  You
     * can use {@link #withDefaultPort(int)} to patch in a default value.
     *
     * @param hostPortString the input string to parse.
     * @return if parsing was successful, a populated HostAddress object.
     * @throws IllegalArgumentException if nothing meaningful could be parsed.
     */
    @JsonCreator
    public static HostAddress fromString(String hostPortString)
    {
        requireNonNull(hostPortString, "hostPortString is null");
        String host;
        String portString = null;

        if (hostPortString.startsWith("[")) {
            // Parse a bracketed host, typically an IPv6 literal.
            Matcher matcher = BRACKET_PATTERN.matcher(hostPortString);
            if (!matcher.matches()) {
                throw new IllegalArgumentException("Invalid bracketed host/port: " + hostPortString);
            }
            host = matcher.group(1);
            portString = matcher.group(2);  // could be null
        }
        else {
            int colonPos = hostPortString.indexOf(':');
            if (colonPos >= 0 && hostPortString.indexOf(':', colonPos + 1) == -1) {
                // Exactly 1 colon.  Split into host:port.
                host = hostPortString.substring(0, colonPos);
                portString = hostPortString.substring(colonPos + 1);
            }
            else {
                // 0 or 2+ colons.  Bare hostname or IPv6 literal.
                host = hostPortString;
            }
        }

        int port = NO_PORT;

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure IPv6 literals are fully bracketed and contain colons: "[2001:db8::1]" or "[::1]:8080"
  2. Strip any trailing garbage (paths, whitespace) before parsing
  3. For non-IPv6 hosts, remove the brackets entirely
  4. Prefer fromUri(URI) when input comes from a URI

Example fix

// before
HostAddress.fromString("[2001:db8::1");
// after
HostAddress.fromString("[2001:db8::1]:8080");
// or
HostAddress.fromString("2001:db8::1").withDefaultPort(8080);
Defensive patterns

Strategy: validation

Validate before calling

private static final Pattern BRACKETED = Pattern.compile("^\\[(.*:.*)\\](?::(\\d*))?$");
if (hostPort.startsWith("[") && !BRACKETED.matcher(hostPort).matches()) {
    throw new IllegalArgumentException("Malformed bracketed IPv6 literal: " + hostPort);
}

Try / catch

try {
    addr = HostAddress.fromString(hostPort);
} catch (IllegalArgumentException e) {
    log.error("Cannot parse address '%s'", hostPort, e);
    addr = null;
}

Prevention

When it happens

Trigger: Calling HostAddress.fromString with a malformed bracketed literal such as "[::1", "[::1]x", "[localhost]", or "[::1]:port" where port is non-numeric with extra characters.

Common situations: Manually truncating IPv6 strings (cutting off the closing bracket); users typing IPv6 without brackets; log/config values where the address was concatenated with a path or trailing text.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/fcf8de2cb0d891cd. Report an issue: GitHub.