From 0d9d61828bbd6cbc289489bf1d439fa91eca3743 Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Wed, 3 May 2017 00:22:28 +0200 Subject: Backport bpo-30205 to 3.5 (#1404) --- 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 b72fc8f..c652c6f 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 4c90aa8..c428d77 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 af6cc94..47abe8d 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 */ -- cgit v0.12