Skip to content

Commit fde82b2

Browse files
Revert "bpo-42854: Use SSL_read/write_ex() (pythonGH-25468)"
This reverts commit 89d1550.
1 parent 136028d commit fde82b2

File tree

3 files changed

+18
-30
lines changed

3 files changed

+18
-30
lines changed

Doc/library/ssl.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,11 +1157,6 @@ SSL Sockets
11571157
to create instances directly. This was never documented or officially
11581158
supported.
11591159

1160-
.. versionchanged:: 3.10
1161-
Python now uses ``SSL_read_ex`` and ``SSL_write_ex`` internally. The
1162-
functions support reading and writing of data larger than 2 GB. Writing
1163-
zero-length data no longer fails with a protocol violation error.
1164-
11651160
SSL sockets also have the following additional methods and attributes:
11661161

11671162
.. method:: SSLSocket.read(len=1024, buffer=None)

Lib/test/test_ssl.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,17 +1130,6 @@ def test_connect_ex_error(self):
11301130
)
11311131
self.assertIn(rc, errors)
11321132

1133-
def test_read_write_zero(self):
1134-
# empty reads and writes now work, bpo-42854, bpo-31711
1135-
client_context, server_context, hostname = testing_context()
1136-
server = ThreadedEchoServer(context=server_context)
1137-
with server:
1138-
with client_context.wrap_socket(socket.socket(),
1139-
server_hostname=hostname) as s:
1140-
s.connect((HOST, server.port))
1141-
self.assertEqual(s.recv(0), b"")
1142-
self.assertEqual(s.send(b""), 0)
1143-
11441133

11451134
class ContextTests(unittest.TestCase):
11461135

Modules/_ssl.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,8 +2352,7 @@ static PyObject *
23522352
_ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b)
23532353
/*[clinic end generated code: output=aa7a6be5527358d8 input=77262d994fe5100a]*/
23542354
{
2355-
size_t count = 0;
2356-
int retval;
2355+
int len;
23572356
int sockstate;
23582357
_PySSLError err;
23592358
int nonblocking;
@@ -2371,6 +2370,12 @@ _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b)
23712370
Py_INCREF(sock);
23722371
}
23732372

2373+
if (b->len > INT_MAX) {
2374+
PyErr_Format(PyExc_OverflowError,
2375+
"string longer than %d bytes", INT_MAX);
2376+
goto error;
2377+
}
2378+
23742379
if (sock != NULL) {
23752380
/* just in case the blocking state of the socket has been changed */
23762381
nonblocking = (sock->sock_timeout >= 0);
@@ -2400,8 +2405,8 @@ _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b)
24002405

24012406
do {
24022407
PySSL_BEGIN_ALLOW_THREADS
2403-
retval = SSL_write_ex(self->ssl, b->buf, (size_t)b->len, &count);
2404-
err = _PySSL_errno(retval == 0, self->ssl, retval);
2408+
len = SSL_write(self->ssl, b->buf, (int)b->len);
2409+
err = _PySSL_errno(len <= 0, self->ssl, len);
24052410
PySSL_END_ALLOW_THREADS
24062411
self->err = err;
24072412

@@ -2434,11 +2439,11 @@ _ssl__SSLSocket_write_impl(PySSLSocket *self, Py_buffer *b)
24342439
err.ssl == SSL_ERROR_WANT_WRITE);
24352440

24362441
Py_XDECREF(sock);
2437-
if (retval == 0)
2438-
return PySSL_SetError(self, retval, __FILE__, __LINE__);
2442+
if (len <= 0)
2443+
return PySSL_SetError(self, len, __FILE__, __LINE__);
24392444
if (PySSL_ChainExceptions(self) < 0)
24402445
return NULL;
2441-
return PyLong_FromSize_t(count);
2446+
return PyLong_FromLong(len);
24422447
error:
24432448
Py_XDECREF(sock);
24442449
PySSL_ChainExceptions(self);
@@ -2488,8 +2493,7 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
24882493
{
24892494
PyObject *dest = NULL;
24902495
char *mem;
2491-
size_t count = 0;
2492-
int retval;
2496+
int count;
24932497
int sockstate;
24942498
_PySSLError err;
24952499
int nonblocking;
@@ -2552,8 +2556,8 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
25522556

25532557
do {
25542558
PySSL_BEGIN_ALLOW_THREADS
2555-
retval = SSL_read_ex(self->ssl, mem, (size_t)len, &count);
2556-
err = _PySSL_errno(retval == 0, self->ssl, retval);
2559+
count = SSL_read(self->ssl, mem, len);
2560+
err = _PySSL_errno(count <= 0, self->ssl, count);
25572561
PySSL_END_ALLOW_THREADS
25582562
self->err = err;
25592563

@@ -2586,8 +2590,8 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
25862590
} while (err.ssl == SSL_ERROR_WANT_READ ||
25872591
err.ssl == SSL_ERROR_WANT_WRITE);
25882592

2589-
if (retval == 0) {
2590-
PySSL_SetError(self, retval, __FILE__, __LINE__);
2593+
if (count <= 0) {
2594+
PySSL_SetError(self, count, __FILE__, __LINE__);
25912595
goto error;
25922596
}
25932597
if (self->exc_type != NULL)
@@ -2600,7 +2604,7 @@ _ssl__SSLSocket_read_impl(PySSLSocket *self, Py_ssize_t len,
26002604
return dest;
26012605
}
26022606
else {
2603-
return PyLong_FromSize_t(count);
2607+
return PyLong_FromLong(count);
26042608
}
26052609

26062610
error:

0 commit comments

Comments
 (0)