From 6cc71a5c2aa841924d4de4e5773bd7d58cab16ad Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 1 Oct 2024 12:27:25 +0900 Subject: [PATCH 1/3] gh-112804: Clamping timeout value for _PySemaphore_PlatformWait --- Python/parking_lot.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Python/parking_lot.c b/Python/parking_lot.c index 841b1d71ea16cb..4672e3155e4ff6 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -102,7 +102,13 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout) millis = INFINITE; } else { - millis = (DWORD) (timeout / 1000000); + // Prevent overflow with clamping the result + if ((PyTime_t)PY_DWORD_MAX * 1000000 < timeout) { + millis = PY_DWORD_MAX; + } + else { + millis = (DWORD) (timeout / 1000000); + } } wait = WaitForSingleObjectEx(sema->platform_sem, millis, FALSE); if (wait == WAIT_OBJECT_0) { From 68d23368b9fdd71baf369b01cb9f6a777b71d450 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 3 Oct 2024 19:34:10 +0900 Subject: [PATCH 2/3] Address code review --- Python/parking_lot.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Python/parking_lot.c b/Python/parking_lot.c index 4672e3155e4ff6..7b0bb8a8e28155 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -102,12 +102,13 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout) millis = INFINITE; } else { + PyTime_t div = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT); // Prevent overflow with clamping the result - if ((PyTime_t)PY_DWORD_MAX * 1000000 < timeout) { + if ((PyTime_t)PY_DWORD_MAX * 1000000 < div) { millis = PY_DWORD_MAX; } else { - millis = (DWORD) (timeout / 1000000); + millis = (DWORD) div; } } wait = WaitForSingleObjectEx(sema->platform_sem, millis, FALSE); From d629976bc4eec420815abe2dc9ff2b56bac2da5f Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Thu, 3 Oct 2024 19:59:22 +0900 Subject: [PATCH 3/3] nit --- Python/parking_lot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/parking_lot.c b/Python/parking_lot.c index 7b0bb8a8e28155..a7e9760e35d87a 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -104,7 +104,7 @@ _PySemaphore_PlatformWait(_PySemaphore *sema, PyTime_t timeout) else { PyTime_t div = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_TIMEOUT); // Prevent overflow with clamping the result - if ((PyTime_t)PY_DWORD_MAX * 1000000 < div) { + if ((PyTime_t)PY_DWORD_MAX < div) { millis = PY_DWORD_MAX; } else {