Skip to content

Commit 7c62bd5

Browse files
authored
[3.13] gh-141311: Avoid assertion in BytesIO.readinto() (GH-141333) (GH-141478)
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. (cherry picked from commit 7d54374)
1 parent fffd38b commit 7c62bd5

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

Lib/test/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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,13 @@ read_bytes(bytesio *self, Py_ssize_t size)
407407
return Py_NewRef(self->buf);
408408
}
409409

410+
/* gh-141311: Avoid undefined behavior when self->pos (limit PY_SSIZE_T_MAX)
411+
is beyond the size of self->buf. Assert above validates size is always in
412+
bounds. When self->pos is out of bounds calling code sets size to 0. */
413+
if (size == 0) {
414+
return PyBytes_FromStringAndSize(NULL, 0);
415+
}
416+
410417
output = PyBytes_AS_STRING(self->buf) + self->pos;
411418
self->pos += size;
412419
return PyBytes_FromStringAndSize(output, size);
@@ -575,13 +582,16 @@ _io_BytesIO_readinto_impl(bytesio *self, Py_buffer *buffer)
575582
n = self->string_size - self->pos;
576583
if (len > n) {
577584
len = n;
578-
if (len < 0)
579-
len = 0;
585+
if (len < 0) {
586+
/* gh-141311: Avoid undefined behavior when self->pos (limit
587+
PY_SSIZE_T_MAX) points beyond the size of self->buf. */
588+
return PyLong_FromSsize_t(0);
589+
}
580590
}
581591

582-
memcpy(buffer->buf, PyBytes_AS_STRING(self->buf) + self->pos, len);
583-
assert(self->pos + len < PY_SSIZE_T_MAX);
592+
assert(self->pos + len <= PY_SSIZE_T_MAX);
584593
assert(len >= 0);
594+
memcpy(buffer->buf, PyBytes_AS_STRING(self->buf) + self->pos, len);
585595
self->pos += len;
586596

587597
return PyLong_FromSsize_t(len);

0 commit comments

Comments
 (0)