Skip to content

gh-153662: Fix crash when calling io.TextIOWrapper.seek() with malformed cookie#153667

Open
ByteFlowing1337 wants to merge 5 commits into
python:mainfrom
ByteFlowing1337:fix-153662
Open

gh-153662: Fix crash when calling io.TextIOWrapper.seek() with malformed cookie#153667
ByteFlowing1337 wants to merge 5 commits into
python:mainfrom
ByteFlowing1337:fix-153662

Conversation

@ByteFlowing1337

@ByteFlowing1337 ByteFlowing1337 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Comment thread Misc/NEWS.d/next/Library/2026-07-13-23-47-14.gh-issue-153662.ei9ya3.rst Outdated
Comment thread Modules/_io/textio.c Outdated
Comment thread Modules/_io/textio.c
/* 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.

Comment thread Lib/test/test_io/test_textio.py Outdated
Comment thread Lib/test/test_io/test_textio.py Outdated
Comment thread Lib/test/test_io/test_textio.py Outdated
Comment thread Lib/test/test_io/test_textio.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants