Skip to content

fix(chrono): reject out-of-range values when loading durations - #6288

Open
kitkatt7 wants to merge 4 commits into
pybind:masterfrom
kitkatt7:chrono-duration-range-check
Open

kitkatt7 wants to merge 4 commits into
pybind:masterfrom
kitkatt7:chrono-duration-range-check

Conversation

@kitkatt7

Copy link
Copy Markdown

Description

Item 16 from #6159, plus the float path. The chrono loaders pass Python values to duration_cast and from_time_t, which do not check range:

  • duration_caster::load adds up a timedelta's fields in int64 microseconds, which overflows beyond about 292,000 years (timedelta.max), and duration_cast can overflow again for a finer or narrower target.
  • A float that is NaN, infinite or too large goes straight to an integral rep, which is undefined behavior. json.loads("1e400") is inf, so a timeout read from a JSON config can hit this.
  • The system_clock time point loader calls from_time_t, which converts to system_clock::duration first, so nanosecond clocks (system_clock on Linux, or an explicit nanoseconds time point) overflow for datetimes outside about 1678-2262.

UBSan reports signed-integer-overflow and float-cast-overflow. Without it the bound function receives a wrong, often negative, value. With functions taking std::chrono::seconds, nanoseconds, duration<int32_t, std::milli> and milliseconds (Apple clang, macOS arm64):

m.s(dt.timedelta.max)               # was -5833720368547, now TypeError
m.ns(dt.timedelta(days=365 * 300))  # was -8985944073709551616, now TypeError
m.ms32(dt.timedelta(days=30))       # was -1702967296, now TypeError
m.ms(float("nan"))                  # was 0, now TypeError
m.ms(-1e19)                         # was -9223372036854775808, now TypeError

The loaders now check that the value fits before converting and return false otherwise, so the call raises TypeError, as the integer caster does for out-of-range ints. The target range check is exact: integer arithmetic for timedelta and datetime, and for floats the same double arithmetic duration_cast does before truncating. In-range values convert as before, including truncation toward zero for negative timedeltas.

Limitations: a timedelta beyond int64 microseconds (about ±292,000 years) is now rejected even when the target could hold it, such as std::chrono::hours or duration<double>; before, it overflowed. The C++ to Python direction is unchanged.

Testing: the new tests in test_chrono.py fail without the chrono.h change (7 failures) and pass with it. The full pytest (1094 passed) and cpptest targets pass on CPython 3.15.1, Debug, PYBIND11_WERROR=ON, Apple clang 21 on macOS arm64, and the new code compiles cleanly with -std=c++11, c++14, c++20 and c++23 under -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Werror. prek passes on the changed files. In test_cmake_build, installed_function fails locally with and without this change ("did not return an extension module"); the other seven checks pass. Not tested with GCC, MSVC, Linux or Windows.

Suggested changelog entry:

  • std::chrono duration and system_clock time point conversions now raise TypeError for timedelta, float and datetime values that do not fit the target type, instead of overflowing.

kitkatt7 and others added 2 commits November 20, 2026 17:03
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.
Assisted-by: ClaudeCode:claude-opus-5-5
@henryiii
henryiii force-pushed the chrono-duration-range-check branch from f16550d to 015aa26 Compare November 20, 2026 22:03
std::time_t can be 32-bit (mingw32) while Python's timestamp() is not.

Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii henryiii self-assigned this Nov 21, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants