FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-141311: Avoid assertion in BytesIO readinto · cmaloney/cpython@db81df6 · GitHub

Repository navigation

Commit db81df6

Browse files
committed
pythongh-141311: Avoid assertion in BytesIO readinto
Account for when self->pos is equal to PY_SSIZE_T_MAX. The length of read was correctly set to zero but the asserts assumed self->pos couldn't reach PY_SSIZE_T_MAX. Return early to avoid edge cases.
1 parent b618731 commit db81df6

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎Lib/test/test_io/test_memoryio.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import pickle
1313
import sys
1414
import weakref
15+
from test.support.import_helper import import_module
1516

1617
class IntLike:
1718
def __init__(self, num):
@@ -552,6 +553,17 @@ def test_relative_seek(self):
552553
memio.seek(1, 1)
553554
self.assertEqual(memio.read(), buf[1:])
554555

556+
def test_issue141311(self):
557+
_testcapi = import_module("_testcapi")
558+
559+
memio = self.ioclass()
560+
# Seek should allow PY_SSIZE_T_MAX, read should be capped to buffer size.
561+
# Past end of buffer read should always return empty bytes (EOF).
562+
self.assertEqual(_testcapi.PY_SSIZE_T_MAX,
563+
memio.seek(_testcapi.PY_SSIZE_T_MAX))
564+
buf = bytearray(2)
565+
self.assertEqual(0, memio.readinto(buf))
566+
555567
def test_unicode(self):
556568
memio = self.ioclass()
557569

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix assertion failure in :class:`io.BytesIO` implementation of
2+
:func:`~io.BufferedIOBase.readinto` when the current offset is at the max
3+
offset and readinto is called.

‎Modules/_io/bytesio.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,9 @@ _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer)
609609
n = self->string_size - self->pos;
610610
if (len > n) {
611611
len = n;
612-
if (len < 0)
613-
len = 0;
612+
if (len < 0) {
613+
return PyLong_FromSsize_t(0);
614+
}
614615
}
615616

616617
assert(self->pos + len < PY_SSIZE_T_MAX);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL