Issue #26644: Raise ValueError for negative SSLSocket.recv() and read()
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index 86ba655..0f6c510 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -2622,7 +2622,18 @@
# 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):
diff --git a/Misc/NEWS b/Misc/NEWS
index 0ecd9d1..8c193f6 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -61,6 +61,9 @@
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.
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 8f34f95..23d4d5c 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -1695,6 +1695,10 @@
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;