This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: crashes in multiply_float_timedelta() and in truedivide_timedelta_float()
Type: crash Stage: resolved
Components: Extension Modules Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, belopolsky, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2017-08-28 14:36 by Oren Milman, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3227 merged Oren Milman, 2017-08-28 15:35
PR 3654 merged python-dev, 2017-09-19 12:58
Messages (7)
msg300954 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-28 14:36
both of the following true division and multiplication operations crash the
interpreter:

import datetime

class BadFloat(float):
    def as_integer_ratio(self):
        return (1 << 1000) - 1

datetime.timedelta() / BadFloat()
datetime.timedelta() * BadFloat()


this is because both multiply_float_timedelta() and truedivide_timedelta_float()
(in Modules/_datetimemodule.c) assume as_integer_ratio() returns tuple.
msg300955 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-28 14:43
i am working on a patch.

BTW, is there anywhere a list of what counts as an extension
module, and what counts as the interpreter core?
msg300956 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-28 15:24
I don't know if there is a list, but definitely the parser, the compiler, the evaluation loop, builtin types and few builtin modules (like builtins and sys) are interpreter core. The _datetime module is an accelerator of the datetime module. It is optional and definitely is not in the core.
msg300957 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-08-28 17:57
Please check if there is similar issue with Decimal.
msg300958 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-08-28 18:23
I guess you meant for me to check whether the following has any problem:
import decimal

class BadFloat(float):
    def as_integer_ratio(self):
        return 1 << 1000

decimal.Decimal.from_float(BadFloat())

so it doesn't crash.
if IIUC, this is because the C implementation of Decimal.from_float()
is PyDecType_FromFloat(), while the relevant part of it is a call to
PyDecType_FromFloatExact().
But PyDecType_FromFloatExact() uses float.as_integer_ratio(), and thus
ISTM that the issue doesn't exist there.

(also, the following doesn't raise an exception, as expected:
import decimal

class BadFloat(float):
    def as_integer_ratio(self):
        raise RuntimeError

decimal.Decimal.from_float(BadFloat())
)
msg302520 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-19 12:58
New changeset 865e4b4f630e2ae91e61239258abb58b488f1d65 by Serhiy Storchaka (Oren Milman) in branch 'master':
bpo-31293: Fix crashes in truediv and mul of a timedelta by a float with a bad as_integer_ratio() method. (#3227)
https://github.com/python/cpython/commit/865e4b4f630e2ae91e61239258abb58b488f1d65
msg302528 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-19 14:00
New changeset f37dd11f0d4832c15d49c2ddc83d533ddaa36e74 by Serhiy Storchaka (Miss Islington (bot)) in branch '3.6':
[3.6] bpo-31293: Fix crashes in truediv and mul of a timedelta by a float with a bad as_integer_ratio() method. (GH-3227) (#3654)
https://github.com/python/cpython/commit/f37dd11f0d4832c15d49c2ddc83d533ddaa36e74
History
Date User Action Args
2022-04-11 14:58:51adminsetgithub: 75474
2017-09-19 14:01:15serhiy.storchakasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.6
2017-09-19 14:00:46serhiy.storchakasetmessages: + msg302528
2017-09-19 12:58:25python-devsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request3647
2017-09-19 12:58:13serhiy.storchakasetmessages: + msg302520
2017-08-28 18:23:30Oren Milmansetmessages: + msg300958
2017-08-28 17:57:52serhiy.storchakasetmessages: + msg300957
2017-08-28 15:35:57Oren Milmansetpull_requests: + pull_request3270
2017-08-28 15:24:47serhiy.storchakasetmessages: + msg300956
2017-08-28 14:43:56Oren Milmansetmessages: + msg300955
2017-08-28 14:42:04serhiy.storchakasetcomponents: + Extension Modules, - Interpreter Core
2017-08-28 14:41:55serhiy.storchakasetnosy: + belopolsky, serhiy.storchaka

stage: needs patch
2017-08-28 14:36:40Oren Milmancreate