diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index a395fa0276..ed948bc704 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -14,7 +14,9 @@ #include #include +#include #include +#include #if !defined(Py_LIMITED_API) # include #endif @@ -149,6 +151,41 @@ PYBIND11_DATETIME_ACCESSOR(time_microsecond, time, microsecond, PyDateTime_TIME_ PYBIND11_DATETIME_ACCESSOR(time_fold, time, fold, PyDateTime_TIME_GET_FOLD) #undef PYBIND11_DATETIME_ACCESSOR +// duration_cast does not check that the result fits in an integral rep; a value that does not fit +// is undefined behavior. These return false instead. +template +bool checked_duration_cast(std::chrono::duration src, Duration &dst) { + using rep = typename Duration::rep; + // The value duration_cast truncates to rep; NaN fails both comparisons. + const double count = std::trunc( + std::chrono::duration_cast>(src) + .count()); + const bool fits = !std::is_integral::value + || (count >= static_cast((std::numeric_limits::min)()) + && count < static_cast((std::numeric_limits::max)()) + 1); + if (fits) { + dst = std::chrono::duration_cast(src); + } + return fits; +} + +template +bool checked_duration_cast(std::chrono::microseconds src, Duration &dst) { + using rep = typename Duration::rep; + using cf = std::ratio_divide; + // duration_cast computes us * cf::num / cf::den in (at least) intmax_t, then narrows. + const std::intmax_t us = src.count(); + const bool no_overflow = us <= INTMAX_MAX / cf::num && us >= INTMAX_MIN / cf::num; + const std::intmax_t count = no_overflow ? us * cf::num / cf::den : 0; + const bool fits = !std::is_integral::value + || (no_overflow && !(std::is_unsigned::value && count < 0) + && static_cast(static_cast(count)) == count); + if (fits) { + dst = std::chrono::duration_cast(src); + } + return fits; +} + template class duration_caster { public: @@ -166,17 +203,29 @@ class duration_caster { if (!src) { return false; } + duration d{}; // If invoked with datetime.delta object if (is_timedelta(src)) { - value = type(duration_cast>( - days(timedelta_days(src)) + seconds(timedelta_seconds(src)) - + microseconds(timedelta_microseconds(src)))); + // A timedelta spans +-999999999 days, more than int64 microseconds can hold. + // Its seconds and microseconds are never negative. + const days dd(timedelta_days(src)); + if (dd >= duration_cast(microseconds::max()) + || dd < duration_cast(microseconds::min()) + || !checked_duration_cast( + microseconds(dd + seconds(timedelta_seconds(src)) + + microseconds(timedelta_microseconds(src))), + d)) { + return false; + } + value = type(d); return true; } // If invoked with a float we assume it is seconds and convert if (PyFloat_Check(src.ptr())) { - value = type(duration_cast>( - duration(PyFloat_AsDouble(src.ptr())))); + if (!checked_duration_cast(duration(PyFloat_AsDouble(src.ptr())), d)) { + return false; + } + value = type(d); return true; } return false; @@ -301,8 +350,15 @@ class type_caster> if (!dt) { throw error_already_set(); } - const auto tt = pybind11::int_(dt.attr("timestamp")()).cast(); - value = time_point_cast(system_clock::from_time_t(tt) + msecs); + // Not time_t, which can be 32-bit (e.g. mingw32) while Python's timestamp() is not. + const auto ts = pybind11::int_(dt.attr("timestamp")()).cast(); + // system_clock counts from the time_t epoch. from_time_t would convert to + // system_clock::duration first, and nanoseconds overflow outside about 1678-2262. + Duration d{}; + if (!checked_duration_cast(microseconds(seconds(ts) + msecs), d)) { + return false; + } + value = type(d); return true; } diff --git a/tests/test_chrono.cpp b/tests/test_chrono.cpp index 357c78efd9..4463a5cde3 100644 --- a/tests/test_chrono.cpp +++ b/tests/test_chrono.cpp @@ -80,6 +80,9 @@ TEST_SUBMODULE(chrono, m) { m.def("test_chrono_float_diff", [](std::chrono::duration a, std::chrono::duration b) { return a - b; }); + // test_chrono_duration_out_of_range + m.def("test_chrono_int32_ms", [](std::chrono::duration d) { return d; }); + m.def("test_nano_timepoint", [](timestamp start, timespan delta) -> timestamp { return start + delta; }); diff --git a/tests/test_chrono.py b/tests/test_chrono.py index 34ffceb418..5de1238266 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -419,6 +419,54 @@ def test_floating_point_duration(): assert 665556 <= diff.microseconds <= 665557 +@pytest.mark.parametrize( + "value", + [ + dt.timedelta.max, + dt.timedelta.min, + float("inf"), + float("-inf"), + float("nan"), + 1e300, + ], +) +def test_chrono_duration_out_of_range(value): + # Values that do not fit in int64 microseconds are rejected instead of overflowing + with pytest.raises(TypeError): + m.test_chrono7(value) + + +def test_chrono_duration_out_of_range_boundaries(): + big = dt.timedelta(days=106751990, seconds=86399, microseconds=999999) + assert m.test_chrono7(big) == big + assert m.test_chrono7(-big) == -big + + # 2**31 - 1 milliseconds is about 24.86 days + for value in ( + dt.timedelta(milliseconds=2**31 - 1), + dt.timedelta(milliseconds=-(2**31)), + ): + assert m.test_chrono_int32_ms(value) == value + assert m.test_chrono_int32_ms(-2147483.0) == dt.timedelta(seconds=-2147483) + for value in ( + dt.timedelta(milliseconds=2**31), + dt.timedelta(milliseconds=-(2**31) - 1), + 2147484.0, + ): + with pytest.raises(TypeError): + m.test_chrono_int32_ms(value) + + # int64 nanoseconds cover about 292 years either side of the epoch + with pytest.raises(TypeError): + m.test_nano_timepoint(dt.datetime(2000, 1, 1), dt.timedelta(days=365 * 300)) + try: + dt.datetime(2300, 1, 1).timestamp() + except (OverflowError, OSError): + return # 32-bit time_t + with pytest.raises(TypeError): + m.test_nano_timepoint(dt.datetime(2300, 1, 1), dt.timedelta(0)) + + def test_nano_timepoint(): time = dt.datetime.now() time1 = m.test_nano_timepoint(time, dt.timedelta(seconds=60))