Commits

Victor Stinner  committed c1a4005

Issue #9566: recv(), recvfrom(), send(), sendall() and sendto() methods
of socket.socket objects now truncate the input buffer to INT_MAX bytes on
Windows to avoid an integer overflow.

(sendall() still send the whole buffer.)

  • Participants
  • Parent commits 5a72adc

Comments (0)

Files changed (1)

File Modules/socketmodule.c

     BEGIN_SELECT_LOOP(s)
     Py_BEGIN_ALLOW_THREADS
     timeout = internal_select_ex(s, 0, interval);
-    if (!timeout)
+    if (!timeout) {
+#if defined(MS_WIN64) || defined(MS_WINDOWS)
+        if (len > INT_MAX)
+            len = INT_MAX;
+        outlen = recv(s->sock_fd, cbuf, (int)len, flags);
+#else
         outlen = recv(s->sock_fd, cbuf, len, flags);
+#endif
+    }
     Py_END_ALLOW_THREADS
 
     if (timeout == 1) {
     timeout = internal_select_ex(s, 0, interval);
     if (!timeout) {
 #ifndef MS_WINDOWS
-        n = recvfrom(s->sock_fd, cbuf, len, flags,
+        if (len > INT_MAX)
+            len = INT_MAX;
+        n = recvfrom(s->sock_fd, cbuf, (int)len, flags,
                      (void *) &addrbuf, &addrlen);
 #else
         n = recvfrom(s->sock_fd, cbuf, len, flags,
     BEGIN_SELECT_LOOP(s)
     Py_BEGIN_ALLOW_THREADS
     timeout = internal_select_ex(s, 1, interval);
-    if (!timeout)
+    if (!timeout) {
 #ifdef __VMS
         n = sendsegmented(s->sock_fd, buf, len, flags);
+#elif defined(MS_WIN64) || defined(MS_WINDOWS)
+        if (len > INT_MAX)
+            len = INT_MAX;
+        n = send(s->sock_fd, buf, (int)len, flags);
 #else
         n = send(s->sock_fd, buf, len, flags);
 #endif
+    }
     Py_END_ALLOW_THREADS
     if (timeout == 1) {
         PyBuffer_Release(&pbuf);
         if (!timeout) {
 #ifdef __VMS
             n = sendsegmented(s->sock_fd, buf, len, flags);
+#elif defined(MS_WIN64) || defined(MS_WINDOWS)
+            if (len > INT_MAX)
+                len = INT_MAX;
+            n = send(s->sock_fd, buf, (int)len, flags);
 #else
             n = send(s->sock_fd, buf, len, flags);
 #endif
     BEGIN_SELECT_LOOP(s)
     Py_BEGIN_ALLOW_THREADS
     timeout = internal_select_ex(s, 1, interval);
-    if (!timeout)
-        n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
+    if (!timeout) {
+#if defined(MS_WIN64) || defined(MS_WINDOWS)
+        if (len > INT_MAX)
+            len = INT_MAX;
+        n = sendto(s->sock_fd, buf, (int)len, flags,
+                   SAS2SA(&addrbuf), addrlen);
+#else
+        n = sendto(s->sock_fd, buf, len, flags,
+                   SAS2SA(&addrbuf), addrlen);
+#endif
+    }
     Py_END_ALLOW_THREADS
 
     if (timeout == 1) {