Skip to content

Commit 01583b3

Browse files
committed
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.)
1 parent f815579 commit 01583b3

1 file changed

Lines changed: 32 additions & 5 deletions

File tree

Modules/socketmodule.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2552,8 +2552,15 @@ sock_recv_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags)
25522552
BEGIN_SELECT_LOOP(s)
25532553
Py_BEGIN_ALLOW_THREADS
25542554
timeout = internal_select_ex(s, 0, interval);
2555-
if (!timeout)
2555+
if (!timeout) {
2556+
#if defined(MS_WIN64) || defined(MS_WINDOWS)
2557+
if (len > INT_MAX)
2558+
len = INT_MAX;
2559+
outlen = recv(s->sock_fd, cbuf, (int)len, flags);
2560+
#else
25562561
outlen = recv(s->sock_fd, cbuf, len, flags);
2562+
#endif
2563+
}
25572564
Py_END_ALLOW_THREADS
25582565

25592566
if (timeout == 1) {
@@ -2760,7 +2767,9 @@ sock_recvfrom_guts(PySocketSockObject *s, char* cbuf, Py_ssize_t len, int flags,
27602767
timeout = internal_select_ex(s, 0, interval);
27612768
if (!timeout) {
27622769
#ifndef MS_WINDOWS
2763-
n = recvfrom(s->sock_fd, cbuf, len, flags,
2770+
if (len > INT_MAX)
2771+
len = INT_MAX;
2772+
n = recvfrom(s->sock_fd, cbuf, (int)len, flags,
27642773
(void *) &addrbuf, &addrlen);
27652774
#else
27662775
n = recvfrom(s->sock_fd, cbuf, len, flags,
@@ -3239,12 +3248,17 @@ sock_send(PySocketSockObject *s, PyObject *args)
32393248
BEGIN_SELECT_LOOP(s)
32403249
Py_BEGIN_ALLOW_THREADS
32413250
timeout = internal_select_ex(s, 1, interval);
3242-
if (!timeout)
3251+
if (!timeout) {
32433252
#ifdef __VMS
32443253
n = sendsegmented(s->sock_fd, buf, len, flags);
3254+
#elif defined(MS_WIN64) || defined(MS_WINDOWS)
3255+
if (len > INT_MAX)
3256+
len = INT_MAX;
3257+
n = send(s->sock_fd, buf, (int)len, flags);
32453258
#else
32463259
n = send(s->sock_fd, buf, len, flags);
32473260
#endif
3261+
}
32483262
Py_END_ALLOW_THREADS
32493263
if (timeout == 1) {
32503264
PyBuffer_Release(&pbuf);
@@ -3294,6 +3308,10 @@ sock_sendall(PySocketSockObject *s, PyObject *args)
32943308
if (!timeout) {
32953309
#ifdef __VMS
32963310
n = sendsegmented(s->sock_fd, buf, len, flags);
3311+
#elif defined(MS_WIN64) || defined(MS_WINDOWS)
3312+
if (len > INT_MAX)
3313+
len = INT_MAX;
3314+
n = send(s->sock_fd, buf, (int)len, flags);
32973315
#else
32983316
n = send(s->sock_fd, buf, len, flags);
32993317
#endif
@@ -3388,8 +3406,17 @@ sock_sendto(PySocketSockObject *s, PyObject *args)
33883406
BEGIN_SELECT_LOOP(s)
33893407
Py_BEGIN_ALLOW_THREADS
33903408
timeout = internal_select_ex(s, 1, interval);
3391-
if (!timeout)
3392-
n = sendto(s->sock_fd, buf, len, flags, SAS2SA(&addrbuf), addrlen);
3409+
if (!timeout) {
3410+
#if defined(MS_WIN64) || defined(MS_WINDOWS)
3411+
if (len > INT_MAX)
3412+
len = INT_MAX;
3413+
n = sendto(s->sock_fd, buf, (int)len, flags,
3414+
SAS2SA(&addrbuf), addrlen);
3415+
#else
3416+
n = sendto(s->sock_fd, buf, len, flags,
3417+
SAS2SA(&addrbuf), addrlen);
3418+
#endif
3419+
}
33933420
Py_END_ALLOW_THREADS
33943421

33953422
if (timeout == 1) {

0 commit comments

Comments
 (0)