Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ def read(self, size=-1):
n = self.readinto(b)
if n is None:
return None
if n < 0 or n > len(b):
raise ValueError(f"readinto returned '{n}' outside buffer size '{len(b)}'")
del b[n:]
return bytes(b)

Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_io/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,23 @@ def test_RawIOBase_read(self):
self.assertEqual(rawio.read(2), None)
self.assertEqual(rawio.read(2), b"")

def test_RawIOBase_read_bounds_checking(self):
# Make sure a `.readinto` call which returns a value oustside
# (0, len(buffer)) raises.
class Misbehaved(io.RawIOBase):
def __init__(self, readinto_return) -> None:
self._readinto_return = readinto_return
def readinto(self, b):
return self._readinto_return

with self.assertRaises(ValueError) as cm:
Misbehaved(2).read(1)
self.assertEqual(str(cm.exception), "readinto returned '2' oustside buffer size '1'")
self.assertRaises(ValueError, Misbehaved(2147483647).read)
self.assertRaises(ValueError, Misbehaved(sys.maxsize).read)
self.assertRaises(ValueError, Misbehaved(-1).read)
self.assertRaises(ValueError, Misbehaved(-1000).read)

def test_types_have_dict(self):
test = (
self.IOBase(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Inside :meth:`io.RawIOBase.read` validate that the count of bytes returned by
:meth:`io.RawIOBase.readinto` is reasonable (inside the provided buffer).
13 changes: 10 additions & 3 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,14 +939,21 @@
return res;
}

n = PyNumber_AsSsize_t(res, PyExc_ValueError);
Py_ssize_t bytes_filled = PyNumber_AsSsize_t(res, PyExc_ValueError);
Py_DECREF(res);
if (n == -1 && PyErr_Occurred()) {
if (readinto_len == -1 && PyErr_Occurred()) {

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64)

'readinto_len': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64)

'readinto_len': undeclared identifier [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64)

'readinto_len': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64)

'readinto_len': undeclared identifier [C:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

‘readinto_len’ undeclared (first use in this function)

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

‘readinto_len’ undeclared (first use in this function)

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

‘readinto_len’ undeclared (first use in this function)

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

‘readinto_len’ undeclared (first use in this function)

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

‘readinto_len’ undeclared (first use in this function)

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

‘readinto_len’ undeclared (first use in this function)

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

‘readinto_len’ undeclared (first use in this function)

Check failure on line 944 in Modules/_io/iobase.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘readinto_len’ undeclared (first use in this function)
Py_DECREF(b);
return NULL;
}
if (bytes_filled < 0 || bytes_filled > n) {
Py_DECREF(b);
PyErr_Format(PyExc_ValueError,
"readinto returned '%zd' oustside buffer size '%zd'",
bytes_filled, n);
return NULL;
}

res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), n);
res = PyBytes_FromStringAndSize(PyByteArray_AsString(b), bytes_filled);
Py_DECREF(b);
return res;
}
Expand Down
Loading