mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-140607: Validate returned byte count in RawIOBase.read (#140611)
While `RawIOBase.readinto` should return a count of bytes between 0 and the length of the given buffer, it is not required to. Add validation inside RawIOBase.read() that the returned byte count is valid. Co-authored-by: Shamil <ashm.tech@proton.me> Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
313145eab5
commit
0f0a362768
4 changed files with 30 additions and 3 deletions
|
|
@ -592,6 +592,22 @@ 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 outside
|
||||
# (0, len(buffer)) raises.
|
||||
class Misbehaved(self.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 outside buffer size 1")
|
||||
for bad_size in (2147483647, sys.maxsize, -1, -1000):
|
||||
with self.assertRaises(ValueError):
|
||||
Misbehaved(bad_size).read()
|
||||
|
||||
def test_types_have_dict(self):
|
||||
test = (
|
||||
self.IOBase(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue