]> granicus.if.org Git - python/commitdiff
Fix division by 0 when checking for overflow in math.prod (GH-11808)
authorPablo Galindo <Pablogsal@gmail.com>
Sun, 10 Feb 2019 19:56:58 +0000 (19:56 +0000)
committerGitHub <noreply@github.com>
Sun, 10 Feb 2019 19:56:58 +0000 (19:56 +0000)
Lib/test/test_math.py
Modules/mathmodule.c

index 083759ca75e16b815e9eb8cd7642d4132a26fb9a..856b1e8ac11e3bf0dbf6c0d4eb015cf68b1ccaf3 100644 (file)
@@ -1756,6 +1756,10 @@ class IsCloseTests(unittest.TestCase):
         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()
index d2f8d5334736eb235893b42575b0ca4726017876..2272f622f0b9f6e3db9decb85340a8509f9e6825 100644 (file)
@@ -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;