Repository navigation
Fix division by zero when packing coarse or sub-nanosecond time_points - #1193
Merged
Merged
Conversation
The pack and object_with_zone adaptors for std::chrono::time_point computed the nanosecond part as count % (1000000000 / nano_num) * nano_num. When the period is longer than one second (std::chrono::minutes, hours, C++20 sys_days), 1000000000 / nano_num is 0; when it is shorter than a nanosecond (std::pico), nano_num itself is 0. Either way packing divided by zero. Split the time since the epoch into whole seconds and the nanosecond remainder with duration_cast instead. Results for the periods that already worked are unchanged.
wleeaf
force-pushed
the
chrono-pack-coarse-duration
branch
from
October 25, 2026 13:14
6c520e9 to
2b78583
Compare
Contributor
|
@wleeaf , thank you for sending the PR. It looks good to me. |
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.
Packing a
std::chrono::time_pointwhose period is longer than one second crashes with SIGFPE:The same applies to
std::chrono::hoursand C++20std::chrono::sys_days, and tomsgpack::object(tp, zone).The
packandobject_with_zoneadaptors inv1/adaptor/cpp11/chrono.hppcompute the nanosecond part ascount % (1000000000 / nano_num) * nano_num, wherenano_num = num * (1000000000 / den). For a period longer than one second,1000000000 / nano_numis 0. For a period shorter than a nanosecond (std::pico),nano_numitself is 0. Either way the code divides by zero.This change splits the time since the epoch into whole seconds and the nanosecond remainder with
duration_cast, keeping the existing adjustment for negative remainders. The encoded bytes for the periods that already worked are unchanged (checked for seconds, milliseconds, andsystem_clock::time_pointmin/max).Tests: new
system_clock_minutes,system_clock_minutes_negativeandsystem_clock_picosecondscases inmsgpack_cpp11.cpp, plussystem_clock_minutesandsystem_clock_minutes_negativeinobject_with_zone.cpp. Each new case hits the division by zero (UBSan "division by zero" at chrono.hpp:254/305) without the fix and passes with it.