Skip to content

gh-160811: fix TextIOWrapper.tell() after seek() past a carriage return - #160812

Merged
cmaloney merged 4 commits into
python:mainfrom
tonghuaroot:textio-tell-seek-cr
Nov 9, 2026
Merged

cmaloney merged 4 commits into
python:mainfrom
tonghuaroot:textio-tell-seek-cr

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

seek() to a cookie with chars_to_skip > 0 replays the input chunk into a new snapshot but kept the bytes-per-char ratio from the previous _read_chunk(), so tell() could compute a skip larger than the snapshot. the C implementation then read past the bytes object, and _pyio returned a position one character too far. this sets the ratio from the replayed chunk in both implementations, the same way _read_chunk() does, and adds a regression test for the C and pure-Python classes. backports to 3.14 and 3.13 need the test moved to Lib/test/test_io.py.

…e return

seek() to a cookie with chars_to_skip replayed the input chunk without updating the bytes-per-char ratio, so tell() could compute skip_bytes larger than the snapshot buffer.  In the C implementation this read past the end of the buffer, and _pyio returned a wrong position.

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's nice to set a working b2cratio ("bytes to character ratio") in this case (empty cookie which indicates EOF), but I think the defensive fix should live in the use of b2cratio inside .tell. Even when the ratio is off, NaN in this case, .tell() should work right. Likely slower but still work right.

@tonghuaroot

Copy link
Copy Markdown
Contributor Author

updated tell to reject an invalid bytes per character estimate and fall back to the byte by byte search. the seek specific ratio updates were removed. added coverage for nan, infinities, and an oversized finite estimate. the full io test suite and refleak check pass.

@cmaloney cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looking good, just some minor comments

Comment thread Modules/_io/textio.c Resolved
Comment thread Lib/_pyio.py Resolved
Comment thread Lib/test/test_io/test_textio.py Outdated

def test_tell_after_seek_past_cr(self):
# tell() must tolerate a stale bytes-per-char ratio after seek().
for data in (b'one\rtwo\rthree\r', 'ab\r\u20ac\u20ac\u20ac'.encode()):

@cmaloney cmaloney Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

On a release build this test passes without the fix (Debug asserts do catch it). Using the issue's longer input ('ab\r' + '\u20ac' * 15) makes CTextIOWrapperTest fail without the fix, in both debug and release builds. Lets add that case.

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.

updated.

@cmaloney
cmaloney merged commit ad126c9 into python:main Nov 9, 2026
60 checks passed
@cmaloney cmaloney added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 9, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @cmaloney for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @tonghuaroot for the PR, and @cmaloney for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @tonghuaroot and @cmaloney, I could not cleanly backport this to 3.14 due to a conflict.

Please backport manually with cherry_picker, see the devguide for more information.

cherry_picker ad126c954cc2f42b31bf383ecb5f1bcadff2973d 3.14

@bedevere-app

bedevere-app Bot commented Nov 9, 2026

Copy link
Copy Markdown

GH-161702 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Nov 9, 2026
@bedevere-app

bedevere-app Bot commented Nov 9, 2026

Copy link
Copy Markdown

GH-161703 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Nov 9, 2026
cmaloney pushed a commit that referenced this pull request Nov 9, 2026
…end of buffer (GH-160812) (#161702)

gh-160811: fix TextIOWrapper.tell() after seek() reading past end of buffer (GH-160812)

TextIOWrapper keeps a "bytes to character" ratio as a float so it can optimize operations. If that got to NaN or Infinity as a value then `.tell()` could compute `skip_bytes` larger than the snapshot buffer. In the C implementation this read past the end of the buffer, and _pyio returned a wrong position.
(cherry picked from commit ad126c9)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
cmaloney added a commit that referenced this pull request Nov 9, 2026
…end of buffer (GH-160812) (#161703)

TextIOWrapper keeps a "bytes to character" ratio as a float so it can optimize operations. If that got to NaN or Infinity as a value then `.tell()` could compute `skip_bytes` larger than the snapshot buffer. In the C implementation this read past the end of the buffer, and _pyio returned a wrong position.
(cherry picked from commit ad126c9)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
gpshead added a commit to gpshead/cpython that referenced this pull request Nov 11, 2026
Resolve conflicts with pythongh-160811 (pythonGH-160812), which fixed the same
TextIOWrapper.tell() out-of-bounds read on main.  Take main's version of
the skip_bytes guard in Lib/_pyio.py and Modules/_io/textio.c, and keep
the tests from both sides.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants