Commit 4207907c authored by Pablo Galindo's avatar Pablo Galindo Committed by GitHub

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

parent 181835d5
...@@ -1756,6 +1756,10 @@ class IsCloseTests(unittest.TestCase): ...@@ -1756,6 +1756,10 @@ class IsCloseTests(unittest.TestCase):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
prod([10, 20], [30, 40]) # start is a keyword-only argument 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(): def test_main():
from doctest import DocFileSuite from doctest import DocFileSuite
suite = unittest.TestSuite() suite = unittest.TestSuite()
......
...@@ -2561,8 +2561,8 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start) ...@@ -2561,8 +2561,8 @@ math_prod_impl(PyObject *module, PyObject *iterable, PyObject *start)
long x = i_result * b; long x = i_result * b;
/* Continue if there is no overflow */ /* Continue if there is no overflow */
if (overflow == 0 if (overflow == 0
&& x < INT_MAX && x > INT_MIN && x < LONG_MAX && x > LONG_MIN
&& !(b != 0 && x / i_result != b)) { && !(b != 0 && x / b != i_result)) {
i_result = x; i_result = x;
Py_DECREF(item); Py_DECREF(item);
continue; continue;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment