c++tools: Work around a BSD bug in getaddrinfo().

Some versions of the BSD getaddrinfo() call do not work with the specific
input of "0" for the servname entry (a segv results).  Since we are making
the call with a dummy port number, the value is actually no important, other
than it should be in range.  Work around the BSD bug by using "1" instead.

Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>

c++tools/ChangeLog:

	* server.cc (accept_from): Use "1" as the dummy port number.
This commit is contained in:
Iain Sandoe 2022-03-13 16:34:54 +00:00
parent 99fcd602a8
commit 41f01c9715

View File

@ -360,7 +360,11 @@ accept_from (char *arg ATTRIBUTE_UNUSED)
hints.ai_next = NULL;
struct addrinfo *addrs = NULL;
if (int e = getaddrinfo (slash == arg ? NULL : arg, "0", &hints, &addrs))
/* getaddrinfo requires either hostname or servname to be non-null, so that we must
set a port number (to cover the case that the string passed contains just /NN).
Use an arbitrary in-range port number, but avoiding "0" which triggers a bug on
some BSD variants. */
if (int e = getaddrinfo (slash == arg ? NULL : arg, "1", &hints, &addrs))
{
noisy ("cannot resolve '%s': %s", arg, gai_strerror (e));
ok = false;