Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/library/socket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,10 @@ Socket Objects
See the Unix manual page :manpage:`recv(2)` for the meaning of the optional argument
*flags*; it defaults to zero.

When the :data:`!socket.MSG_TRUNC` flag is used, only receive up to
*bufsize* bytes even if the C function :c:func:`!recv` returns more
bytes (the real datagram size).

.. versionchanged:: 3.5
If the system call is interrupted and the signal handler does not raise
an exception, the method now retries the system call instead of raising
Expand All @@ -1801,6 +1805,10 @@ Socket Objects
to zero. The format of *address* depends on the address family --- see
:ref:`socket-addresses`.

When the :data:`!socket.MSG_TRUNC` flag is used, only receive up to
*bufsize* bytes even if the C function :c:func:`!recvfrom` returns more
bytes (the real datagram size).

.. versionchanged:: 3.5
If the system call is interrupted and the signal handler does not raise
an exception, the method now retries the system call instead of raising
Expand Down
60 changes: 60 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
HOST = socket_helper.HOST
# test unicode string and carriage return
MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8')
MSG2 = 'Monty Python test'.encode('utf-8')

VSOCKPORT = 1234
AIX = platform.system() == "AIX"
Expand Down Expand Up @@ -3303,6 +3304,65 @@ def testRecvFromNegative(self):
def _testRecvFromNegative(self):
self.cli.sendto(MSG, 0, (HOST, self.port))

# gh-160622: with MSG_TRUNC, recv() and recvfrom() must not return
# more bytes than requested, even if the datagram was longer.
@requireAttrs(socket, "MSG_TRUNC")
@unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE")
def testRecvTrunc(self):
# Truncated read where recv() returns the real size of the datagram
msg = self.serv.recv(len(MSG) - 5, socket.MSG_TRUNC)
self.assertEqual(msg, MSG[:-5])

# Read the whole second datagram
msg = self.serv.recv(len(MSG2) + 5, socket.MSG_TRUNC)
self.assertEqual(msg, MSG2)

def _testRecvTrunc(self):
self.cli.sendto(MSG, 0, (HOST, self.port))
self.cli.sendto(MSG2, 0, (HOST, self.port))

@requireAttrs(socket, "MSG_TRUNC")
@requireAttrs(socket, "MSG_PEEK")
@unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE")
def testRecvTruncPeek(self):
msg = self.serv.recv(len(MSG) - 5, socket.MSG_TRUNC | socket.MSG_PEEK)
self.assertEqual(msg, MSG[:-5])

msg = self.serv.recv(len(MSG))
self.assertEqual(msg, MSG)

def _testRecvTruncPeek(self):
self.cli.sendto(MSG, 0, (HOST, self.port))

@requireAttrs(socket, "MSG_TRUNC")
@unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE")
def testRecvFromTrunc(self):
# Truncated read where recvfrom() returns the real size of the datagram
msg, addr = self.serv.recvfrom(len(MSG) - 5, socket.MSG_TRUNC)
self.assertEqual(msg, MSG[:-5])

# Read the whole second datagram
msg, addr = self.serv.recvfrom(len(MSG2) + 5, socket.MSG_TRUNC)
self.assertEqual(msg, MSG2)

def _testRecvFromTrunc(self):
self.cli.sendto(MSG, 0, (HOST, self.port))
self.cli.sendto(MSG2, 0, (HOST, self.port))

@requireAttrs(socket, "MSG_TRUNC")
@requireAttrs(socket, "MSG_PEEK")
@unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE")
def testRecvFromTruncPeek(self):
msg, addr = self.serv.recvfrom(len(MSG) - 5,
socket.MSG_TRUNC | socket.MSG_PEEK)
self.assertEqual(msg, MSG[:-5])

msg, addr = self.serv.recvfrom(len(MSG))
self.assertEqual(msg, MSG)

def _testRecvFromTruncPeek(self):
self.cli.sendto(MSG, 0, (HOST, self.port))


@unittest.skipUnless(HAVE_SOCKET_UDPLITE,
'UDPLITE sockets required for this test.')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix :meth:`socket.socket.recv` and :meth:`socket.socket.recvfrom` for the
:data:`!socket.MSG_TRUNC` flag: only receive up to *bufsize* bytes even if the
C function :c:func:`!recv` or :c:func:`!recvfrom` returns more bytes (the real
datagram size).
14 changes: 14 additions & 0 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4067,6 +4067,13 @@ _socket_socket_recv_impl(PySocketSockObject *s, Py_ssize_t recvlen,
return NULL;
}

/* With MSG_TRUNC, recv() returns the real size of the datagram,
which may be larger than the buffer. Only recvlen bytes were
written. */
if (outlen > recvlen) {
outlen = recvlen;
}

return PyBytesWriter_FinishWithSize(writer, outlen);
}

Expand Down Expand Up @@ -4243,6 +4250,13 @@ _socket_socket_recvfrom_impl(PySocketSockObject *s, Py_ssize_t recvlen,
goto error;
}

/* With MSG_TRUNC, recvfrom() returns the real size of the datagram,
which may be larger than the buffer. Only recvlen bytes were
written. */
if (outlen > recvlen) {
outlen = recvlen;
}

PyObject *buf = PyBytesWriter_FinishWithSize(writer, outlen);
if (buf == NULL) {
goto error;
Expand Down
Loading