Skip to content

Commit 6830b37

Browse files
authored
Merge branch 'main' into gh-148731
2 parents e5c3261 + 1274766 commit 6830b37

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Lib/test/test_asyncio/test_sock_lowlevel.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,27 @@ def test_recvfrom_into(self):
427427
self.loop.run_until_complete(
428428
self._basetest_datagram_recvfrom_into(server_address))
429429

430+
async def _basetest_datagram_recvfrom_into_wrong_size(self, server_address):
431+
# Call sock_sendto() with a size larger than the buffer
432+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
433+
sock.setblocking(False)
434+
435+
buf = bytearray(5000)
436+
data = b'\x01' * 4096
437+
wrong_size = len(buf) + 1
438+
await self.loop.sock_sendto(sock, data, server_address)
439+
with self.assertRaises(ValueError):
440+
await self.loop.sock_recvfrom_into(
441+
sock, buf, wrong_size)
442+
443+
size, addr = await self.loop.sock_recvfrom_into(sock, buf)
444+
self.assertEqual(buf[:size], data)
445+
446+
def test_recvfrom_into_wrong_size(self):
447+
with test_utils.run_udp_echo_server() as server_address:
448+
self.loop.run_until_complete(
449+
self._basetest_datagram_recvfrom_into_wrong_size(server_address))
450+
430451
async def _basetest_datagram_sendto_blocking(self, server_address):
431452
# Sad path, sock.sendto() raises BlockingIOError
432453
# This involves patching sock.sendto() to raise BlockingIOError but
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added buffer boundary check when using ``nbytes`` parameter with
2+
:meth:`!asyncio.AbstractEventLoop.sock_recvfrom_into`. Only
3+
relevant for Windows and the :class:`asyncio.ProactorEventLoop`.

Modules/overlapped.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,11 @@ _overlapped_Overlapped_WSARecvFromInto_impl(OverlappedObject *self,
19101910
}
19111911
#endif
19121912

1913+
if (bufobj->len < (Py_ssize_t)size) {
1914+
PyErr_SetString(PyExc_ValueError, "nbytes is greater than the length of the buffer");
1915+
return NULL;
1916+
}
1917+
19131918
wsabuf.buf = bufobj->buf;
19141919
wsabuf.len = size;
19151920

0 commit comments

Comments
 (0)