[3.14] gh-140607: Validate returned byte count in RawIOBase.read (GH-140611) (#140728)

* [3.14] gh-140607: Validate returned byte count in RawIOBase.read (GH-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.
(cherry picked from commit 0f0a362768)

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Shamil <ashm.tech@proton.me>
Co-authored-by: Victor Stinner <vstinner@python.org>

* fixup: Use older attribute name

---------

Co-authored-by: Shamil <ashm.tech@proton.me>
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Cody Maloney 2025-10-29 05:31:48 -07:00 committed by GitHub
parent 7c4a8e5818
commit 9a7dccd7a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 3 deletions

View file

@ -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)