From df422190db1062c4f286178f46241b11f6bb60ce Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Mon, 1 May 2017 23:38:57 +0200 Subject: [PATCH 1/2] bpo-30205: Fix getsockname() for unbound AF_UNIX sockets on Linux --- Lib/test/test_socket.py | 4 ++++ Modules/socketmodule.c | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 2155d630dd4790..5e37fc608f26ad 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -4682,6 +4682,10 @@ def bind(self, sock, path): else: raise + def testUnbound(self): + # Issue #30205 + self.assertEqual(self.sock.getsockname(), '') + def testStrAddr(self): # Test binding to and retrieving a normal string pathname. path = os.path.abspath(support.TESTFN) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 6d5c256ff340d6..456c66478e9691 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -1211,9 +1211,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 */ From b43be2c28b2d1abdb2817e0c4d3a2b3498692b9d Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Tue, 2 May 2017 17:01:29 +0200 Subject: [PATCH 2/2] Add NEWS entry --- Misc/NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Misc/NEWS b/Misc/NEWS index 118483f9437ed4..c3eaaf2d4df664 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -317,6 +317,8 @@ Extension Modules Library ------- +- bpo-30205: Fix getsockname() for unbound AF_UNIX sockets on Linux. + - bpo-30228: The seek() and tell() methods of io.FileIO now set the internal seekable attribute to avoid one syscall on open() (in buffered or text mode).