Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,22 @@ def test_read_non_blocking(self):
os.close(r)
os.close(w)

@support.subTests('cookie_value', [
# Set 'cookie.chars_to_skip' to INT_MIN
(100 << (12 * 8)) | (0x80 << (19 * 8)),
# Set 'cookie.chars_to_skip' to INT_MAX
(100 << (12 * 8)) | (0x7FFFFFFF << (16 * 8))
])
def test_seek_reject_invalid_chars_to_skip(self, cookie_value):
# See https://github.com/python/cpython/issues/153662
data = b"1"
raw = io.BytesIO(data)
buf = io.BufferedReader(raw)
tio = io.TextIOWrapper(buf, encoding='utf-8')
tio.read()
with self.assertRaisesRegex(OSError, "can't restore logical file position"):
tio.seek(cookie_value)


class MemviewBytesIO(io.BytesIO):
'''A BytesIO object whose read method returns memoryviews
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a crash when calling :meth:`io.TextIOWrapper.seek` with a malformed cookie.
3 changes: 2 additions & 1 deletion Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,8 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
textiowrapper_set_decoded_chars(self, decoded);

/* Skip chars_to_skip of the decoded characters. */
if (PyUnicode_GetLength(self->decoded_chars) < cookie.chars_to_skip) {
/* gh-153662: Reject negative chars_to_skip */
if (cookie.chars_to_skip < 0 || PyUnicode_GetLength(self->decoded_chars) < cookie.chars_to_skip) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a way to have a helper for validating this? what if the chars to skip are way too large? can we hit an overflow somehow?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there can hardly be an overflow.

If chars_to_skip is too large, in normal cases where self->decoded_chars is not that large, it will be rejected by the second condition, and an OSError is raised.

Even in the case that chars_to_skip is as large as INT_MAX and hasn't been rejected, it's later assigned to self->decoded_chars_used, which is a Py_ssize_t which is hard to overflow on 64-bit platforms.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be hard but it can be crafted, as we did in your tests. So if possible, please add some more tests with malformed cookie values.

PyErr_SetString(PyExc_OSError, "can't restore logical file position");
goto fail;
}
Expand Down
Loading