[Python-checkins] cpython (merge 3.5 -> default): Issue #23804: Merge SSL recv() fix from 3.5
martin.panter
python-checkins at python.org
Sun Jul 10 21:38:07 EDT 2016
https://hg.python.org/cpython/rev/4ef2404d343e
changeset: 102320:4ef2404d343e
parent: 102317:bd90a39c9d66
parent: 102318:74856df7e55b
user: Martin Panter <vadmium+py at gmail.com>
date: Mon Jul 11 01:32:09 2016 +0000
summary:
Issue #23804: Merge SSL recv() fix from 3.5
files:
Lib/test/test_ssl.py | 29 +++++++++++++++++++++--------
Misc/NEWS | 3 +++
Modules/_ssl.c | 8 ++++++++
3 files changed, 32 insertions(+), 8 deletions(-)
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -2788,20 +2788,13 @@
# consume data
s.read()
+ # read(-1, buffer) is supported, even though read(-1) is not
data = b"data"
-
- # read(-1, buffer) is supported, even though read(-1) is not
s.send(data)
buffer = bytearray(len(data))
self.assertEqual(s.read(-1, buffer), len(data))
self.assertEqual(buffer, data)
- # recv/read(0) should return no data
- s.send(data)
- self.assertEqual(s.recv(0), b"")
- self.assertEqual(s.read(0), b"")
- self.assertEqual(s.read(), data)
-
# Make sure sendmsg et al are disallowed to avoid
# inadvertent disclosure of data and/or corruption
# of the encrypted data stream
@@ -2817,6 +2810,26 @@
s.close()
+ def test_recv_zero(self):
+ server = ThreadedEchoServer(CERTFILE)
+ server.__enter__()
+ self.addCleanup(server.__exit__, None, None)
+ s = socket.create_connection((HOST, server.port))
+ self.addCleanup(s.close)
+ s = ssl.wrap_socket(s, suppress_ragged_eofs=False)
+ self.addCleanup(s.close)
+
+ # recv/read(0) should return no data
+ s.send(b"data")
+ self.assertEqual(s.recv(0), b"")
+ self.assertEqual(s.read(0), b"")
+ self.assertEqual(s.read(), b"data")
+
+ # Should not block if the other end sends no data
+ s.setblocking(False)
+ self.assertEqual(s.recv(0), b"")
+ self.assertEqual(s.recv_into(bytearray()), 0)
+
def test_nonblocking_send(self):
server = ThreadedEchoServer(CERTFILE,
certreqs=ssl.CERT_NONE,
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -27,6 +27,9 @@
Library
-------
+- Issue #23804: Fix SSL zero-length recv() calls to not block and not raise
+ an error about unclean EOF.
+
- Issue #27466: Change time format returned by http.cookie.time2netscape,
confirming the netscape cookie format and making it consistent with
documentation.
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -1913,6 +1913,10 @@
dest = PyBytes_FromStringAndSize(NULL, len);
if (dest == NULL)
goto error;
+ if (len == 0) {
+ Py_XDECREF(sock);
+ return dest;
+ }
mem = PyBytes_AS_STRING(dest);
}
else {
@@ -1924,6 +1928,10 @@
"maximum length can't fit in a C 'int'");
goto error;
}
+ if (len == 0) {
+ count = 0;
+ goto done;
+ }
}
}
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list