From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:53:32 +0000 (+0200) Subject: [3.13] gh-112804: Clamping timeout value for _PySemaphore_PlatformWait (gh-124914... X-Git-Tag: v3.13.1~395 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=80ba17a3dd383882622cf303a2f2fb19cbaa0ee6;p=thirdparty%2FPython%2Fcpython.git [3.13] gh-112804: Clamping timeout value for _PySemaphore_PlatformWait (gh-124914) (gh-124991) gh-112804: Clamping timeout value for _PySemaphore_PlatformWait (gh-124914) * gh-112804: Clamping timeout value for _PySemaphore_PlatformWait * Address code review * nit (cherry picked from commit a5fc50994a3fae46d0c3d496c4e1d5e00548a1b8) Co-authored-by: Donghee Na --- diff --git a/Python/parking_lot.c b/Python/parking_lot.c index 841b1d71ea16..a7e9760e35d8 100644 --- a/Python/parking_lot.c +++ b/Python/parking_lot.c @@ -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) {