Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afd465d

Browse files
committed
Issue #26644: Merge SSL negative read fix from 3.5
2 parents 3625af5 + 5503d47 commit afd465d

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

Lib/test/test_ssl.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,13 @@ def _recvfrom_into():
27832783
# consume data
27842784
s.read()
27852785

2786+
# read(-1, buffer) is supported, even though read(-1) is not
2787+
data = b"data"
2788+
s.send(data)
2789+
buffer = bytearray(len(data))
2790+
self.assertEqual(s.read(-1, buffer), len(data))
2791+
self.assertEqual(buffer, data)
2792+
27862793
# Make sure sendmsg et al are disallowed to avoid
27872794
# inadvertent disclosure of data and/or corruption
27882795
# of the encrypted data stream
@@ -2792,6 +2799,10 @@ def _recvfrom_into():
27922799
s.recvmsg_into, bytearray(100))
27932800

27942801
s.write(b"over\n")
2802+
2803+
self.assertRaises(ValueError, s.recv, -1)
2804+
self.assertRaises(ValueError, s.read, -1)
2805+
27952806
s.close()
27962807

27972808
def test_nonblocking_send(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ Core and Builtins
232232
Library
233233
-------
234234

235+
- Issue #26644: Raise ValueError rather than SystemError when a negative
236+
length is passed to SSLSocket.recv() or read().
237+
235238
- Issue #26616: Fixed a bug in datetime.astimezone() method.
236239

237240
- Issue #26637: The :mod:`importlib` module now emits an :exc:`ImportError`

Modules/_ssl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,11 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, int len, int group_right_1,
18951895
_PyTime_t timeout, deadline = 0;
18961896
int has_timeout;
18971897

1898+
if (!group_right_1 && len < 0) {
1899+
PyErr_SetString(PyExc_ValueError, "size should not be negative");
1900+
return NULL;
1901+
}
1902+
18981903
if (sock != NULL) {
18991904
if (((PyObject*)sock) == Py_None) {
19001905
_setSSLError("Underlying socket connection gone",

0 commit comments

Comments
 (0)