]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112804: Clamping timeout value for _PySemaphore_PlatformWait (gh-124914)
authorDonghee Na <donghee.na@python.org>
Sat, 5 Oct 2024 02:27:32 +0000 (11:27 +0900)
committerGitHub <noreply@github.com>
Sat, 5 Oct 2024 02:27:32 +0000 (11:27 +0900)
* gh-112804: Clamping timeout value for _PySemaphore_PlatformWait

* Address code review

* nit

Python/parking_lot.c

index 841b1d71ea16cb1183ae154a5110c59cd99a534f..a7e9760e35d87a806f7b4e3b8865cefd56e2e3c2 100644 (file)
@@ -102,7 +102,14 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout)
         millis = INFINITE;
     }
     else {
-        millis = (DWORD) (timeout / 1000000);
+        PyTime_t div = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT);
+        // Prevent overflow with clamping the result
+        if ((PyTime_t)PY_DWORD_MAX < div) {
+            millis = PY_DWORD_MAX;
+        }
+        else {
+            millis = (DWORD) div;
+        }
     }
     wait = WaitForSingleObjectEx(sema->platform_sem, millis, FALSE);
     if (wait == WAIT_OBJECT_0) {