{"record":{"id":"58eeb69cfb07ccd6","repo":"GoogleContainerTools/jib","slug":"invalid-port-range-port-smaller-number-must-c","errorCode":null,"errorMessage":"Invalid port range '<port>'; smaller number must come first.","messagePattern":"Invalid port range '<port>'; smaller number must come first\\.","errorType":"validation","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"jib-core/src/main/java/com/google/cloud/tools/jib/api/Ports.java","lineNumber":74,"sourceCode":"        throw new NumberFormatException(\n            \"Invalid port configuration: '\"\n                + port\n                + \"'. Make sure the port is a single number or a range of two numbers separated \"\n                + \"with a '-', with or without protocol specified (e.g. '<portNum>/tcp' or \"\n                + \"'<portNum>/udp').\");\n      }\n\n      // Parse protocol\n      int min = Integer.parseInt(matcher.group(1));\n      int max = min;\n      if (!Strings.isNullOrEmpty(matcher.group(2))) {\n        max = Integer.parseInt(matcher.group(2));\n      }\n      String protocol = matcher.group(3);\n\n      // Error if configured as 'max-min' instead of 'min-max'\n      if (min > max) {\n        throw new NumberFormatException(\n            \"Invalid port range '\" + port + \"'; smaller number must come first.\");\n      }\n\n      // Warn for possibly invalid port numbers\n      if (min < 1 || max > 65535) {\n        throw new NumberFormatException(\n            \"Port number '\" + port + \"' is out of usual range (1-65535).\");\n      }\n\n      for (int portNumber = min; portNumber <= max; portNumber++) {\n        result.add(Port.parseProtocol(portNumber, protocol));\n      }\n    }\n\n    return result;\n  }\n\n  private Ports() {}","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/GoogleContainerTools/jib/blob/fb949e2676afbbd7dd7a1ef61e20251931325654/jib-core/src/main/java/com/google/cloud/tools/jib/api/Ports.java#L56-L92","documentation":"Ports.parse also validates the order of a port range. When a range like '9000-8000' is given (min parsed greater than max parsed), it throws NumberFormatException with 'Invalid port range ... smaller number must come first.' The library requires ranges in ascending order to keep the exposed port list deterministic.","triggerScenarios":"Calling Ports.parse with a range string whose lower bound is greater than its upper bound, e.g. '9000-8000', '65535-1024'.","commonSituations":"Programmatically generating ranges from min/max variables that are swapped; hand-written config where the range was typed backwards; templating that interpolates high-first ordering.","solutions":["Rewrite the range so the smaller number comes first: '8000-9000' instead of '9000-8000'.","Normalize programmatically with Math.min/Math.max before formatting the range string.","Validate ranges in your config layer before invoking Ports.parse.","If both orders are acceptable input, swap and re-format rather than rejecting the build."],"exampleFix":"// before\nbuilder.addExpose(\"9000-8000\");\n// after\nint a = 9000, b = 8000;\nbuilder.addExpose(Math.min(a, b) + \"-\" + Math.max(a, b)); // \"8000-9000\"","handlingStrategy":"validation","validationCode":"if (min > max) throw new IllegalArgumentException(\"range must be min-max, got \" + min + \"-\" + max);","typeGuard":"null","tryCatchPattern":"try { Ports.parse(List.of(spec)); } catch (NumberFormatException e) { /* normalize via Math.min/max and retry */ }","preventionTips":["Generate range strings with Math.min/Math.max","Review hand-written ranges for reversed bounds","Validate port config at load time","Keep min/max variables clearly named to avoid swaps"],"tags":["validation","ports","config"],"backgroundTag":"invalid-argument-value","analyzedSha":"fb949e2676afbbd7dd7a1ef61e20251931325654","analyzedAt":"2026-09-06T14:04:09.491Z","contentChangedAt":"2026-09-06T14:04:09.491Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}