From a1924837223963bc8a4859d5c66da9840eb7857c Mon Sep 17 00:00:00 2001 From: Alhuda Khan Date: Thu, 19 Nov 2026 02:43:20 +0530 Subject: [PATCH 1/4] fix(chrono): reject out-of-range values when loading durations duration_cast and from_time_t do not check range. A timedelta beyond int64 microseconds, a NaN, infinite or too-large float, or a datetime outside the range of a nanosecond clock overflowed (undefined behavior) and produced a wrong, often negative, duration. Check the range before converting and fail the load instead, like the integer caster does for out-of-range ints. --- include/pybind11/chrono.h | 77 ++++++++++++++++++++++++++++++++++++--- tests/test_chrono.cpp | 3 ++ tests/test_chrono.py | 44 ++++++++++++++++++++++ 3 files changed, 118 insertions(+), 6 deletions(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index a395fa0276..13fb0e8daf 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,51 @@ 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 the target rep. For an integral rep, a +// value that does not fit is signed overflow or an out-of-range (or NaN) float conversion, so the +// loaders below reject it instead. +template +bool duration_fits(const Source &, std::false_type /* integral rep */) { + return true; +} + +template +bool duration_fits(std::chrono::duration src, std::true_type /* integral rep */) { + 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()); + return count >= static_cast((std::numeric_limits::min)()) + && count < static_cast((std::numeric_limits::max)()) + 1; +} + +template +bool duration_fits(std::chrono::microseconds src, std::true_type /* integral rep */) { + using rep = typename Duration::rep; + using cf = std::ratio_divide; + // duration_cast computes src * cf::num / cf::den in (at least) intmax_t, then narrows. + const std::intmax_t us = src.count(); + const std::uintmax_t magnitude + = us < 0 ? 0 - static_cast(us) : static_cast(us); + if (magnitude > static_cast((std::numeric_limits::max)()) + / static_cast(cf::num)) { + return false; + } + const std::intmax_t count = us * cf::num / cf::den; + return !(std::is_unsigned::value && count < 0) + && static_cast(static_cast(count)) == count; +} + +template +bool checked_duration_cast(const Source &src, Duration &dst) { + if (!duration_fits(src, std::is_integral{})) { + return false; + } + dst = std::chrono::duration_cast(src); + return true; +} + template class duration_caster { public: @@ -166,17 +213,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; @@ -302,7 +361,13 @@ class type_caster> throw error_already_set(); } const auto tt = pybind11::int_(dt.attr("timestamp")()).cast(); - value = time_point_cast(system_clock::from_time_t(tt) + msecs); + // 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(tt) + 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..bcf0a52bd0 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -419,6 +419,50 @@ 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)) + 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)) From 015aa2646e2c0d225693e079483e11f39c8b189f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 19 Nov 2026 10:36:54 -0500 Subject: [PATCH 2/4] test(chrono): skip year 2300 check with 32-bit time_t Assisted-by: ClaudeCode:claude-opus-5-5 --- tests/test_chrono.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_chrono.py b/tests/test_chrono.py index bcf0a52bd0..5de1238266 100644 --- a/tests/test_chrono.py +++ b/tests/test_chrono.py @@ -459,6 +459,10 @@ def test_chrono_duration_out_of_range_boundaries(): # 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)) From eb779e6c9e321445762efdc7acc1b9f1bac2dcc6 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 20 Nov 2026 20:47:35 -0500 Subject: [PATCH 3/4] fix(chrono): avoid time_t overflow when loading time_point std::time_t can be 32-bit (mingw32) while Python's timestamp() is not. Assisted-by: ClaudeCode:claude-opus-5-5 --- include/pybind11/chrono.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index 13fb0e8daf..aaab786844 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -360,11 +360,12 @@ class type_caster> if (!dt) { throw error_already_set(); } - const auto tt = pybind11::int_(dt.attr("timestamp")()).cast(); + // 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(tt) + msecs), d)) { + if (!checked_duration_cast(microseconds(seconds(ts) + msecs), d)) { return false; } value = type(d); From a4caf8d17fe9b3333b56b0106d434cd61ddc0ff9 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 20 Nov 2026 23:47:15 -0500 Subject: [PATCH 4/4] refactor(chrono): merge duration range-check helpers Assisted-by: ClaudeCode:claude-fable-5-2 --- include/pybind11/chrono.h | 50 ++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/include/pybind11/chrono.h b/include/pybind11/chrono.h index aaab786844..ed948bc704 100644 --- a/include/pybind11/chrono.h +++ b/include/pybind11/chrono.h @@ -151,49 +151,39 @@ 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 the target rep. For an integral rep, a -// value that does not fit is signed overflow or an out-of-range (or NaN) float conversion, so the -// loaders below reject it instead. -template -bool duration_fits(const Source &, std::false_type /* integral rep */) { - return true; -} - +// 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 duration_fits(std::chrono::duration src, std::true_type /* integral rep */) { +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()); - return count >= static_cast((std::numeric_limits::min)()) - && count < static_cast((std::numeric_limits::max)()) + 1; + 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 duration_fits(std::chrono::microseconds src, std::true_type /* integral rep */) { +bool checked_duration_cast(std::chrono::microseconds src, Duration &dst) { using rep = typename Duration::rep; using cf = std::ratio_divide; - // duration_cast computes src * cf::num / cf::den in (at least) intmax_t, then narrows. + // duration_cast computes us * cf::num / cf::den in (at least) intmax_t, then narrows. const std::intmax_t us = src.count(); - const std::uintmax_t magnitude - = us < 0 ? 0 - static_cast(us) : static_cast(us); - if (magnitude > static_cast((std::numeric_limits::max)()) - / static_cast(cf::num)) { - return false; - } - const std::intmax_t count = us * cf::num / cf::den; - return !(std::is_unsigned::value && count < 0) - && static_cast(static_cast(count)) == count; -} - -template -bool checked_duration_cast(const Source &src, Duration &dst) { - if (!duration_fits(src, std::is_integral{})) { - return false; + 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); } - dst = std::chrono::duration_cast(src); - return true; + return fits; } template