Repository navigation
Fix UBSan signed integer overflow in toStartOfSecond/Millisecond/Microsecond near INT64_MIN - #105482
Conversation
…osecond near INT64_MIN `toStartOfSecond`, `toStartOfMillisecond`, and `toStartOfMicrosecond` for `DateTime64` and `Time64` did `datetime64 - fractional_with_sign` in signed `Int64`, which triggers UBSan signed-integer-overflow when the input is near `INT64_MIN`. Use unsigned arithmetic for the final subtraction, matching the multiplication path already fixed in 4154381. Surfaced by the `FunctionsStress` property fuzzer at `src/Functions/DateTimeTransforms.h:954` (1 master hit + 3 unrelated PRs in the last 90 days). CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102854&sha=d1d3a9ac7bc28763a2f46268f6fa0918ac096573&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29
Pre-PR validation gateSession: cron:clickhouse-ci-task-worker:20260520-233000 a) Deterministic repro? Yes. Locally, against fires b) Root cause explained? c) Fix matches root cause? Yes. Replace the signed subtraction with d) Test intent preserved? / New tests added? Added e) Demonstrated in both directions? Without the fix: UBSan fires immediately on the boundary query above. With the fix: 20/20 f) Fix is general? Yes. Six call sites have the same |
|
cc @alexey-milovidov @rschu1ze — could you review? Same fix style as 4154381 (the existing unsigned-arithmetic fix for the multiplication path in the same three functions); this PR extends it to the subtraction path that still triggered UBSan near |
|
Workflow [PR], commit [bd1e245] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
…crosecond The negative-value normalization branch added back a full scale_multiplier instead of the unit that was dropped. getFractionalPartWithScaleMultiplier was called with scale_multiplier / 1000 (millisecond) or / 1000000 (microsecond), so the remainder must be normalized by that same divisor, not by the whole scale_multiplier. Adding the full multiplier shifted negative pre-epoch results by nearly a full second, e.g. toStartOfMillisecond(toDateTime64(-123.456789, 6)) returned -124.456000 instead of -123.457000. toStartOfSecond was already correct because its divisor is the full scale_multiplier. The Time64 overloads are shared template code that is not reachable from SQL (the registered functions use DataTypeDateTime64 and checkArguments rejects Time64), but they are corrected here for consistency. Extends regression test 04270_toStartOfSubsecond_ubsan_int64_min with negative pre-epoch cases. The INT64_MIN millisecond/microsecond reference values also change because those inputs hit the same corrected branch. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
CI Ledger — covered SHA: f4f65c2CI fully finished on this HEAD: Finish Workflow SUCCESS @ 2026-06-03T16:40:11Z, Mergeable Check SUCCESS, PR aggregator SUCCESS. Zero CIDB FAIL/ERROR rows on this commit. Every Build (amd/arm matrix), Stateless tests, Integration tests, Unit tests (asan_ubsan + tsan + msan + function_prop_fuzzer), AST fuzzer, BuzzHouse, Stress, Performance Comparison, SQLLogic, Compatibility, Install, Docker, Code Review, Fast test, Style — all pass. Awaiting human re-review from the cc'd reviewers (@ alexey-milovidov, @ rschu1ze) on the unsigned-arithmetic fix for Session: cron:our-pr-ci-monitor:20260605-033000 |
…rflow Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…tof-subsecond-ubsan-overflow
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 11/22 (50.00%) | Lost baseline coverage: none · Uncovered code |
|
Hi — this looks like a backport candidate for |
…Time64 The AST fuzzer hit a UBSan signed-integer-overflow on master when an extreme DateTime64 near INT64_MIN flows into the toStartOf*Interval family (STID 1421-42cf, Stress test (amd_asan_ubsan)). toStartOfHourInterval (reached by toStartOfInterval with an N-hour unit, N > 1) computes the final result as `values.date + time` in signed Int64. findIndex clamps only the LUT index, not t itself, so for t near INT64_MIN the raw `time = t - values.date` is hugely negative and `values.date + time` underflows Int64 -- undefined behavior: runtime error: signed integer overflow: -2208988800 + -9223372034645788816 cannot be represented in type 'long' The sum is now computed in UInt64 (defined wraparound) and cast back to the signed type. For every in-range input this is byte-identical to the previous result; only inputs within a divisor of INT64_MIN -- which have no valid datetime meaning -- now wrap deterministically instead of invoking UB. The minute-interval and roundDown fast paths that exhibited the same bug class were fixed independently on master by the roundDownToMultiple helper added in PR ClickHouse#107985; this change covers the remaining hour-interval site. It is the toStartOf*Interval analogue of the subsecond fix in PR ClickHouse#105482 (toStartOfSecond/Millisecond/Microsecond in DateTimeTransforms.h). The regression test exercises the whole toStartOf*Interval family at the INT64_MIN boundary so the entire family stays UBSan-clean. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Two fixes in
toStartOfSecond/toStartOfMillisecond/toStartOfMicrosecondforDateTime64.UBSan signed integer overflow near
INT64_MIN. Each function diddatetime64 - fractional_with_signin signedInt64, which is UB at the boundary. The final subtraction now uses unsigned arithmetic, mirroring the multiplication path fixed in 4154381.Wrong result for negative pre-epoch values in
toStartOfMillisecond/toStartOfMicrosecond. The negative-branch normalization added back a fullscale_multiplierinstead of the dropped unit (scale_multiplier / 1000for milliseconds,/ 1000000for microseconds), producing a value off by nearly a full second. For exampletoStartOfMillisecond(toDateTime64(-123.456789, 6))returned-124.456000instead of-123.457000.toStartOfSecondwas already correct because its divisor is the fullscale_multiplier.The
Time64overloads inDateTimeTransforms.hare shared template code but are not reachable from SQL: the registered functions useDataTypeDateTime64andcheckArgumentsrejectsTime64viaisDateOrDate32OrDateTimeOrDateTime64. They are corrected for internal consistency only, with no user-visible effect.Surfaced by the
FunctionsStressproperty fuzzer inUnit tests (asan_ubsan, function_prop_fuzzer)atsrc/Functions/DateTimeTransforms.h(1 master hit + 3 unrelated PRs in 90 days). Reproduced and verified against an ASan+UBSan build: UBSan no longer fires and negative pre-epoch truncation is correct. Regression test:04270_toStartOfSubsecond_ubsan_int64_min.CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102854&sha=d1d3a9ac7bc28763a2f46268f6fa0918ac096573&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
toStartOfMillisecondandtoStartOfMicrosecondreturning a result off by nearly a second for negative (pre-epoch)DateTime64values, and fixUndefinedBehaviorSanitizersigned-integer-overflow intoStartOfSecond,toStartOfMillisecond, andtoStartOfMicrosecondforDateTime64inputs nearINT64_MIN.Documentation entry for user-facing changes
Version info
26.6.1.1032