Avoid signed overflow in TimeZoneInfo::BreakTime() - #358
Closed
ravi0800 wants to merge 1 commit into
Closed
Conversation
devbww
reviewed
Jul 22, 2026
Comment on lines
+969
to
+970
| // shift * kSecsPer400Years can be beyond the int64 range, so derive | ||
| // the shifted time from the remainder rather than the product. |
Contributor
There was a problem hiding this comment.
Thanks, but:
A change in the pipeline (#357) ensures, in the extended_ case, that transitions_[timecnt - 1].unix_time is non-negative.
Together with a merged change (#352), this means that ...
0 <= unix_time - transitions_[timecnt - 1].unix_time <= 2^59
..., and hence that ...
1 <= shift <= 3,945,739,832,464
And all of that means shift * kSecsPer400Years will no longer overflow. So, we can drop this PR.
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.
BreakTime() maps a time past the last transition of an extended zone back into the supported range by computing a 400-year shift and then multiplying it back out as seconds(shift * kSecsPer400Years). A TZif whose transitions are all negative gets the 2147483647 sentinel appended at the end of Load(), so last_time sits near 2.1e9; looking up a time close to the int64 maximum then makes shift 730692562 and the product 9223372042316409600, past INT64_MAX. UBSan flags both the multiply and the tp - d that follows it. cctz::parse() reaches the same sink on ordinary input, since it looks up time_point::max() for its range check.
The shifted time is last_time + diff % kSecsPer400Years - kSecsPer400Years, which is the value the subtraction was after and always stays inside the int64 range, so the product never has to be formed. TimeLocal() already bounds its own shift * kSecsPer400Years, and keeping the arithmetic here means callers of lookup() don't have to know which instants are safe to ask about. Results for the bundled zoneinfo are unchanged and the existing tests pass.