Skip to content

Commit 5ef883b

Browse files
[2.7] bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (GH-3947) (#4088)
Bad remainder in divmod() in intermediate calculations caused an assertion failure.. (cherry picked from commit 4ffd465)
1 parent f7d19b0 commit 5ef883b

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

Lib/test/test_datetime.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,46 @@ def as_hours(self):
492492
self.assertEqual(str(t3), str(t4))
493493
self.assertEqual(t4.as_hours(), -1)
494494

495+
def test_issue31752(self):
496+
# The interpreter shouldn't crash because divmod() returns negative
497+
# remainder.
498+
class BadInt(int):
499+
def __mul__(self, other):
500+
return Prod()
501+
502+
class BadLong(long):
503+
def __mul__(self, other):
504+
return Prod()
505+
506+
class Prod:
507+
def __radd__(self, other):
508+
return Sum()
509+
510+
class Sum(int):
511+
def __divmod__(self, other):
512+
# negative remainder
513+
return (0, -1)
514+
515+
timedelta(microseconds=BadInt(1))
516+
timedelta(hours=BadInt(1))
517+
timedelta(weeks=BadInt(1))
518+
timedelta(microseconds=BadLong(1))
519+
timedelta(hours=BadLong(1))
520+
timedelta(weeks=BadLong(1))
521+
522+
class Sum(long):
523+
def __divmod__(self, other):
524+
# negative remainder
525+
return (0, -1)
526+
527+
timedelta(microseconds=BadInt(1))
528+
timedelta(hours=BadInt(1))
529+
timedelta(weeks=BadInt(1))
530+
timedelta(microseconds=BadLong(1))
531+
timedelta(hours=BadLong(1))
532+
timedelta(weeks=BadLong(1))
533+
534+
495535
#############################################################################
496536
# date tests
497537

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix possible crash in timedelta constructor called with custom integers.

Modules/datetimemodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,7 @@ delta_to_microseconds(PyDateTime_Delta *self)
15371537
if (x2 == NULL)
15381538
goto Done;
15391539
result = PyNumber_Add(x1, x2);
1540+
assert(result == NULL || PyInt_CheckExact(result) || PyLong_CheckExact(result));
15401541

15411542
Done:
15421543
Py_XDECREF(x1);
@@ -1559,6 +1560,7 @@ microseconds_to_delta_ex(PyObject *pyus, PyTypeObject *type)
15591560
PyObject *num = NULL;
15601561
PyObject *result = NULL;
15611562

1563+
assert(PyInt_CheckExact(pyus) || PyLong_CheckExact(pyus));
15621564
tuple = PyNumber_Divmod(pyus, us_per_second);
15631565
if (tuple == NULL)
15641566
goto Done;
@@ -1851,11 +1853,13 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
18511853
assert(num != NULL);
18521854

18531855
if (PyInt_Check(num) || PyLong_Check(num)) {
1854-
prod = PyNumber_Multiply(num, factor);
1856+
prod = PyNumber_Multiply(factor, num);
18551857
if (prod == NULL)
18561858
return NULL;
1859+
assert(PyInt_CheckExact(prod) || PyLong_CheckExact(prod));
18571860
sum = PyNumber_Add(sofar, prod);
18581861
Py_DECREF(prod);
1862+
assert(sum == NULL || PyInt_CheckExact(sum) || PyLong_CheckExact(sum));
18591863
return sum;
18601864
}
18611865

@@ -1898,7 +1902,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
18981902
* fractional part requires float arithmetic, and may
18991903
* lose a little info.
19001904
*/
1901-
assert(PyInt_Check(factor) || PyLong_Check(factor));
1905+
assert(PyInt_CheckExact(factor) || PyLong_CheckExact(factor));
19021906
if (PyInt_Check(factor))
19031907
dnum = (double)PyInt_AsLong(factor);
19041908
else
@@ -1916,6 +1920,7 @@ accum(const char* tag, PyObject *sofar, PyObject *num, PyObject *factor,
19161920
Py_DECREF(sum);
19171921
Py_DECREF(x);
19181922
*leftover += fracpart;
1923+
assert(y == NULL || PyInt_CheckExact(y) || PyLong_CheckExact(y));
19191924
return y;
19201925
}
19211926

0 commit comments

Comments
 (0)