[Python-checkins] cpython (merge 3.3 -> default): Issue #18025: Fixed a segfault in io.BufferedIOBase.readinto() when raw

serhiy.storchaka python-checkins at python.org
Tue May 28 15:28:15 CEST 2013


http://hg.python.org/cpython/rev/ad56dff3602f
changeset:   83956:ad56dff3602f
parent:      83954:96e543ba96a4
parent:      83955:b864f4056b78
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Tue May 28 16:27:08 2013 +0300
summary:
  Issue #18025: Fixed a segfault in io.BufferedIOBase.readinto() when raw
stream's read() returns more bytes than requested.

files:
  Lib/test/test_io.py      |  9 +++++++++
  Misc/NEWS                |  3 +++
  Modules/_io/bufferedio.c |  8 ++++++++
  3 files changed, 20 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -3027,6 +3027,15 @@
 class CMiscIOTest(MiscIOTest):
     io = io
 
+    def test_readinto_buffer_overflow(self):
+        # Issue #18025
+        class BadReader(self.io.BufferedIOBase):
+            def read(self, n=-1):
+                return b'x' * 10**6
+        bufio = BadReader()
+        b = bytearray(2)
+        self.assertRaises(ValueError, bufio.readinto, b)
+
 class PyMiscIOTest(MiscIOTest):
     io = pyio
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -96,6 +96,9 @@
 Library
 -------
 
+- Issue #18025: Fixed a segfault in io.BufferedIOBase.readinto() when raw
+  stream's read() returns more bytes than requested.
+
 - Issue #18011: base64.b32decode() now raises a binascii.Error if there are
   non-alphabet characters present in the input string to conform a docstring.
   Updated the module documentation.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -69,6 +69,14 @@
     }
 
     len = Py_SIZE(data);
+    if (len > buf.len) {
+        PyErr_Format(PyExc_ValueError,
+                     "read() returned too much data: "
+                     "%zd bytes requested, %zd returned",
+                     buf.len, len);
+        Py_DECREF(data);
+        goto error;
+    }
     memcpy(buf.buf, PyBytes_AS_STRING(data), len);
 
     PyBuffer_Release(&buf);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list