-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
gh-141311: Avoid assertion in BytesIO readinto #141333
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
base: main
Are you sure you want to change the base?
Changes from all commits
db81df6
34ed126
2c84efe
d119904
d78cc84
4c12be7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix assertion failure in :func:`!io.BytesIO.readinto` and possible | ||
| undefined behavior in :class:`io.BytesIO` when the current position | ||
| is at or near :data:`sys.maxsize`. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -436,6 +436,15 @@ read_bytes_lock_held(bytesio *self, Py_ssize_t size) | |||||
| return Py_NewRef(self->buf); | ||||||
| } | ||||||
|
|
||||||
| /* gh-141311: avoid overflow with self->buf + self->pos */ | ||||||
| if (self->pos >= PY_SSIZE_T_MAX - size) { | ||||||
| self->pos = PY_SSIZE_T_MAX; | ||||||
| size = 0; | ||||||
| } | ||||||
| if (size == 0) { | ||||||
| return PyBytes_FromStringAndSize(NULL, 0); | ||||||
| } | ||||||
|
|
||||||
| output = PyBytes_AS_STRING(self->buf) + self->pos; | ||||||
| self->pos += size; | ||||||
| return PyBytes_FromStringAndSize(output, size); | ||||||
|
|
@@ -609,8 +618,10 @@ _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer) | |||||
| n = self->string_size - self->pos; | ||||||
| if (len > n) { | ||||||
| len = n; | ||||||
| if (len < 0) | ||||||
| len = 0; | ||||||
| if (len < 0) { | ||||||
| /* gh-141311: avoid overflow with self->buf + self->pos */ | ||||||
|
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. The issue is a "past the end of array", not integer overflow |
||||||
| return PyLong_FromSsize_t(0); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| assert(self->pos + len < PY_SSIZE_T_MAX); | ||||||
|
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. Isn't it enough to fix this assertion?
Suggested change
Contributor
Author
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. It will but I worry about the later
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. Good point. If adding PY_SSIZE_T_MAX causes troubles, adding slightly smaller value will also cause troubles. Actually, if pos larger than the size of the array, ot is an undefined behaviour. So, a specoal case for empty read is needed too.
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. Please update also the assert. |
||||||
|
|
||||||
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.
I do not think this is possible. size=0 if self->pos >= self->string_size.