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

Skip to content

Commit f6b1d66

Browse files
committed
Issue #23804: Fix SSL recv/read(0) to not return 1024 bytes
1 parent ce91387 commit f6b1d66

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

Doc/library/ssl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ SSL Sockets
842842

843843
SSL sockets also have the following additional methods and attributes:
844844

845-
.. method:: SSLSocket.read(len=0, buffer=None)
845+
.. method:: SSLSocket.read(len=1024, buffer=None)
846846

847847
Read up to *len* bytes of data from the SSL socket and return the result as
848848
a ``bytes`` instance. If *buffer* is specified, then read into the buffer

Lib/ssl.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def server_hostname(self):
561561
server hostame is set."""
562562
return self._sslobj.server_hostname
563563

564-
def read(self, len=0, buffer=None):
564+
def read(self, len=1024, buffer=None):
565565
"""Read up to 'len' bytes from the SSL object and return them.
566566
567567
If 'buffer' is provided, read into this buffer and return the number of
@@ -570,7 +570,7 @@ def read(self, len=0, buffer=None):
570570
if buffer is not None:
571571
v = self._sslobj.read(len, buffer)
572572
else:
573-
v = self._sslobj.read(len or 1024)
573+
v = self._sslobj.read(len)
574574
return v
575575

576576
def write(self, data):
@@ -776,7 +776,7 @@ def _check_connected(self):
776776
# EAGAIN.
777777
self.getpeername()
778778

779-
def read(self, len=0, buffer=None):
779+
def read(self, len=1024, buffer=None):
780780
"""Read up to LEN bytes and return them.
781781
Return zero-length string on EOF."""
782782

Lib/test/test_ssl.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2792,13 +2792,20 @@ def _recvfrom_into():
27922792
# consume data
27932793
s.read()
27942794

2795-
# read(-1, buffer) is supported, even though read(-1) is not
27962795
data = b"data"
2796+
2797+
# read(-1, buffer) is supported, even though read(-1) is not
27972798
s.send(data)
27982799
buffer = bytearray(len(data))
27992800
self.assertEqual(s.read(-1, buffer), len(data))
28002801
self.assertEqual(buffer, data)
28012802

2803+
# recv/read(0) should return no data
2804+
s.send(data)
2805+
self.assertEqual(s.recv(0), b"")
2806+
self.assertEqual(s.read(0), b"")
2807+
self.assertEqual(s.read(), data)
2808+
28022809
# Make sure sendmsg et al are disallowed to avoid
28032810
# inadvertent disclosure of data and/or corruption
28042811
# of the encrypted data stream

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ Library
101101
- Issue #26644: Raise ValueError rather than SystemError when a negative
102102
length is passed to SSLSocket.recv() or read().
103103

104+
- Issue #23804: Fix SSL recv(0) and read(0) methods to return zero bytes
105+
instead of up to 1024.
106+
104107
- Issue #26616: Fixed a bug in datetime.astimezone() method.
105108

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

0 commit comments

Comments
 (0)