Skip to content

Commit a5fc509

Browse files
authored
pythongh-112804: Clamping timeout value for _PySemaphore_PlatformWait (pythongh-124914)
* pythongh-112804: Clamping timeout value for _PySemaphore_PlatformWait * Address code review * nit
1 parent adfe765 commit a5fc509

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

Python/parking_lot.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,14 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout)
102102
millis = INFINITE;
103103
}
104104
else {
105-
millis = (DWORD) (timeout / 1000000);
105+
PyTime_t div = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT);
106+
// Prevent overflow with clamping the result
107+
if ((PyTime_t)PY_DWORD_MAX < div) {
108+
millis = PY_DWORD_MAX;
109+
}
110+
else {
111+
millis = (DWORD) div;
112+
}
106113
}
107114
wait = WaitForSingleObjectEx(sema->platform_sem, millis, FALSE);
108115
if (wait == WAIT_OBJECT_0) {

0 commit comments

Comments
 (0)