From 9e5f483bedd63f79c647dd43d809621c4c9b99cf Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Tue, 6 Oct 2020 19:36:11 -0700 Subject: [PATCH] add overflow detection to DIVS overflow shouldn't occur when calling DIVS since it should have been detected in the code calling DIVS to properly handle edge cases. --- src/nmutil/divmod.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/nmutil/divmod.py b/src/nmutil/divmod.py index af5d214..357fdcb 100644 --- a/src/nmutil/divmod.py +++ b/src/nmutil/divmod.py @@ -17,6 +17,9 @@ def trunc_divs(n, d): res = copy(n) if (sign_n == sign_d): res.value = abs_q + if res.value & (1 << (res.bits - 1)) != 0: + # result should be positive but is negative + raise OverflowError() return res mask = (1 << res.bits) - 1 res.value = (-abs_q) & mask -- 2.30.2