Incorrect error handling in vtkSocket::Connect
I ran into paraview/paraview#17752 (closed) and discovered a bug in
vtkSocket::Connect. That ticket created a condition where it tried to connect to an invalid hostname (specifically, the hostname of the machine, which was not present in
/etc/hosts on OSX).
vtkSocket::Connect executes as follows. Assume the
gethostbynamefails to look up the hostname, https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/System/vtkSocket.cxx#L395
- Then it uses
inet_addrto try to convert a
XXX.XXX.XXX.XXXstring, which (obviously) fails, and returns
-1. It doesn't detect this error condition, but since this is a perfectly valid address,
gethostbyaddrconverts it to
Then it tries to connect to the broadcast address instead of flagging the unresolvable hostname. Since it didn't set the options correctly for a broadcast address, it throws
EACCES, resulting in the error I saw.
Man page indicates a possible resolution (https://man7.org/linux/man-pages/man3/inet_addr.3.html):
The inet_addr() function converts the Internet host address cp from IPv4 numbers-and-dots notation into binary data in network byte order. If the input is invalid, INADDR_NONE (usually -1) is returned. Use of this function is problematic because -1 is a valid address (255.255.255.255). Avoid its use in favor of inet_aton(), inet_pton(3), or getaddrinfo(3), which provide a cleaner way to indicate error return.