classification
Title: Assertion failure in timedelta() in case of bad __divmod__
Type: crash Stage: resolved
Components: Extension Modules Versions: Python 3.7, Python 3.6, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, belopolsky, louielu, serhiy.storchaka, terry.reedy
Priority: normal Keywords: patch

Created on 2017-10-10 21:29 by serhiy.storchaka, last changed 2017-10-23 16:57 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3947 merged serhiy.storchaka, 2017-10-10 21:49
PR 4086 merged python-dev, 2017-10-23 14:12
PR 4088 merged serhiy.storchaka, 2017-10-23 15:50
Messages (7)
msg304083 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-10 21:29
The following code causes an assertion error in timedelta constructor.

from datetime import timedelta

class BadInt(int):
    def __mul__(self, other):
        return Prod()

class Prod:
    def __radd__(self, other):
        return Sum()

class Sum:
    def __divmod__(self, other):
        return (0, -1)

timedelta(hours=BadInt(1))


Result:

python: /home/serhiy/py/cpython/Modules/_datetimemodule.c:1573: microseconds_to_delta_ex: Assertion `0 <= temp && temp < 1000000' failed.
Aborted (core dumped)
msg304131 - (view) Author: Louie Lu (louielu) * Date: 2017-10-11 13:22
I got a different result in latest commit: eeb5ffd54e5

➜  cpython git:(master) ✗ ./python test.py
Traceback (most recent call last):
  File "test.py", line 15, in <module>
    timedelta(hours=BadInt(1))
SystemError: <class 'datetime.timedelta'> returned NULL without setting an error

Do I miss some configure, I'm using ./configure --with-debug
msg304351 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-10-13 19:32
On Win10, installed 3.5.4, 3.6.3, 3.7.1a1 all raise SystemError.
3.6 and 3.7 repository debug builds raise AssertionError and Windows crash box.  After the patch, a silent crash.
msg304751 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-22 14:27
Unpatched Python should crash in debug build and can raise SystemError in release build.

I can't reproduce a crash on Windows after the patch.
msg304808 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-23 14:12
New changeset 4ffd4653a7ec9c97775472276cf5e159e2366bb2 by Serhiy Storchaka in branch 'master':
bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (#3947)
https://github.com/python/cpython/commit/4ffd4653a7ec9c97775472276cf5e159e2366bb2
msg304810 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-23 15:20
New changeset 6e45d7b90accbbdfef353c41ab0a78a3e4742803 by Serhiy Storchaka (Miss Islington (bot)) in branch '3.6':
bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (GH-3947) (#4086)
https://github.com/python/cpython/commit/6e45d7b90accbbdfef353c41ab0a78a3e4742803
msg304822 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-10-23 16:57
New changeset 5ef883b096895a84123760859f0f34ad37bf2277 by Serhiy Storchaka in branch '2.7':
[2.7] bpo-31752: Fix possible crash in timedelta constructor called with custom integers. (GH-3947) (#4088)
https://github.com/python/cpython/commit/5ef883b096895a84123760859f0f34ad37bf2277
History
Date User Action Args
2017-10-23 16:57:20serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-10-23 16:57:06serhiy.storchakasetmessages: + msg304822
2017-10-23 15:50:28serhiy.storchakasetpull_requests: + pull_request4058
2017-10-23 15:20:16serhiy.storchakasetmessages: + msg304810
2017-10-23 14:12:38python-devsetpull_requests: + pull_request4056
2017-10-23 14:12:34serhiy.storchakasetmessages: + msg304808
2017-10-22 14:27:41serhiy.storchakasetmessages: + msg304751
2017-10-13 19:32:13terry.reedysetnosy: + terry.reedy
messages: + msg304351
2017-10-11 13:22:30louielusetnosy: + louielu
messages: + msg304131
2017-10-10 21:49:17serhiy.storchakasetkeywords: + patch
pull_requests: + pull_request3922
2017-10-10 21:29:28serhiy.storchakacreate