]> granicus.if.org Git - python/commitdiff
bpo-31752: Fix possible crash in timedelta constructor called with custom integers...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 23 Oct 2017 14:12:28 +0000 (17:12 +0300)
committerGitHub <noreply@github.com>
Mon, 23 Oct 2017 14:12:28 +0000 (17:12 +0300)
Bad remainder in divmod() in intermediate calculations caused an assertion failure.

Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2017-10-11-00-45-01.bpo-31752.DhWevN.rst [new file with mode: 0644]
Modules/_datetimemodule.c

index a042efd878b341661c3cc0b9219ce0ef2915692d..4edfb42d35511ef2256d840d3bb84da7c9c61bd7 100644 (file)
@@ -886,6 +886,26 @@ class TestTimeDelta(HarmlessMixedComparison, unittest.TestCase):
             with self.assertRaises(ValueError):
                 timedelta() * get_bad_float(bad_ratio)
 
+    def test_issue31752(self):
+        # The interpreter shouldn't crash because divmod() returns negative
+        # remainder.
+        class BadInt(int):
+            def __mul__(self, other):
+                return Prod()
+
+        class Prod:
+            def __radd__(self, other):
+                return Sum()
+
+        class Sum(int):
+            def __divmod__(self, other):
+                # negative remainder
+                return (0, -1)
+
+        timedelta(microseconds=BadInt(1))
+        timedelta(hours=BadInt(1))
+        timedelta(weeks=BadInt(1))
+
 
 #############################################################################
 # date tests
diff --git a/Misc/NEWS.d/next/Library/2017-10-11-00-45-01.bpo-31752.DhWevN.rst b/Misc/NEWS.d/next/Library/2017-10-11-00-45-01.bpo-31752.DhWevN.rst
new file mode 100644 (file)
index 0000000..4ec140b
--- /dev/null
@@ -0,0 +1 @@
+Fix possible crash in timedelta constructor called with custom integers.
index fc2cdba70c5e8f3198150eb995e716a02666025f..09b557958a02facd9d5c94de48b8a5ccbf6e7d6c 100644 (file)
@@ -1537,6 +1537,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
     if (x2 == NULL)
         goto Done;
     result = PyNumber_Add(x1, x2);
+    assert(result == NULL || PyLong_CheckExact(result));
 
 Done:
     Py_XDECREF(x1);
@@ -1559,6 +1560,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
     PyObject *num = NULL;
     PyObject *result = NULL;
 
+    assert(PyLong_CheckExact(pyus));
     tuple = PyNumber_Divmod(pyus, us_per_second);
     if (tuple == NULL)
         goto Done;
@@ -2081,11 +2083,13 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
     assert(num != NULL);
 
     if (PyLong_Check(num)) {
-        prod = PyNumber_Multiply(num, factor);
+        prod = PyNumber_Multiply(factor, num);
         if (prod == NULL)
             return NULL;
+        assert(PyLong_CheckExact(prod));
         sum = PyNumber_Add(sofar, prod);
         Py_DECREF(prod);
+        assert(sum == NULL || PyLong_CheckExact(sum));
         return sum;
     }
 
@@ -2128,7 +2132,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
          * fractional part requires float arithmetic, and may
          * lose a little info.
          */
-        assert(PyLong_Check(factor));
+        assert(PyLong_CheckExact(factor));
         dnum = PyLong_AsDouble(factor);
 
         dnum *= fracpart;
@@ -2143,6 +2147,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
         Py_DECREF(sum);
         Py_DECREF(x);
         *leftover += fracpart;
+        assert(y == NULL || PyLong_CheckExact(y));
         return y;
     }