Skip to content

[BUG] SocketAddr integer constructor silently wraps an out-of-range port #4305

Closed

Description

@thc1006

Part of #4287. Split out of the #4292 review so it does not widen that PR.

SocketAddr(u_long addr, int port) narrows the port without checking its range:

https://github.com/open-telemetry/opentelemetry-cpp/blob/main/ext/include/opentelemetry/ext/http/server/socket_tools.h

inet4.sin_port = htons(static_cast<uint16_t>(port));

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.

Activity

  1. added
    needs-triage
    on Jul 25, 2026
  2. noob339 commented on Jul 31, 2026

    @noob339

    I will take on the issue

  3. noob339 commented on Jul 31, 2026

    @noob339

    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.

  4. thc1006 commented on Aug 3, 2026

    @thc1006
    MemberAuthor

    @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.

  5. added
    triage/accepted
    and removed
    needs-triage
    on Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage/accepted

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions