Repository navigation
Conversation
henryiii
force-pushed
the
chrono-duration-range-check
branch
from
November 20, 2026 20:01
09da22c to
f16550d
Compare
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
force-pushed
the
chrono-duration-range-check
branch
from
November 20, 2026 22:03
f16550d to
015aa26
Compare
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
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Item 16 from #6159, plus the
floatpath. The chrono loaders pass Python values toduration_castandfrom_time_t, which do not check range:duration_caster::loadadds up atimedelta's fields in int64 microseconds, which overflows beyond about 292,000 years (timedelta.max), andduration_castcan overflow again for a finer or narrower target.floatthat is NaN, infinite or too large goes straight to an integral rep, which is undefined behavior.json.loads("1e400")isinf, so a timeout read from a JSON config can hit this.system_clocktime point loader callsfrom_time_t, which converts tosystem_clock::durationfirst, so nanosecond clocks (system_clockon Linux, or an explicitnanosecondstime point) overflow for datetimes outside about 1678-2262.UBSan reports
signed-integer-overflowandfloat-cast-overflow. Without it the bound function receives a wrong, often negative, value. With functions takingstd::chrono::seconds,nanoseconds,duration<int32_t, std::milli>andmilliseconds(Apple clang, macOS arm64):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 fortimedeltaanddatetime, and for floats the same double arithmeticduration_castdoes before truncating. In-range values convert as before, including truncation toward zero for negative timedeltas.Limitations: a
timedeltabeyond int64 microseconds (about ±292,000 years) is now rejected even when the target could hold it, such asstd::chrono::hoursorduration<double>; before, it overflowed. The C++ to Python direction is unchanged.Testing: the new tests in
test_chrono.pyfail without thechrono.hchange (7 failures) and pass with it. The fullpytest(1094 passed) andcpptesttargets 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++20andc++23under-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Werror.prekpasses on the changed files. Intest_cmake_build,installed_functionfails 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::chronoduration andsystem_clocktime point conversions now raiseTypeErrorfortimedelta,floatanddatetimevalues that do not fit the target type, instead of overflowing.