prestodb/presto · error · IllegalArgumentException

Port is invalid:

Error message

Port is invalid: 

What it means

HostAddress.fromParts builds a HostAddress from an explicit host string and port. The port must satisfy isValidPort (0..65535; typically 1..65535 usable); an out-of-range int is rejected before any parsing so callers get a clear message instead of a malformed address.

Source

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

    {
        return hasPort() ? port : defaultPort;
    }

    /**
     * Build a HostAddress instance from separate host and port values.
     * <p>
     * <p>Note: Non-bracketed IPv6 literals are allowed.
     *
     * @param host the host string to parse.  Must not contain a port number.
     * @param port a port number from [0..65535]
     * @return if parsing was successful, a populated HostAddress object.
     * @throws IllegalArgumentException if {@code host} contains a port number,
     * or {@code port} is out of range.
     */
    public static HostAddress fromParts(String host, int port)
    {
        if (!isValidPort(port)) {
            throw new IllegalArgumentException("Port is invalid: " + port);
        }
        HostAddress parsedHost = fromString(host);
        if (parsedHost.hasPort()) {
            throw new IllegalArgumentException("host contains a port declaration: " + host);
        }
        return new HostAddress(parsedHost.host, port);
    }

    private static final Pattern BRACKET_PATTERN = Pattern.compile("^\\[(.*:.*)\\](?::(\\d*))?$");

    /**
     * Split a freeform string into a host and port, without strict validation.
     * <p>
     * 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.

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Validate the port is in 1..65535 before calling fromParts
  2. Check the config/default value supplying the port (e.g. http-server.port) is actually set
  3. Parse host and port with HostAddress.fromUri(String) instead of splitting manually

Example fix

// before
int port = config.getPort(); // 0 when unset
HostAddress addr = HostAddress.fromParts(host, port);
// after
int port = config.getPort();
if (port < 1 || port > 65535) { throw new ConfigException("invalid port: " + port); }
HostAddress addr = HostAddress.fromParts(host, port);
Defensive patterns

Strategy: validation

Validate before calling

if (port < 1 || port > 65535) {
    throw new IllegalArgumentException("Port must be in 1..65535, got: " + port);
}

Try / catch

try {
    addr = HostAddress.fromParts(host, port);
} catch (IllegalArgumentException e) {
    throw new ConfigException("Invalid port for host " + host + ": " + port, e);
}

Prevention

When it happens

Trigger: Calling HostAddress.fromParts(host, port) where port is negative, greater than 65535, or otherwise fails isValidPort — often a port parsed from config or a URI default of 0/-1.

Common situations: Port read from an unset config property defaulting to 0; integer overflow or truncated string parse; concatenating host:port strings and re-splitting incorrectly before calling fromParts.

Related errors


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