]> granicus.if.org Git - python/commitdiff
Issue #26644: Raise ValueError for negative SSLSocket.recv() and read()
authorMartin Panter <vadmium+py@gmail.com>
Sun, 27 Mar 2016 05:35:19 +0000 (05:35 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Sun, 27 Mar 2016 05:35:19 +0000 (05:35 +0000)
Lib/test/test_ssl.py
Misc/NEWS
Modules/_ssl.c

index 86ba655eaa7756bfc938deee974cba2ef432d44f..0f6c510ff57644be782be0b08a560943eb89473d 100644 (file)
@@ -2622,7 +2622,18 @@ else:
                         # consume data
                         s.read()
 
+                # read(-1, buffer) is supported, even though read(-1) is not
+                data = b"data"
+                s.send(data)
+                buffer = bytearray(len(data))
+                self.assertEqual(s.read(-1, buffer), len(data))
+                self.assertEqual(buffer, data)
+
                 s.write(b"over\n")
+
+                self.assertRaises(ValueError, s.recv, -1)
+                self.assertRaises(ValueError, s.read, -1)
+
                 s.close()
 
         def test_handshake_timeout(self):
index 0ecd9d1c73b8aa780f329750aad3579b3775978b..8c193f659800a8f1807d09340f6d26046d985e0c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -61,6 +61,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #26644: Raise ValueError rather than SystemError when a negative
+  length is passed to SSLSocket.recv() or read().
+
 - Issue #24266: Ctrl+C during Readline history search now cancels the search
   mode when compiled with Readline 7.
 
index 8f34f955cf083f62cc4c1e10ce614be88cdb4043..23d4d5ceab22075721d1bcb15cf9f4dd37e87540 100644 (file)
@@ -1695,6 +1695,10 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
         goto error;
 
     if ((buf.buf == NULL) && (buf.obj == NULL)) {
+        if (len < 0) {
+            PyErr_SetString(PyExc_ValueError, "size should not be negative");
+            goto error;
+        }
         dest = PyBytes_FromStringAndSize(NULL, len);
         if (dest == NULL)
             goto error;