[Python-checkins] cpython (3.5): socket: use INVALID_SOCKET

victor.stinner python-checkins at python.org
Fri Jul 22 11:48:11 EDT 2016


https://hg.python.org/cpython/rev/025281485318
changeset:   102416:025281485318
branch:      3.5
parent:      102414:6c11f52ab9db
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Jul 22 17:43:59 2016 +0200
summary:
  socket: use INVALID_SOCKET

* Replace "fd = -1" with "fd = INVALID_SOCKET"
* Replace "fd < 0" with "fd == INVALID_SOCKET": SOCKET_T is unsigned on Windows

Bug found by Pavel Belikov ("Fragment N1"):
http://www.viva64.com/en/b/0414/#ID0ECDAE

files:
  Modules/_ssl.c         |   8 ++++++--
  Modules/socketmodule.c |  11 ++++++-----
  2 files changed, 12 insertions(+), 7 deletions(-)


diff --git a/Modules/_ssl.c b/Modules/_ssl.c
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -113,6 +113,10 @@
 # define HAVE_ALPN
 #endif
 
+#ifndef INVALID_SOCKET /* MS defines this */
+#define INVALID_SOCKET (-1)
+#endif
+
 enum py_ssl_error {
     /* these mirror ssl.h */
     PY_SSL_ERROR_NONE,
@@ -1699,7 +1703,7 @@
     }
 
     /* Guard against closed socket */
-    if (s->sock_fd < 0)
+    if (s->sock_fd == INVALID_SOCKET)
         return SOCKET_HAS_BEEN_CLOSED;
 
     /* Prefer poll, if available, since you can poll() any fd
@@ -2023,7 +2027,7 @@
 
     if (sock != NULL) {
         /* Guard against closed socket */
-        if ((((PyObject*)sock) == Py_None) || (sock->sock_fd < 0)) {
+        if ((((PyObject*)sock) == Py_None) || (sock->sock_fd == INVALID_SOCKET)) {
             _setSSLError("Underlying socket connection gone",
                          PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__);
             return NULL;
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -2568,8 +2568,9 @@
      * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
      * for more details.
      */
-    if ((fd = s->sock_fd) != -1) {
-        s->sock_fd = -1;
+    fd = s->sock_fd;
+    if (fd != INVALID_SOCKET) {
+        s->sock_fd = INVALID_SOCKET;
         Py_BEGIN_ALLOW_THREADS
         (void) SOCKETCLOSE(fd);
         Py_END_ALLOW_THREADS
@@ -2587,7 +2588,7 @@
 sock_detach(PySocketSockObject *s)
 {
     SOCKET_T fd = s->sock_fd;
-    s->sock_fd = -1;
+    s->sock_fd = INVALID_SOCKET;
     return PyLong_FromSocket_t(fd);
 }
 
@@ -4165,7 +4166,7 @@
 static void
 sock_dealloc(PySocketSockObject *s)
 {
-    if (s->sock_fd != -1) {
+    if (s->sock_fd != INVALID_SOCKET) {
         PyObject *exc, *val, *tb;
         Py_ssize_t old_refcount = Py_REFCNT(s);
         ++Py_REFCNT(s);
@@ -4221,7 +4222,7 @@
 
     new = type->tp_alloc(type, 0);
     if (new != NULL) {
-        ((PySocketSockObject *)new)->sock_fd = -1;
+        ((PySocketSockObject *)new)->sock_fd = INVALID_SOCKET;
         ((PySocketSockObject *)new)->sock_timeout = _PyTime_FromSeconds(-1);
         ((PySocketSockObject *)new)->errorhandler = &set_error;
     }

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list