gh-153539: Fix use-after-free in TextIOWrapper.tell() with a reentrant decoder - #153540
Merged
serhiy-storchaka merged 2 commits intoAug 17, 2026
Merged
Conversation
…entrant decoder TextIOWrapper.tell() used a borrowed next_input from the snapshot across the decoder's getstate/decode/setstate calls, so a decoder that reenters seek() from getstate could free it and leave tell() reading freed memory. Own the reference across those calls, matching the sibling textiowrapper_read_chunk.
serhiy-storchaka
approved these changes
Aug 17, 2026
serhiy-storchaka
left a comment
Member
There was a problem hiding this comment.
LGTM. 👍
But the main problem is concurrency -- if two threads call seek() concurrently they can reproduce this bug. Please reflect this in NEWS and comments.
Contributor
Author
|
Done in e011fa3: noted the concurrent |
|
Thanks @tonghuaroot for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15. |
brittanyrey
pushed a commit
to brittanyrey/cpython
that referenced
this pull request
Aug 18, 2026
…entrant decoder (pythonGH-153540) TextIOWrapper.tell() used a borrowed next_input from the snapshot across the decoder's getstate/decode/setstate calls, so a decoder that reenters seek() from getstate could free it and leave tell() reading freed memory. Own the reference across those calls, matching the sibling textiowrapper_read_chunk.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
TextIOWrapper.tell()used a borrowednext_inputfrom the snapshot across the decoder'sgetstate/decode/setstatecalls, so a decoder that re-entersseek()fromgetstatecould free it and leavetell()reading freed memory. Own the reference across those calls, matchingtextiowrapper_read_chunk.