Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-30711: Wrap broken getaddrinfo
If the getaddrinfo function does not detect invalid numeric services,
replace it with a wrapper that returns an error code if the port number
is invalid, and calls the getaddrinfo function otherwise.
  • Loading branch information
smejkar committed Jun 27, 2017
commit db64d75a4cfb18007df4e4d8c9ef2a15df7993fa
22 changes: 22 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,28 @@ def testGetaddrinfo(self):
except socket.gaierror:
pass

@support.cpython_only
def test_getaddrinfo_invalid_port(self):
# Issue #30711: test that getaddrinfo detects invalid port number
# See HAVE_BROKEN_GETADDRINFO in configure.ac for details.
import _testcapi
flags = getattr(socket, 'AI_NUMERICSERV', None)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, "65536", flags=flags)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, "-1", flags=flags)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, str(_testcapi.LONG_MAX), flags=flags)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, str(_testcapi.LONG_MAX + 1), flags=flags)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, str(_testcapi.LONG_MIN), flags=flags)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, str(_testcapi.LONG_MIN - 1), flags=flags)
with self.assertRaises(socket.gaierror):
socket.getaddrinfo(None, str(_testcapi.ULONG_MAX - 65535 + 1),
flags=flags)

def test_getnameinfo(self):
# only IP addresses are allowed
self.assertRaises(OSError, socket.getnameinfo, ('mail.python.org',0), 0)
Expand Down
25 changes: 25 additions & 0 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,31 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
#define gai_strerror fake_gai_strerror
#define freeaddrinfo fake_freeaddrinfo
#include "getaddrinfo.c"
#elif defined(HAVE_BROKEN_GETADDRINFO)
/* getaddrinfo does not detect invalid numeric services.
Check the port number and return EAI_SERVICE if it's
invalid; call getaddrinfo otherwise. */
static int
wrap_getaddrinfo(const char *hostname, const char *servname,
const struct addrinfo *hints, struct addrinfo **res)
{
if (servname != NULL && servname[0] != '\0' &&
(hints == NULL || /* assume AF_UNSPEC */
hints->ai_family == AF_UNSPEC ||
#ifdef ENABLE_IPV6
hints->ai_family == AF_INET6 ||
#endif
hints->ai_family == AF_INET)) {
long port;
char *end;

port = strtol(servname, &end, 10);
if (*end == '\0' && (port < 0 || port > 0xffff))
return EAI_SERVICE;
}
return getaddrinfo(hostname, servname, hints, res);
}
#define getaddrinfo wrap_getaddrinfo
#endif
#if !defined(HAVE_GETNAMEINFO)
#define getnameinfo fake_getnameinfo
Expand Down