Issue #17269: Workaround for a platform bug in getaddrinfo on OSX

Without this patch socket.getaddrinfo crashed when called
with some unusual argument combinations.
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index 111e553..3b112e6 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -665,6 +665,8 @@
         socket.getaddrinfo(None, 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0,
                            socket.AI_PASSIVE)
 
+        # Issue 17269
+        socket.getaddrinfo("localhost", None, 0, 0, 0, socket.AI_NUMERICSERV)
 
     def check_sendall_interrupted(self, with_timeout):
         # socketpair() is not stricly required, but it makes things easier.
diff --git a/Misc/NEWS b/Misc/NEWS
index 9c472dd..8fdc881 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,9 @@
 
 - Fix typos in the multiprocessing module.
 
+- Issue #17269: Workaround for socket.getaddrinfo crash on MacOS X
+  with port None or "0" and flags AI_NUMERICSERV.
+
 IDLE
 ----
 
@@ -51,7 +54,7 @@
 Core and Builtins
 -----------------
 
-- Issue #15535: Fixed regression in the pickling of named tuples by 
+- Issue #15535: Fixed regression in the pickling of named tuples by
   removing the __dict__ property introduced in 2.7.4.
 
 - Issue #17857: Prevent build failures with pre-3.5.0 versions of sqlite3,
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index bdc055d..238c849 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -4179,6 +4179,15 @@
                         "getaddrinfo() argument 2 must be integer or string");
         goto err;
     }
+#ifdef __APPLE__
+    if ((flags & AI_NUMERICSERV) && (pptr == NULL || (pptr[0] == '0' && pptr[1] == 0))) {
+        /* On OSX upto at least OSX 10.8 getaddrinfo crashes
+	 * if AI_NUMERICSERV is set and the servname is NULL or "0".
+	 * This workaround avoids a segfault in libsystem.
+	 */
+        pptr = "00";
+    }
+#endif
     memset(&hints, 0, sizeof(hints));
     hints.ai_family = family;
     hints.ai_socktype = socktype;