Repository navigation
Fix signed integer overflow in toStartOfSubsecondInterval when rounding near INT64_MIN - #111370
Conversation
…ng near INT64_MIN In toStartOfSubsecondInterval, the negative-value branches for equal scales and for a value scale smaller than the interval scale computed ((t + 1) / num_units - 1) * num_units without an overflow check, so for values within one interval of INT64_MIN the multiplication wrapped to a positive garbage value (undefined behavior caught by the UBSan build in DateTimeTransforms.h:623). The scale_multiplier > unit_scale branch already guarded the same multiplication with mulOverflow; now all three branches throw DECIMAL_OVERFLOW consistently. Found by the AST fuzzer in a stress test run: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=97032&sha=815fbc65e060728ae528de8baab6cf23e00ebe90&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29 (on #97032)
|
Workflow [PR], commit [3116798] Summary: ✅
AI ReviewSummaryThis PR closes the remaining signed-overflow cases in Final Verdict
|
…ondinterval-mul-overflow
|
The only completed red job, Integration tests (amd_asan_ubsan, db disk, old analyzer, 3/6), has two failures unrelated to this PR (which only touches
@groeneai, investigate the failure of |
|
Acknowledged. No fix PR is currently open for |
|
Fix in a separate PR: #111506. Root cause: the test injects a 1200ms network delay and expects the S3 INSERT to time out, but set no explicit S3 timeout. On a fresh connection the adaptive connect timeout (500ms/1000ms) trips reliably, but when the request reuses a pooled keep-alive connection there is no fresh connect, so the only remaining gate is the send/receive idleness timeout (default 3000ms/30000ms, far above 1200ms) and the write completes: the rare |
…ondinterval-mul-overflow
…ondinterval-mul-overflow
|
Both red jobs on commit 3116397 are unrelated to this PR (which only touches
Merged master and pushed cc7628b to clear the settings-history red. |
The AI review pointed out that the three `origin.has_value()` branches of `toStartOfSubsecondInterval` subtract the remainder from the (possibly already scaled) time with a raw signed subtraction. The concrete examples in the review are not reachable: the only caller, `FunctionToStartOfInterval`, rejects `origin > time_arg` with `BAD_ARGUMENTS`, and the remainder of a non-negative difference never exceeds that difference, so the result is always at least the origin. Still, the helper should not depend on a precondition enforced by its caller, so use `common::subOverflow` and throw `DECIMAL_OVERFLOW` there as well. Add three-argument regression tests near `INT64_MIN` for all three scale relations, including the case where the interval start lands exactly on `INT64_MIN`, and one where scaling the origin up to the interval scale genuinely overflows and must throw.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 33/36 (91.67%) · Uncovered code |
…ESTORE A non-APPEND refreshable materialized view is only supported in Atomic and Replicated databases. CREATE enforced this in validateMaterializedViewColumnsAndEngine, but the call is gated on mode <= CREATE, so a full-definition ATTACH (mode ATTACH) and a RESTORE that remaps the view into another database (SECONDARY_CREATE with is_restore_from_backup) bypassed the check. In a Memory database the view then got a Nil StorageID UUID, and its refresh (CREATE OR REPLACE of the temp inner table into a Replicated-db target) wrote the Nil into DDLLogEntry.parent_table_uuid, so checkParentTableExists(Nil) -> DatabaseCatalog::tryGetByUUID(Nil) tripped chassert(uuid != Nil && ...) (abort in debug/sanitizer; in release the refresh instead failed permanently with TABLE_IS_DROPPED "Parent table doesn't exist"). Hoist the engine check out of validateMaterializedViewColumnsAndEngine into createTable, gated on is_fresh_definition = mode <= CREATE || (mode == ATTACH && !attach_short_syntax) || is_restore_from_backup, mirroring the existing is_fresh_definition predicate in registerStorageMergeTree.cpp. Stored definitions (short ATTACH, ATTACH DATABASE, restart) and plain Replicated-DDL replay stay exempt; fresh full-definition ATTACH and RESTORE-with-remap are validated like CREATE. Also add a fail-close Nil guard in checkParentTableExists (return false, mirroring the existing guard in getRMVCoordinationInfo) so metadata written by older binaries loads and its refresh fails cleanly instead of aborting. Found by BuzzHouse (arm_asan_ubsan on PR ClickHouse#111370, amd_msan on PR ClickHouse#109454; both unrelated to this code path). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a signed integer overflow in
toStartOfIntervalwith subsecond intervals: forDateTime64values within one interval of the lower bound ofInt64, the rounded result silently wrapped to a positive garbage value; now the function throws aDECIMAL_OVERFLOWexception.In
toStartOfSubsecondInterval, the negative-value branches for equal scales and for a value scale smaller than the interval scale computed((t + 1) / num_units - 1) * num_unitswithout an overflow check, so for values within one interval ofINT64_MINthe multiplication wrapped to a positive value (undefined behavior reported by the UBSan build atDateTimeTransforms.h:623). Thescale_multiplier > unit_scalebranch already guarded the same multiplication withmulOverflow(added in #101032); this applies the same guard to the other two branches, so all three throwDECIMAL_OVERFLOWconsistently. This is the incomplete-fix case described in the linked issue.Found by the AST fuzzer in a stress test run on #97032:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=97032&sha=815fbc65e060728ae528de8baab6cf23e00ebe90&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%29
Closes: #101096
Related: #101032
Related: #97032
Version info
26.8.1.176(included in26.8and later)