Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 281945f

Browse files
Issue #18025: Fixed a segfault in io.BufferedIOBase.readinto() when raw
stream's read() returns more bytes than requested.
2 parents 5758fa7 + 37a79a1 commit 281945f

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/test/test_io.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3027,6 +3027,15 @@ def test_open_allargs(self):
30273027
class CMiscIOTest(MiscIOTest):
30283028
io = io
30293029

3030+
def test_readinto_buffer_overflow(self):
3031+
# Issue #18025
3032+
class BadReader(self.io.BufferedIOBase):
3033+
def read(self, n=-1):
3034+
return b'x' * 10**6
3035+
bufio = BadReader()
3036+
b = bytearray(2)
3037+
self.assertRaises(ValueError, bufio.readinto, b)
3038+
30303039
class PyMiscIOTest(MiscIOTest):
30313040
io = pyio
30323041

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ Core and Builtins
9696
Library
9797
-------
9898

99+
- Issue #18025: Fixed a segfault in io.BufferedIOBase.readinto() when raw
100+
stream's read() returns more bytes than requested.
101+
99102
- Issue #18011: base64.b32decode() now raises a binascii.Error if there are
100103
non-alphabet characters present in the input string to conform a docstring.
101104
Updated the module documentation.

Modules/_io/bufferedio.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ bufferediobase_readinto(PyObject *self, PyObject *args)
6969
}
7070

7171
len = Py_SIZE(data);
72+
if (len > buf.len) {
73+
PyErr_Format(PyExc_ValueError,
74+
"read() returned too much data: "
75+
"%zd bytes requested, %zd returned",
76+
buf.len, len);
77+
Py_DECREF(data);
78+
goto error;
79+
}
7280
memcpy(buf.buf, PyBytes_AS_STRING(data), len);
7381

7482
PyBuffer_Release(&buf);

0 commit comments

Comments
 (0)