Kaydet (Commit) 6e789002 authored tarafından Andrew Svetlov's avatar Andrew Svetlov Kaydeden (comit) Miss Islington (bot)

bpo-35589: Prevent buffer copy in sock_sendall() (GH-11418)



No NEWs is needed since the problem was introduced on master only and never released.


https://bugs.python.org/issue35589
üst bfba8c37
...@@ -428,32 +428,35 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop): ...@@ -428,32 +428,35 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
if n == len(data): if n == len(data):
# all data sent # all data sent
return return
else:
data = bytearray(memoryview(data)[n:])
fut = self.create_future() fut = self.create_future()
fd = sock.fileno() fd = sock.fileno()
fut.add_done_callback( fut.add_done_callback(
functools.partial(self._sock_write_done, fd)) functools.partial(self._sock_write_done, fd))
self.add_writer(fd, self._sock_sendall, fut, sock, data) # use a trick with a list in closure to store a mutable state
self.add_writer(fd, self._sock_sendall, fut, sock,
memoryview(data), [n])
return await fut return await fut
def _sock_sendall(self, fut, sock, data): def _sock_sendall(self, fut, sock, view, pos):
if fut.done(): if fut.done():
# Future cancellation can be scheduled on previous loop iteration # Future cancellation can be scheduled on previous loop iteration
return return
start = pos[0]
try: try:
n = sock.send(data) n = sock.send(view[start:])
except (BlockingIOError, InterruptedError): except (BlockingIOError, InterruptedError):
return return
except Exception as exc: except Exception as exc:
fut.set_exception(exc) fut.set_exception(exc)
return return
if n == len(data): start += n
if start == len(view):
fut.set_result(None) fut.set_result(None)
else: else:
del data[:n] pos[0] = start
async def sock_connect(self, sock, address): async def sock_connect(self, sock, address):
"""Connect to a remote socket at address. """Connect to a remote socket at address.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment