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

index 2497e47c668bc3c5492f5fcdefd61f6c265df35d..80dfc405c7180929d2734a59a2217e37a9970a13 100644 (file)
@@ -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)
index 936e2b02017f8ed1523b556911d7fef0aa1c1a95..3b615434692239efd72b5d68d1ebd6cc0a41bd8b 100644 (file)
--- 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
index f3654c97e76ce910f655e46c2cc14a2ab4a28b60..42aec59ca7f6e59a50da89f3772e3db52c964e99 100644 (file)
@@ -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 */