You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] SocketAddr integer constructor silently wraps an out-of-range port #4305
So port = -1 becomes 65535, 65536 becomes 0, 99999 becomes 34463. HttpServer::addListeningPort(int port) passes straight through to this overload, so an out-of-range port silently binds a different one.
#4292 gave the string constructor a contract where a malformed port produces an invalid SocketAddr (AF_UNSPEC, port() == -1). The two constructors are now inconsistent: one rejects a bad port, the other wraps it.
This is deliberately not folded into #4292 because the integer overload has a real caller and changing its failure behaviour means deciding how addListeningPort should report a bad port (return value, clamp, or leave the socket unbound), which is a separate design question.
Suggested direction
Validate port < 0 || port > 65535 in the integer overload, leave AF_UNSPEC on failure to match the string constructor, and have addListeningPort surface the failure rather than binding a wrapped port. Add tests for the boundary values.
Adds range validation to the integer SocketAddr constructor, makes addListeningPort() return -1 for invalid ports, and adds regression coverage for -1, 65536, and 99999.
The PR includes Fixes #4287, so this issue will close automatically once the PR is merged.
@noob339 flagging something you may not have seen, since it could bear on #4320. @dbarker described the scope for these two files on #4287: http_server.h and socket_tools.h are internal, supporting the tests and a single example, and the bar for them is being correct enough for that rather than production hardening.
Whether an out-of-range listening port sits inside or outside that bar is his call rather than mine. I am leaving this issue open and I am not touching your PR. I mention it only because I closed several of my own tickets on those two files today for that reason, and it seemed worse to let you find out afterwards.
Activity
noob339 commentedon Jul 31, 2026
I will take on the issue
noob339 commentedon Jul 31, 2026
Opened PR #4320 w/ fix
Adds range validation to the integer SocketAddr constructor, makes addListeningPort() return -1 for invalid ports, and adds regression coverage for -1, 65536, and 99999.
The PR includes Fixes #4287, so this issue will close automatically once the PR is merged.
thc1006 commentedon Aug 3, 2026
@noob339 flagging something you may not have seen, since it could bear on #4320. @dbarker described the scope for these two files on #4287:
http_server.handsocket_tools.hare internal, supporting the tests and a single example, and the bar for them is being correct enough for that rather than production hardening.Whether an out-of-range listening port sits inside or outside that bar is his call rather than mine. I am leaving this issue open and I am not touching your PR. I mention it only because I closed several of my own tickets on those two files today for that reason, and it seemed worse to let you find out afterwards.