From: Antoine Pitrou Date: Tue, 2 May 2017 22:22:28 +0000 (+0200) Subject: Backport bpo-30205 to 3.5 (#1404) X-Git-Tag: v3.5.4rc1~176 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0d9d61828bbd6cbc289489bf1d439fa91eca3743;p=python Backport bpo-30205 to 3.5 (#1404) --- diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index b72fc8fbf0..c652c6fd01 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -4589,6 +4589,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 4c90aa83df..c428d77f17 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -49,6 +49,8 @@ Extension Modules 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 af6cc94415..47abe8d3f2 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -1183,9 +1183,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 */