Skip to content

Fix signed integer overflow in toStartOfInterval for Millisecond and Microsecond - #101032

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix/toStartOfInterval-millisecond-overflow
Mar 29, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix/toStartOfInterval-millisecond-overflow

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 28, 2026 •

Copy link
Copy Markdown
Member

The scale_multiplier > target_scale branches in ToStartOfInterval<Millisecond> and ToStartOfInterval<Microsecond> had no overflow protection for arithmetic on the internal DateTime64 value t. When t is near INT64_MAX, expressions like t + scale_diff / 2 and milliseconds * scale_diff can overflow Int64.

Add addOverflow and mulOverflow checks matching the pattern already used in the scale_multiplier < target_scale branches.

Found by AST fuzzer in https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100378&sha=151861385cd19310e841942ec17ca333b0fe571c&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29
#100378

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

  • Merged into: 26.4.1.374

…d Microsecond

The `scale_multiplier > target_scale` branches in `ToStartOfInterval<Millisecond>`
and `ToStartOfInterval<Microsecond>` had no overflow protection for arithmetic
on the internal DateTime64 value `t`. When `t` is near `INT64_MAX`, expressions
like `t + scale_diff / 2` and `milliseconds * scale_diff` can overflow Int64.

Add `addOverflow` and `mulOverflow` checks matching the pattern already used in
the `scale_multiplier < target_scale` branches.

Found by AST fuzzer in https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100378&sha=151861385cd19310e841942ec17ca333b0fe571c&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1eddd40]

Summary: ✅


AI Review

Summary

This PR adds signed-overflow checks in toStartOfInterval for Millisecond and Microsecond when DateTime64 scale conversion rounds/divides at higher scales. The fix is correct and low-risk, but coverage is still incomplete for the negative-value path (t < 0) that now contains mirrored overflow-sensitive arithmetic.

Findings

⚠️ Majors

  • [tests/queries/0_stateless/04065_toStartOfInterval_overflow.sql:5-11] Tests cover only near-INT64_MAX inputs. The PR also changes overflow-related arithmetic in the t < 0 path, but there is no near-INT64_MIN case to validate it.
    Suggested fix: add at least one DateTime64 near-INT64_MIN case for both toIntervalMillisecond and toIntervalMicrosecond to exercise negative-branch behavior.
Tests
  • ⚠️ Add negative boundary tests using reinterpret(toInt64(-9223372036854775807), 'DateTime64(6|9)') with toIntervalMillisecond(1) / toIntervalMicrosecond(1) and expected DECIMAL_OVERFLOW (or explicit non-overflow expected value if that branch should succeed).
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Add negative-boundary tests for the t < 0 path in toStartOfInterval for Millisecond/Microsecond.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 28, 2026
alexey-milovidov and others added 2 commits March 28, 2026 23:46
`CAST(large_int AS DateTime64(6))` clamps the value to the valid
DateTime64 range (1900-2299), so the internal value never reaches
near INT64_MAX and the overflow check is never exercised. Use
`reinterpret` to inject raw internal values directly.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.20% 84.10% -0.10%
Functions 24.60% 24.50% -0.10%
Branches 76.70% 76.70% +0.00%

Changed lines: 90.91% (40/44) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov self-assigned this Mar 29, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants