Skip to content

Commit

Permalink
ACE_INET_Addr::set errantly succeeds when ACE_LACKS_GETSERVBYNAME
Browse files Browse the repository at this point in the history
Problem
-------

Calling `set("1.2.3.4")` on an ACE_INET_Addr eventually tries to part
it as a port name.  When `ACE_LACKS_GETSERVBYNAME`, this succeeds
returning port number 0 with the reset address.

When port names are not expected, this breaks a useful idiom of
parsing addresses that may or may not contain port numbers:

    ACE_INET_Addr addr;
    if (addr.set(x) == 0) {
      // Success, address contained a port number
    } else if (addr.set(u_short(0), x) == 0) {
      // Success, address did not contain a port number.
    } else {
      // Fail
    }

Solution
--------

Return -1 for the port number when `ACE_LACKS_GETSERVBYNAME`.
  • Loading branch information
jrw972 committed Jun 21, 2023
1 parent ab6ab47 commit 56aaafb
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions ACE/ace/INET_Addr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,11 @@ static int get_port_number_from_name (const char port_name[],
}

// We try to resolve port number from its name.
port_number = -1;
#if defined (ACE_LACKS_GETSERVBYNAME)
port_number = 0;
ACE_UNUSED_ARG (port_name);
ACE_UNUSED_ARG (protocol);
#else
port_number = -1;
servent sentry;
ACE_SERVENT_DATA buf;
servent *sp = ACE_OS::getservbyname_r (port_name,
Expand Down

0 comments on commit 56aaafb

Please sign in to comment.