diff --git a/Doc/library/socket.rst b/Doc/library/socket.rst index 054ccc556740c2..a447782efbd7a7 100644 --- a/Doc/library/socket.rst +++ b/Doc/library/socket.rst @@ -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 @@ -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 diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index 51b666bbaba827..73c575436e76cb 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -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" @@ -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.') diff --git a/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-69121.7n56jv.rst b/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-69121.7n56jv.rst new file mode 100644 index 00000000000000..587d393d77c466 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-25-19-04-57.gh-issue-69121.7n56jv.rst @@ -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). diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 97146b1d752c3d..34905db0ef46a0 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -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); } @@ -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;