Skip to content

Commit d8f6384

Browse files
[3.15] gh-160811: fix TextIOWrapper.tell() after seek() reading past 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 (童话) <[email protected]>
1 parent 04cfecc commit d8f6384

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎Lib/_pyio.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,9 +2441,12 @@ def tell(self):
24412441
# be O(1) in most situations (common decoders, sensible input).
24422442
# Actually, it will be exactly 1 for fixed-size codecs (all
24432443
# 8-bit codecs, also UTF-16 and UTF-32).
2444-
skip_bytes = int(self._b2cratio * chars_to_skip)
2444+
skip_estimate = self._b2cratio * chars_to_skip
2445+
if 0 <= skip_estimate <= len(next_input):
2446+
skip_bytes = int(skip_estimate)
2447+
else:
2448+
skip_bytes = 0
24452449
skip_back = 1
2446-
assert skip_bytes <= len(next_input)
24472450
while skip_bytes > 0:
24482451
decoder.setstate((b'', dec_flags))
24492452
# Decode up to temptative start point

‎Lib/test/test_io/test_textio.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,22 @@ def test_tell_after_readline_with_cr(self):
706706
remaining = f.read()
707707
self.assertEqual(remaining, "")
708708

709+
def test_tell_after_seek_past_cr(self):
710+
# tell() must tolerate a stale bytes-per-char ratio after seek().
711+
for data in (b'one\rtwo\rthree\r',
712+
('ab\r' + '\u20ac' * 15).encode()):
713+
expected = data.decode().replace('\r', '\n')
714+
for i in range(len(expected)):
715+
with self.subTest(data=data, i=i):
716+
f = self.TextIOWrapper(self.BytesIO(data),
717+
encoding='utf-8')
718+
f.read(i)
719+
pos = f.tell()
720+
f.read(1)
721+
f.seek(pos)
722+
self.assertEqual(f.read(1), expected[i])
723+
f.seek(f.tell())
724+
self.assertEqual(f.read(), expected[i + 1:])
709725

710726
def test_seek_with_encoder_state(self):
711727
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
@@ -1721,6 +1737,17 @@ def readinto(self, b):
17211737
class PyTextIOWrapperTest(TextIOWrapperTest, PyTestCase):
17221738
shutdown_error = "LookupError: unknown encoding: ascii"
17231739

1740+
def test_tell_with_invalid_b2cratio(self):
1741+
for ratio in (float('nan'), float('inf'), -float('inf'), 1e100):
1742+
with self.subTest(ratio=ratio):
1743+
with self.TextIOWrapper(self.BytesIO(b'abc'),
1744+
encoding='utf-8') as f:
1745+
self.assertEqual(f.read(1), 'a')
1746+
f._b2cratio = ratio
1747+
position = f.tell()
1748+
f.seek(position)
1749+
self.assertEqual(f.read(), 'bc')
1750+
17241751

17251752
class IncrementalNewlineDecoderTest:
17261753

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :meth:`io.TextIOWrapper.tell` reading past the end of the buffer after
2+
:meth:`~io.TextIOWrapper.seek`.

‎Modules/_io/textio.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,9 +2953,16 @@ _io_TextIOWrapper_tell_impl(textio *self)
29532953

29542954
/* Fast search for an acceptable start point, close to our
29552955
current pos */
2956-
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
2956+
double skip_estimate = self->b2cratio * chars_to_skip;
2957+
if (skip_estimate >= 0 &&
2958+
skip_estimate <= PyBytes_GET_SIZE(next_input))
2959+
{
2960+
skip_bytes = (Py_ssize_t)skip_estimate;
2961+
}
2962+
else {
2963+
skip_bytes = 0;
2964+
}
29572965
skip_back = 1;
2958-
assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
29592966
input = PyBytes_AS_STRING(next_input);
29602967
while (skip_bytes > 0) {
29612968
/* Decode up to temptative start point */

0 commit comments

Comments
 (0)