[Python-checkins] Fix division by 0 when checking for overflow in math.prod (GH-11808)

Pablo Galindo webhook-mailer at python.org
Sun Feb 10 14:57:02 EST 2019


https://github.com/python/cpython/commit/4207907c2b8c0b3da62de2acdb8b22b5bbe7f7a2
commit: 4207907c2b8c0b3da62de2acdb8b22b5bbe7f7a2
branch: master
author: Pablo Galindo <Pablogsal at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-02-10T19:56:58Z
summary:

Fix division by 0 when checking for overflow in math.prod (GH-11808)

files:
M Lib/test/test_math.py
M Modules/mathmodule.c

diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py
index 083759ca75e1..856b1e8ac11e 100644
--- a/Lib/test/test_math.py
+++ b/Lib/test/test_math.py
@@ -1756,6 +1756,10 @@ def test_prod(self):
         with self.assertRaises(TypeError):
             prod([10, 20], [30, 40])     # start is a keyword-only argument
 
+        self.assertEqual(prod([0, 1, 2, 3]), 0)
+        self.assertEqual(prod([1, 0, 2, 3]), 0)
+        self.assertEqual(prod(range(10)), 0)
+
 def test_main():
     from doctest import DocFileSuite
     suite = unittest.TestSuite()
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index d2f8d5334736..2272f622f0b9 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -2561,8 +2561,8 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
                 long x = i_result * b;
                 /* Continue if there is no overflow */
                 if (overflow == 0
-                    && x < INT_MAX && x > INT_MIN
-                    && !(b != 0 && x / i_result != b)) {
+                    && x < LONG_MAX && x > LONG_MIN
+                    && !(b != 0 && x / b != i_result)) {
                     i_result = x;
                     Py_DECREF(item);
                     continue;



More information about the Python-checkins mailing list