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).
The function vtkSocket::Connect
executes as follows. Assume the hostName= “some_invalid_hostname”
:
-
gethostbyname
fails to look up the hostname, https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/System/vtkSocket.cxx#L395 - Then it uses
inet_addr
to try to convert aXXX.XXX.XXX.XXX
string, which (obviously) fails, and returns-1
. It doesn't detect this error condition, but since this is a perfectly valid address,gethostbyaddr
converts it to255.255.255.255
.
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.