Skip to content

Commit 7d54374

Browse files
authored
gh-141311: Avoid assertion in BytesIO.readinto() (GH-141333)
Fix error in assertion which causes failure if pos is equal to PY_SSIZE_T_MAX. Fix undefined behavior in read() and readinto() if pos is larger that the size of the underlying buffer.
1 parent 20f53df commit 7d54374

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

Lib/test/test_io/test_memoryio.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def testSeek(self):
5454
self.assertEqual(buf[3:], bytesIo.read())
5555
self.assertRaises(TypeError, bytesIo.seek, 0.0)
5656

57+
self.assertEqual(sys.maxsize, bytesIo.seek(sys.maxsize))
58+
self.assertEqual(self.EOF, bytesIo.read(4))
59+
60+
self.assertEqual(sys.maxsize - 2, bytesIo.seek(sys.maxsize - 2))
61+
self.assertEqual(self.EOF, bytesIo.read(4))
62+
5763
def testTell(self):
5864
buf = self.buftype("1234567890")
5965
bytesIo = self.ioclass(buf)
@@ -552,6 +558,14 @@ def test_relative_seek(self):
552558
memio.seek(1, 1)
553559
self.assertEqual(memio.read(), buf[1:])
554560

561+
def test_issue141311(self):
562+
memio = self.ioclass()
563+
# Seek allows PY_SSIZE_T_MAX, read should handle that.
564+
# Past end of buffer read should always return 0 (EOF).
565+
self.assertEqual(sys.maxsize, memio.seek(sys.maxsize))
566+
buf = bytearray(2)
567+
self.assertEqual(0, memio.readinto(buf))
568+
555569
def test_unicode(self):
556570
memio = self.ioclass()
557571

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix assertion failure in :func:`!io.BytesIO.readinto` and undefined behavior
2+
arising when read position is above capcity in :class:`io.BytesIO`.

Modules/_io/bytesio.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ read_bytes_lock_held(bytesio *self, Py_ssize_t size)
436436
return Py_NewRef(self->buf);
437437
}
438438

439+
/* gh-141311: Avoid undefined behavior when self->pos (limit PY_SSIZE_T_MAX)
440+
is beyond the size of self->buf. Assert above validates size is always in
441+
bounds. When self->pos is out of bounds calling code sets size to 0. */
442+
if (size == 0) {
443+
return PyBytes_FromStringAndSize(NULL, 0);
444+
}
445+
439446
output = PyBytes_AS_STRING(self->buf) + self->pos;
440447
self->pos += size;
441448
return PyBytes_FromStringAndSize(output, size);
@@ -609,11 +616,14 @@ _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer)
609616
n = self->string_size - self->pos;
610617
if (len > n) {
611618
len = n;
612-
if (len < 0)
613-
len = 0;
619+
if (len < 0) {
620+
/* gh-141311: Avoid undefined behavior when self->pos (limit
621+
PY_SSIZE_T_MAX) points beyond the size of self->buf. */
622+
return PyLong_FromSsize_t(0);
623+
}
614624
}
615625

616-
assert(self->pos + len < PY_SSIZE_T_MAX);
626+
assert(self->pos + len <= PY_SSIZE_T_MAX);
617627
assert(len >= 0);
618628
memcpy(buffer->buf, PyBytes_AS_STRING(self->buf) + self->pos, len);
619629
self->pos += len;

0 commit comments

Comments
 (0)