Backport bpo-30205 to 3.6 (#1403)
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 2497e47..80dfc40 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -4660,6 +4660,10 @@
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 936e2b0..3b61543 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,8 @@
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 f3654c9..42aec59 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1212,9 +1212,9 @@
{
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 */