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

Skip to content

Commit 5503d47

Browse files
committed
Issue #26644: Raise ValueError for negative SSLSocket.recv() and read()
1 parent 13f0c61 commit 5503d47

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
@@ -2792,6 +2792,13 @@ def _recvfrom_into():
27922792
# consume data
27932793
s.read()
27942794

2795+
# read(-1, buffer) is supported, even though read(-1) is not
2796+
data = b"data"
2797+
s.send(data)
2798+
buffer = bytearray(len(data))
2799+
self.assertEqual(s.read(-1, buffer), len(data))
2800+
self.assertEqual(buffer, data)
2801+
27952802
# Make sure sendmsg et al are disallowed to avoid
27962803
# inadvertent disclosure of data and/or corruption
27972804
# of the encrypted data stream
@@ -2801,6 +2808,10 @@ def _recvfrom_into():
28012808
s.recvmsg_into, bytearray(100))
28022809

28032810
s.write(b"over\n")
2811+
2812+
self.assertRaises(ValueError, s.recv, -1)
2813+
self.assertRaises(ValueError, s.read, -1)
2814+
28042815
s.close()
28052816

28062817
def test_nonblocking_send(self):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ Core and Builtins
9494
Library
9595
-------
9696

97+
- Issue #26644: Raise ValueError rather than SystemError when a negative
98+
length is passed to SSLSocket.recv() or read().
99+
97100
- Issue #26616: Fixed a bug in datetime.astimezone() method.
98101

99102
- Issue #21925: :func:`warnings.formatwarning` now catches exceptions on

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)