From: Antoine Pitrou Date: Tue, 2 May 2017 22:14:29 +0000 (+0200) Subject: Backport bpo-30205 to 3.6 (#1403) X-Git-Tag: v3.6.2rc1~183 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0c2ff0898db2db9cd9c643dfadbff11761bacf5f;p=python Backport bpo-30205 to 3.6 (#1403) --- diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 2497e47c66..80dfc405c7 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -4660,6 +4660,10 @@ class TestUnixDomain(unittest.TestCase): else: raise + def testUnbound(self): + # Issue #30205 + self.assertIn(self.sock.getsockname(), ('', None)) + def testStrAddr(self): # Test binding to and retrieving a normal string pathname. path = os.path.abspath(support.TESTFN) diff --git a/Misc/NEWS b/Misc/NEWS index 936e2b0201..3b61543469 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -36,6 +36,8 @@ Core and Builtins Library ------- +- bpo-30205: Fix getsockname() for unbound AF_UNIX sockets on Linux. + - bpo-30070: Fixed leaks and crashes in errors handling in the parser module. - bpo-30061: Fixed crashes in IOBase methods __next__() and readlines() when diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index f3654c97e7..42aec59ca7 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -1212,9 +1212,9 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto) { struct sockaddr_un *a = (struct sockaddr_un *) addr; #ifdef __linux__ - if (a->sun_path[0] == 0) { /* Linux abstract namespace */ - addrlen -= offsetof(struct sockaddr_un, sun_path); - return PyBytes_FromStringAndSize(a->sun_path, addrlen); + size_t linuxaddrlen = addrlen - offsetof(struct sockaddr_un, sun_path); + if (linuxaddrlen > 0 && a->sun_path[0] == 0) { /* Linux abstract namespace */ + return PyBytes_FromStringAndSize(a->sun_path, linuxaddrlen); } else #endif /* linux */