Skip to content

gh-126876: Fix socket internal_select() for large timeout #126968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2024
Merged
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
27 changes: 27 additions & 0 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -5132,6 +5132,33 @@ def _testRecv(self):
# send data: recv() will no longer block
self.cli.sendall(MSG)

def testLargeTimeout(self):
# gh-126876: Check that a timeout larger than INT_MAX is replaced with
# INT_MAX in the poll() code path. The following assertion must not
# fail: assert(INT_MIN <= ms && ms <= INT_MAX).
large_timeout = _testcapi.INT_MAX + 1

# test recv() with large timeout
conn, addr = self.serv.accept()
self.addCleanup(conn.close)
try:
conn.settimeout(large_timeout)
except OverflowError:
# On Windows, settimeout() fails with OverflowError, whereas
# we want to test recv(). Just give up silently.
return
msg = conn.recv(len(MSG))

def _testLargeTimeout(self):
# test sendall() with large timeout
large_timeout = _testcapi.INT_MAX + 1
self.cli.connect((HOST, self.port))
try:
self.cli.settimeout(large_timeout)
except OverflowError:
return
self.cli.sendall(MSG)


class FileObjectClassTestCase(SocketConnectedTest):
"""Unit tests for the object returned by socket.makefile()
Expand Down
5 changes: 4 additions & 1 deletion Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,9 @@ internal_select(PySocketSockObject *s, int writing, PyTime_t interval,

/* s->sock_timeout is in seconds, timeout in ms */
ms = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING);
assert(ms <= INT_MAX);
if (ms > INT_MAX) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, indeed my proposal was wrong. Because later ms is casted to int: n = poll(&pollfd, 1, (int)ms);

Thanks for picking this up!

ms = INT_MAX;
}

/* On some OSes, typically BSD-based ones, the timeout parameter of the
poll() syscall, when negative, must be exactly INFTIM, where defined,
Expand All @@ -822,6 +824,7 @@ internal_select(PySocketSockObject *s, int writing, PyTime_t interval,
ms = -1;
#endif
}
assert(INT_MIN <= ms && ms <= INT_MAX);

Py_BEGIN_ALLOW_THREADS;
n = poll(&pollfd, 1, (int)ms);
Expand Down
Loading