Skip to content

Commit 0443f92

Browse files
author
Alexey Izbyshev
authored
[3.11] bpo-35191: Fix unexpected integer truncation in socket.setblocking() (GH-10415)
On platforms with 64-bit long, socket.setblocking(x) treated all x which lower 32 bits are zero as False due to integer truncation. Reported by ubsan.
1 parent eb3c0dc commit 0443f92

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

Lib/test/test_socket.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4705,10 +4705,10 @@ def testSetBlocking_overflow(self):
47054705
self.skipTest('needs UINT_MAX < ULONG_MAX')
47064706

47074707
self.serv.setblocking(False)
4708-
self.assertEqual(self.serv.gettimeout(), 0.0)
4708+
self.assert_sock_timeout(self.serv, 0.0)
47094709

47104710
self.serv.setblocking(_testcapi.UINT_MAX + 1)
4711-
self.assertIsNone(self.serv.gettimeout())
4711+
self.assert_sock_timeout(self.serv, None)
47124712

47134713
_testSetBlocking_overflow = support.cpython_only(_testSetBlocking)
47144714

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix unexpected integer truncation in :meth:`socket.setblocking` which caused
2+
it to interpret multiples of ``2**32`` as ``False``.

Modules/socketmodule.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2815,12 +2815,14 @@ For IP sockets, the address info is a pair (hostaddr, port).");
28152815
static PyObject *
28162816
sock_setblocking(PySocketSockObject *s, PyObject *arg)
28172817
{
2818-
long block;
2818+
long value;
2819+
int block;
28192820

2820-
block = PyLong_AsLong(arg);
2821-
if (block == -1 && PyErr_Occurred())
2821+
value = PyLong_AsLong(arg);
2822+
if (value == -1 && PyErr_Occurred())
28222823
return NULL;
28232824

2825+
block = (value != 0);
28242826
s->sock_timeout = _PyTime_FromSeconds(block ? -1 : 0);
28252827
if (internal_setblocking(s, block) == -1) {
28262828
return NULL;

0 commit comments

Comments
 (0)