{"record":{"id":"f00a1dbca024cebd","repo":"XTLS/Xray-core","slug":"invalid-port-range-val","errorCode":null,"errorMessage":"invalid port range: {val}","messagePattern":"invalid port range: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/net/port.go","lineNumber":23,"sourceCode":"\t\"strconv\"\n\n\t\"github.com/xtls/xray-core/common/errors\"\n)\n\n// Port represents a network port in TCP and UDP protocol.\ntype Port uint16\n\n// PortFromBytes converts a byte array to a Port, assuming bytes are in big endian order.\n// @unsafe Caller must ensure that the byte array has at least 2 elements.\nfunc PortFromBytes(port []byte) Port {\n\treturn Port(binary.BigEndian.Uint16(port))\n}\n\n// PortFromInt converts an integer to a Port.\n// @error when the integer is not positive or larger then 65535\nfunc PortFromInt(val uint32) (Port, error) {\n\tif val > 65535 {\n\t\treturn Port(0), errors.New(\"invalid port range: \", val)\n\t}\n\treturn Port(val), nil\n}\n\n// PortFromString converts a string to a Port.\n// @error when the string is not an integer or the integral value is a not a valid Port.\nfunc PortFromString(s string) (Port, error) {\n\tval, err := strconv.ParseUint(s, 10, 32)\n\tif err != nil {\n\t\treturn Port(0), errors.New(\"invalid port range: \", s)\n\t}\n\treturn PortFromInt(uint32(val))\n}\n\n// Value return the corresponding uint16 value of a Port.\nfunc (p Port) Value() uint16 {\n\treturn uint16(p)\n}","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/XTLS/Xray-core/blob/7d214f8b094f75322fa3990f8aadad1c912f24f5/common/net/port.go#L5-L41","documentation":"Returned by net.PortFromInt when the uint32 value exceeds 65535, the maximum representable TCP/UDP port. The Port type is a uint16, so any larger input cannot be stored and is rejected before truncation could silently corrupt it.","triggerScenarios":"Calling PortFromInt(val) with val > 65535, typically after parsing configuration values (inbound/outbound port settings) or deriving a port from arithmetic that overflowed the valid range.","commonSituations":"Config files with a typo like port 70000, a port computed from an offset (base + shift) exceeding the range, or unvalidated user input parsed with ParseUint(..., 32) reaching PortFromInt.","solutions":["Fix the configuration value to the 1-65535 range","Validate parsed integers against 65535 before converting to Port","If the value comes from a byte stream, use PortFromBytes which reads exactly 2 big-endian bytes and cannot overflow"],"exampleFix":"// before\nport, err := net.PortFromInt(cfg.Port) // cfg.Port = 70000\n\n// after\nif cfg.Port > 65535 {\n    return errors.New(\"port out of range in config\")\n}\nport, err := net.PortFromInt(cfg.Port)","handlingStrategy":"validation","validationCode":"func validPortInt(v uint32) bool { return v >= 1 && v <= 65535 }\nif !validPortInt(cfg.Port) { return fmt.Errorf(\"port %d out of range\", cfg.Port) }","typeGuard":"func isValidPortInt(v uint32) bool { return v <= 65535 }","tryCatchPattern":null,"preventionTips":["Validate port ranges at config-load time","Prefer PortFromBytes for wire data"],"tags":["validation","port","config"],"backgroundTag":null,"analyzedSha":"7d214f8b094f75322fa3990f8aadad1c912f24f5","analyzedAt":"2026-08-15T14:26:24.325Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}