{"record":{"id":"7cea634a85354ca3","repo":"JuliusBrussee/caveman","slug":"invalid-listen-port-q","errorCode":null,"errorMessage":"invalid listen port %q","messagePattern":"invalid listen port %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/internal/runstate/runstate.go","lineNumber":97,"sourceCode":"\tfor name, raw := range upstreams {\n\t\tout[name] = \"\"\n\t\tu, err := url.Parse(raw)\n\t\tif err != nil || (u.Scheme != \"http\" && u.Scheme != \"https\") || u.Hostname() == \"\" || u.User != nil || u.RawQuery != \"\" || u.ForceQuery || u.Fragment != \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tout[name] = raw\n\t}\n\treturn out\n}\n\nfunc PortFromListen(listen string) (int, error) {\n\t_, raw, err := net.SplitHostPort(listen)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"invalid listen address %q: %w\", listen, err)\n\t}\n\tport, err := strconv.Atoi(raw)\n\tif err != nil || port < 1 || port > 65535 {\n\t\treturn 0, fmt.Errorf(\"invalid listen port %q\", raw)\n\t}\n\treturn port, nil\n}\n\nfunc Path(home string, port int) string {\n\treturn filepath.Join(home, \"run\", strconv.Itoa(port)+\".json\")\n}\n\nfunc New(listen, mode, owner, version string) (State, error) {\n\tport, err := PortFromListen(listen)\n\tif err != nil {\n\t\treturn State{}, err\n\t}\n\tif owner != \"wrap\" && owner != \"start\" {\n\t\towner = \"start\"\n\t}\n\tvar token [16]byte\n\tif _, err := rand.Read(token[:]); err != nil {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/internal/runstate/runstate.go#L79-L115","documentation":"PortFromListen splits the listen address successfully but the port component fails validation: it is either not an integer or outside the 1-65535 range. This is a distinct error from the address-split failure and carries no wrapped cause.","triggerScenarios":"Passing a listen string whose port is non-numeric ('host:abc'), zero ('host:0'), negative ('host:-1'), or above 65535 ('host:70000').","commonSituations":"Placeholder ports like 0 in config, typos adding an extra digit, or copying a URL that includes a scheme so the 'port' becomes garbage.","solutions":["Use a port between 1 and 65535 in the listen address","Check for typos such as leading zeros with extra digits or a swapped value","Strip any URL scheme before passing the address","Validate the port in config before passing it to New"],"exampleFix":"// before\nrunstate.PortFromListen(\"127.0.0.1:70000\")\n// after\nrunstate.PortFromListen(\"127.0.0.1:8080\")","handlingStrategy":"validation","validationCode":"_, port, err := net.SplitHostPort(listen)\nif err == nil {\n    p, aerr := strconv.Atoi(port)\n    if aerr != nil || p < 1 || p > 65535 { return fmt.Errorf(\"port %q out of range\", port) }\n}","typeGuard":null,"tryCatchPattern":"port, err := runstate.PortFromListen(listen)\nif err != nil && strings.HasPrefix(err.Error(), \"invalid listen port\") {\n    return fmt.Errorf(\"configure a port between 1 and 65535, got %q\", listen)\n}","preventionTips":["Range-check ports when read from config or env","Reject 0 and negative ports explicitly","Don't pass full URLs where a listen address is expected","Add a startup config validation step"],"tags":["config","port-validation"],"backgroundTag":"value-out-of-range","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}