-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
[2.7] bpo-36337: Use ssize_t and size_t for socket.send()/sendall() #12397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
17bf06c
bpo-36337: Use ssize_t and size_t for socket.send()/sendall()
matrixise 4c59062
On Windows, the type of send() length argument is int, not size_t: ba…
matrixise 59c20ea
Fix on Windows
matrixise c78ef94
Fix PEP7
matrixise b4e16ea
convert to long from ssize_t
matrixise 405a5d2
On VMS, sendsegmented(), len type is int, not Py_ssize_t
matrixise 995c41d
Improve the blurb entry
matrixise 3300ad6
avoid a buffer overflow when len is greater than INT_MAX on VMS
matrixise 55f2b36
use Py_ssize_t for sendsegmented
matrixise 40dbaf4
Improvement with Victor
matrixise 84a8d9f
Improvement with the remark of Serhiy
matrixise File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2019-03-18-10-08-30.bpo-36337.QhJnXy.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix buffer overflow in :meth:`~socket.socket.send` and | ||
| :meth:`~socket.socket.sendall` methods of :func:`socket.socket` for data larger | ||
| than 2 GiB. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -625,16 +625,16 @@ set_gaierror(int error) | |
|
|
||
| #ifdef __VMS | ||
| /* Function to send in segments */ | ||
| static int | ||
| sendsegmented(int sock_fd, char *buf, int len, int flags) | ||
| static Py_ssize_t | ||
| sendsegmented(int sock_fd, char *buf, Py_ssize_t len, int flags) | ||
| { | ||
| int n = 0; | ||
| int remaining = len; | ||
| Py_ssize_t remaining = len; | ||
|
|
||
| while (remaining > 0) { | ||
| unsigned int segment; | ||
|
|
||
| segment = (remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : remaining); | ||
| segment = ((size_t)remaining >= SEGMENT_SIZE ? SEGMENT_SIZE : (unsigned int) remaining); | ||
| n = send(sock_fd, buf, segment, flags); | ||
| if (n < 0) { | ||
| return n; | ||
|
|
@@ -2797,7 +2797,8 @@ static PyObject * | |
| sock_send(PySocketSockObject *s, PyObject *args) | ||
| { | ||
| char *buf; | ||
| int len, n = -1, flags = 0, timeout; | ||
| int flags = 0, timeout; | ||
| Py_ssize_t len, n = -1; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return PyInt_FromLong((long)n); below is wrong |
||
| Py_buffer pbuf; | ||
|
|
||
| if (!PyArg_ParseTuple(args, "s*|i:send", &pbuf, &flags)) | ||
|
|
@@ -2813,12 +2814,18 @@ sock_send(PySocketSockObject *s, PyObject *args) | |
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's wrong: sendsegmented() len type is int, not Py_ssize_t. Don't forget the VMS! :-) |
||
| #elif 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); | ||
|
|
@@ -2830,7 +2837,7 @@ sock_send(PySocketSockObject *s, PyObject *args) | |
| PyBuffer_Release(&pbuf); | ||
| if (n < 0) | ||
| return s->errorhandler(); | ||
| return PyInt_FromLong((long)n); | ||
| return PyInt_FromSsize_t(n); | ||
| } | ||
|
|
||
| PyDoc_STRVAR(send_doc, | ||
|
|
@@ -2847,7 +2854,8 @@ static PyObject * | |
| sock_sendall(PySocketSockObject *s, PyObject *args) | ||
| { | ||
| char *buf; | ||
| int len, n = -1, flags = 0, timeout, saved_errno; | ||
| int flags = 0, timeout, saved_errno; | ||
| Py_ssize_t len, n = -1; | ||
| Py_buffer pbuf; | ||
|
|
||
| if (!PyArg_ParseTuple(args, "s*|i:sendall", &pbuf, &flags)) | ||
|
|
@@ -2868,6 +2876,11 @@ sock_sendall(PySocketSockObject *s, PyObject *args) | |
| if (!timeout) { | ||
| #ifdef __VMS | ||
| n = sendsegmented(s->sock_fd, buf, len, flags); | ||
| #elif 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 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, is this cast safe? In C99 ssize_t can be an unsigned long, unless I am missing something. I think you need:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SEGMENT_SIZE is 32 KiB: remaining is only casted for values between [0; 32 KiB]: