From 0c2ff0898db2db9cd9c643dfadbff11761bacf5f Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 3 May 2017 00:14:29 +0200 Subject: [PATCH] Backport bpo-30205 to 3.6 (#1403) --- Lib/test/test_socket.py | 4 ++++ Misc/NEWS | 2 ++ Modules/socketmodule.c | 6 +++--- 3 files changed, 9 insertions(+), 3 deletions(-) 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 */ -- 2.50.0