Repository navigation
Fix UBSan signed-integer overflow converting extreme DateTime64 to Time/Time64 - #110451
alexey-milovidov merged 8 commits into
Conversation
…me/Time64 toTime/toTime64 computed the local seconds-of-day as (dt64 + offset) % 86400. For an extreme input (dt64 = INT64_MIN) in a timezone with a negative offset the addition overflows Int64 before the modulo, tripping UBSan. Reduce the timestamp modulo 86400 before adding the offset. Mathematically identical since (a + b) mod m == ((a mod m) + b) mod m and the negative case is normalized with += 86400, but the intermediate sum is now bounded by 86400 + max timezone offset, so it cannot overflow. Applied to both Int64 sites (DateTime64 -> Time at FunctionsConversion.h:273 and DateTime64 -> Time64). The UInt32 sites cannot overflow Int64 and are left as is. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-4:20260714-203900 |
|
cc @yariks5s @rschu1ze — could you review this? It fixes a UBSan signed-integer overflow in the DateTime64 -> Time/Time64 conversion: |
|
Workflow [PR], commit [4f1986e] Summary: ✅
AI ReviewSummaryThis PR is back to the intended narrow fix: both Final Verdict
|
The extreme DateTime64 (INT64_MIN/MAX) reproducer used America/New_York / Europe/Moscow, whose timezoneOffset() returns a tzdata-version-dependent LMT-era offset for out-of-range inputs. CI produced 03:33:50 (EST, -5:00) while a local build produced 03:29:52 (NY LMT, -4:56:02) for the same commit, so the exact wall-clock reference was not portable. Switch the extreme cases to fixed-offset Etc/GMT zones, which have no historical transitions and therefore a constant offset even at INT64_MIN/ MAX. The overflow is still triggered (INT64_MIN with a negative offset, INT64_MAX with a positive offset) and the buggy vs fixed results still differ (buggy 03:30:08/22:29:51 vs fixed 20:29:52/05:30:07), so the test remains a valid regression test while being deterministic on every arch and tzdata version. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed 1ff078c: made the extreme-value reference deterministic. Root cause of the red Fast test: the INT64_MIN/MAX cases used Fix: switch the extreme cases to fixed-offset |
…h values The DateTime64 -> Time64 path rescaled the full epoch value with convertDecimals before applying the day modulo, so an extreme input (e.g. INT64_MIN) threw DECIMAL_OVERFLOW even though the wall-clock time-of-day is always representable. It also split the value with truncation toward zero, so a pre-epoch fractional value landed on the wrong wall-clock day (a negative Time64). Split the original DateTime64 into floor-aligned whole seconds plus a non-negative sub-second fraction first, apply the timezone/day modulo to the whole part, then rescale only the bounded fraction and reassemble. convertDecimals never sees the full out-of-range epoch value, and pre-epoch fractions round toward the floor second so the wall-clock day is correct. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Fixed in 584045d. The
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260714-233500 |
clang-tidy/-Werror flagged 'to_fraction' as used-uninitialized in the DateTime64->Time64 branch. It is assigned on both branches before use; initialize to 0 at declaration to satisfy the check. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…o fix-totime-datetime64-int64min-overflow
|
The |
CI finish ledger — 002ffd5CI is fully finished on this head. Every failure has an owner.
The earlier Session id: cron:our-pr-ci-monitor:20260715-070000 |
|
@groeneai investigate the CI failures. Why do you need that much changes in the code while you were fixing only the UBSan? |
|
Both questions answered below. 1. The CI failuresCurrent head is
The host OOM was a fleet-wide event that day, not something this branch caused. In that one check, The branch is also 4966 commits behind master, and the two changes that addressed that cohort are not on it: A GitHub re-run would reuse this same build, so it cannot pick those up. I will merge master into the branch and push, so the next run is real signal rather than a 12-day-old snapshot. The test added by this PR is not implicated either: in the CI database it is 416 OK against 1 FAIL since 2026-07-14, and the single FAIL is the old 2. Why the diff is larger than the UBSan fixFair question. The real diff against the merge base is 3 files, +82/-26, and The UBSan fix proper is 2 changed lines, in the first commit plus the same reorder at the Everything else is one later commit,
The second one is reachable with an ordinary value and no settings at all. On master today: SELECT CAST(toDateTime64('1969-12-31 23:59:59.250', 3, 'UTC') AS Time64(3));
-- master: -00:00:00.750
-- this PR: 23:59:59.250So That said, you are the assignee and you are entitled to a smaller PR. I am happy to split it:
Or, if you would rather not have the second fix at all, say so and I will drop it; the automated review comment is not binding. Tell me which you prefer and I will do it in one push. Until then I am not restructuring the diff, only merging master so CI gives a usable answer. |
Pick up the fleet-wide stateless-test host OOM mitigations (ClickHouse#110503, ClickHouse#110574) so CI on this branch produces real signal. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Master merged and pushed: head is now The merge changed nothing in the fix itself. Against Verified locally after the merge: the tree builds clean with clang-21 ( Still waiting on your call about splitting the |
FunctionFactory resolves toTime to the legacy toTimeWithFixedDate whenever use_legacy_to_time = 1, and the stress runner injects a random compatibility='NN.N' client option that restores that value for versions before 26.7. In that mode the toTime statements of this test return a DateTime instead of a Time, and the values are byte-identical with and without the DateTime64 -> Time fix, so that half of the fix is no longer covered. Pinning the setting keeps the statements on ToTimeImpl::execute(Int64) regardless of the injected compatibility value. The pin is a no-op on the default path: the reference file is unchanged. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110451&sha=6b44de7be8d0a0d1cf5e21a972c708353e729a77&name_0=PR
CI finish ledger — da3a6b7CI fully finished on this head (Finish Workflow pass, Mergeable Check pass, 0 checks queued or in progress): 160 pass, 17 skipped, 0 failures. Nothing to own. The reds recorded on earlier heads are all gone here: the Session id: cron:our-pr-ci-monitor:20260727-220000 |
|
@groeneai, fix conflicts and update the branch. |
Master picked up the floor-aligned seconds split for DateTime64 -> Time64 through ClickHouse#109738, which rewrote the same block this branch touched. Resolved by taking master's structure (it already carries that split and the accurateCastOrNull representability gate) and re-applying only the overflow reorder on top, at both Int64 sites. The Time64 reassembly no longer needs DecimalUtils::multiplyAdd: Time64 caps at scale 9, so the widest value is 86399 * 1e9 + 999999999 = 8.64e13, five orders of magnitude below INT64_MAX. That guard was dead code and is dropped. What remains against master is the 2 line fix the branch exists for.
|
Done, the branch is updated and the conflict is resolved: head is now The conflict was against my own PR #109738, which merged 38 minutes before your comment and rewrote the same That shrank this PR to what it was always meant to be, which also answers @ yariks5s's earlier question about the size of the diff. Against master it is now 2 changed lines of code: and the same reorder at the I also dropped the The fix is still needed on master: with the reorder reverted, the two extreme cases return |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 9/9 (100.00%) · Uncovered code |
|
While it may make the conversion slower, it is unlikely to be important. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a signed-integer overflow (UBSan) when converting an extreme
DateTime64value (e.g.INT64_MIN) toTime/Time64in a timezone with a negative offset.Description
toTime/toTime64computed the local seconds-of-day as(dt64 + offset) % 86400. For an extreme input in a timezone with a negative offset the addition overflowsInt64before the modulo (UBSan:signed integer overflow: -9223372036854775808 + -18000).Fix: reduce the timestamp modulo 86400 before adding the offset. This is mathematically identical (
(a + b) mod m == ((a mod m) + b) mod m, negatives normalized with+= 86400), but the intermediate sum is now bounded by86400 + max timezone offset, so it cannot overflow. Applied to bothInt64sites (DateTime64 -> Timeat FunctionsConversion.h:273 andDateTime64 -> Time64); theUInt32sites cannot overflowInt64.Repro (UBSan build):
Regression test:
04512_totime_datetime64_extreme_overflow. No related open issue (found by a private fuzzer; 0 public CIDB hits for this site in 30d).Version info
26.8.1.567(included in26.8and later)