Skip to content

Commit e23bfa2

Browse files
tonghuarootcmaloney
authored andcommitted
[3.14] 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>
1 parent 2012d35 commit e23bfa2

4 files changed

Lines changed: 44 additions & 4 deletions

File tree

‎Lib/_pyio.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,9 +2417,12 @@ def tell(self):
24172417
# be O(1) in most situations (common decoders, sensible input).
24182418
# Actually, it will be exactly 1 for fixed-size codecs (all
24192419
# 8-bit codecs, also UTF-16 and UTF-32).
2420-
skip_bytes = int(self._b2cratio * chars_to_skip)
2420+
skip_estimate = self._b2cratio * chars_to_skip
2421+
if 0 <= skip_estimate <= len(next_input):
2422+
skip_bytes = int(skip_estimate)
2423+
else:
2424+
skip_bytes = 0
24212425
skip_back = 1
2422-
assert skip_bytes <= len(next_input)
24232426
while skip_bytes > 0:
24242427
decoder.setstate((b'', dec_flags))
24252428
# Decode up to temptative start point

‎Lib/test/test_io.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,6 +3373,23 @@ def test_tell_after_readline_with_cr(self):
33733373
remaining = f.read()
33743374
self.assertEqual(remaining, "")
33753375

3376+
def test_tell_after_seek_past_cr(self):
3377+
# tell() must tolerate a stale bytes-per-char ratio after seek().
3378+
for data in (b'one\rtwo\rthree\r',
3379+
('ab\r' + '\u20ac' * 15).encode()):
3380+
expected = data.decode().replace('\r', '\n')
3381+
for i in range(len(expected)):
3382+
with self.subTest(data=data, i=i):
3383+
f = self.TextIOWrapper(self.BytesIO(data),
3384+
encoding='utf-8')
3385+
f.read(i)
3386+
pos = f.tell()
3387+
f.read(1)
3388+
f.seek(pos)
3389+
self.assertEqual(f.read(1), expected[i])
3390+
f.seek(f.tell())
3391+
self.assertEqual(f.read(), expected[i + 1:])
3392+
33763393
def test_seek_with_encoder_state(self):
33773394
f = self.open(os_helper.TESTFN, "w", encoding="euc_jis_2004")
33783395
f.write("\u00e6\u0300")
@@ -4412,6 +4429,17 @@ class PyTextIOWrapperTest(TextIOWrapperTest):
44124429
io = pyio
44134430
shutdown_error = "LookupError: unknown encoding: ascii"
44144431

4432+
def test_tell_with_invalid_b2cratio(self):
4433+
for ratio in (float('nan'), float('inf'), -float('inf'), 1e100):
4434+
with self.subTest(ratio=ratio):
4435+
with self.TextIOWrapper(self.BytesIO(b'abc'),
4436+
encoding='utf-8') as f:
4437+
self.assertEqual(f.read(1), 'a')
4438+
f._b2cratio = ratio
4439+
position = f.tell()
4440+
f.seek(position)
4441+
self.assertEqual(f.read(), 'bc')
4442+
44154443

44164444
class IncrementalNewlineDecoderTest(unittest.TestCase):
44174445

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
@@ -2967,9 +2967,16 @@ _io_TextIOWrapper_tell_impl(textio *self)
29672967

29682968
/* Fast search for an acceptable start point, close to our
29692969
current pos */
2970-
skip_bytes = (Py_ssize_t) (self->b2cratio * chars_to_skip);
2970+
double skip_estimate = self->b2cratio * chars_to_skip;
2971+
if (skip_estimate >= 0 &&
2972+
skip_estimate <= PyBytes_GET_SIZE(next_input))
2973+
{
2974+
skip_bytes = (Py_ssize_t)skip_estimate;
2975+
}
2976+
else {
2977+
skip_bytes = 0;
2978+
}
29712979
skip_back = 1;
2972-
assert(skip_bytes <= PyBytes_GET_SIZE(next_input));
29732980
input = PyBytes_AS_STRING(next_input);
29742981
while (skip_bytes > 0) {
29752982
/* Decode up to temptative start point */

0 commit comments

Comments
 (0)