]> granicus.if.org Git - python/commitdiff
Backport bpo-30205 to 3.5 (#1404)
authorAntoine Pitrou <pitrou@free.fr>
Tue, 2 May 2017 22:22:28 +0000 (00:22 +0200)
committerGitHub <noreply@github.com>
Tue, 2 May 2017 22:22:28 +0000 (00:22 +0200)
Lib/test/test_socket.py
Misc/NEWS
Modules/socketmodule.c

index b72fc8fbf0cadd3b48cbd7c64a70586dccb46834..c652c6fd01428ff6c27c888492e863ce605dcd7a 100644 (file)
@@ -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)
index 4c90aa83dfb0daad19cda129f71be2080902f121..c428d77f17b754328c5ab2bc6ead7eecbd33c794 100644 (file)
--- 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
index af6cc94415b6869d0b37e22bd6cc2d5b514348b0..47abe8d3f2c6eecdbb994492d23997a6cab836b9 100644 (file)
@@ -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 */