{"record":{"id":"e0b0b43b814b6579","repo":"grpc/grpc-java","slug":"invalid-port","errorCode":null,"errorMessage":"Invalid port","messagePattern":"Invalid port","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/grpc/Uri.java","lineNumber":970,"sourceCode":"     *\n     * <p>This field is optional.\n     *\n     * @param port the new \"port\" component, or -1 to clear this field\n     * @return this, for fluent building\n     */\n    @CanIgnoreReturnValue\n    public Builder setPort(int port) {\n      this.port = port < 0 ? null : Integer.toString(port);\n      return this;\n    }\n\n    @CanIgnoreReturnValue\n    Builder setRawPort(String port) {\n      if (port != null && !port.isEmpty()) {\n        try {\n          Integer.parseInt(port); // Result unused.\n        } catch (NumberFormatException e) {\n          throw new IllegalArgumentException(\"Invalid port\", e);\n        }\n      }\n      this.port = port;\n      return this;\n    }\n\n    /**\n     * Specifies the userinfo, host and port URI components all at once using a single string.\n     *\n     * <p>This setter is \"raw\" in the sense that special characters in userinfo and host must be\n     * passed in percent-encoded. See <a\n     * href=\"https://datatracker.ietf.org/doc/html/rfc3986#section-3.2\">RFC 3986 3.2</a> for the set\n     * of characters allowed in each component of an authority.\n     *\n     * <p>There's no \"cooked\" method to set authority like for other URI components because\n     * authority is a *compound* URI component whose userinfo, host and port components are\n     * delimited with special characters '@' and ':'. But the first two of those components can\n     * themselves contain these delimiters so we need percent-encoding to parse them unambiguously.","sourceCodeStart":952,"sourceCodeEnd":988,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/api/src/main/java/io/grpc/Uri.java#L952-L988","documentation":"Thrown by Uri.Builder.setRawPort when the port string is non-empty but not parseable as an integer via Integer.parseInt. The raw port is stored as a string, but it must still be numerically valid so it can later be interpreted as a port number.","triggerScenarios":"Calling setRawPort or building a URI whose authority contains a port segment that is non-numeric, e.g. 'host:8o80', 'host:port', or 'host:8080x'.","commonSituations":"Concatenating host and port from config where the port variable was mis-typed or contains a trailing character; environment variable ports with whitespace or quotes; copying 'host:port' placeholders literally.","solutions":["Ensure the port string is all digits before building the URI","Trim whitespace and strip quotes from port config values","Parse/validate with Integer.parseInt yourself and report a clearer message","Omit the port entirely (null/empty) instead of passing a placeholder"],"exampleFix":"// before\nbuilder.setHost(host).setRawPort(System.getenv(\"PORT\"));\n// after\nString port = System.getenv(\"PORT\");\nif (port != null && !port.trim().isEmpty()) {\n  Integer.parseInt(port.trim()); // fail fast with clearer message\n  builder.setHost(host).setRawPort(port.trim());\n}","handlingStrategy":"validation","validationCode":"static Integer parsePort(String s) {\n  if (s == null || s.trim().isEmpty()) return null;\n  try { return Integer.valueOf(s.trim()); } catch (NumberFormatException e) { return null; }\n}","typeGuard":"boolean isValidPort(String s) { if (s == null || s.isEmpty()) return true; try { Integer.parseInt(s); return true; } catch (NumberFormatException e) { return false; } }","tryCatchPattern":"try { builder.setRawPort(portStr); } catch (IllegalArgumentException e) { throw new ConfigException(\"Port is not numeric: \" + portStr, e); }","preventionTips":["Store ports as integers in config, stringify only at the last step","Trim and strip quotes from env-provided ports","Validate with Integer.parseInt before building URIs","Never copy host:port placeholders literally"],"tags":["uri","port","validation"],"backgroundTag":"invalid-url-format","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}