Repository navigation
gh-160811: fix TextIOWrapper.tell() after seek() past a carriage return - #160812
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
|
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
left a comment
There was a problem hiding this comment.
Looking good, just some minor comments
|
|
||
| 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()): |
There was a problem hiding this comment.
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.
|
Thanks @tonghuaroot for the PR, and @cmaloney for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14. |
|
Thanks @tonghuaroot for the PR, and @cmaloney for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15. |
|
Sorry, @tonghuaroot and @cmaloney, I could not cleanly backport this to Please backport manually with cherry_picker, see the devguide for more information. |
|
GH-161702 is a backport of this pull request to the 3.15 branch. |
|
GH-161703 is a backport of this pull request to the 3.14 branch. |
…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>
…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>
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>
seek()to a cookie withchars_to_skip > 0replays the input chunk into a new snapshot but kept the bytes-per-char ratio from the previous_read_chunk(), sotell()could compute a skip larger than the snapshot. the C implementation then read past the bytes object, and_pyioreturned 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 toLib/test/test_io.py.