Skip to content

Fix signed integer overflow in parseDateTimeBestEffort - #100368

Merged
alexey-milovidov merged 11 commits into
masterfrom
fix-parse-datetime-overflow
Apr 10, 2026
Merged

alexey-milovidov merged 11 commits into
masterfrom
fix-parse-datetime-overflow

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

When parsing unix timestamps with fractional parts (e.g. 1234567890.12345678901234567), the fractional digit count was not clamped to std::numeric_limits<Int64>::digits10, causing signed integer overflow in readDecimalNumber. This was already fixed for one call site (the general fractional parsing path at line 532) but two other call sites (unix timestamps with 9 and 10 digits) were missing the same protection.

The overflow was caught by UBSan in the FunctionsStress.stress unit test:

src/IO/parseDateTimeBestEffort.cpp:82:22: runtime error: signed integer overflow: 
36435637994349340 * 10000 cannot be represented in type 'long'

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=d370916dc6c37021dcc9c7dd6f327cfc8b1198fd&name_0=MasterCI&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29

Changelog category (leave one):

  • CI Fix or Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix signed integer overflow when parsing fractional seconds in parseDateTimeBestEffort with very long fractional parts.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.759

…art parsing

When parsing unix timestamps with fractional parts (e.g. `1234567890.12345678901234567`),
the fractional digit count was not clamped to `std::numeric_limits<Int64>::digits10`,
causing signed integer overflow in `readDecimalNumber`. This was already fixed for one
call site (the general fractional parsing path) but two other call sites (unix timestamp
with 9 and 10 digits) were missing the same protection.

Apply the same clamping pattern to the remaining call sites.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=d370916dc6c37021dcc9c7dd6f327cfc8b1198fd&name_0=MasterCI&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29

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

clickhouse-gh Bot commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9212d31]

Summary: ❌

job_name test_name status info comment
Bugfix validation (functional tests) failure
04054_parse_datetime_best_effort_overflow FAIL cidb

AI Review

Summary

This PR adds a focused stateless regression test for the signed-overflow scenario in parseDateTimeBestEffort fractional parsing (via parseDateTime64BestEffort) using both 9-digit and 10-digit Unix timestamp forms and high-precision fractional parts. The test is well-scoped and now uses overflow-triggering 19-digit fractions that exceed Int64, so it properly validates the bugfix path.

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: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 22, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Missing test.

alexey-milovidov and others added 2 commits March 24, 2026 00:23
Test that `parseDateTime64BestEffort` handles fractional parts with 18-19
digits without signed integer overflow in `readDecimalNumber`. These inputs
previously triggered UBSan errors because the fractional digit count was
not clamped to `std::numeric_limits<Int64>::digits10`.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
-- https://github.com/ClickHouse/ClickHouse/pull/100368

-- 10-digit unix timestamp with 19-digit fractional part (exceeds Int64::digits10 = 18)
SELECT parseDateTime64BestEffort('1234567890.1234567890123456789');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ The new regression test currently does not reproduce the overflow on the pre-fix binary, which is why Bugfix validation reports Failed to reproduce the bug.

The 19-digit fractional literals here start with 1 (1234567890123456789), and that value still fits in Int64, so old code can parse it without triggering signed overflow. Please use a 19-digit fractional value that exceeds Int64 (for example, 9999999999999999999) so the old path reliably hits the overflow and the bugfix validation test actually validates the fix.

alexey-milovidov and others added 2 commits March 24, 2026 15:19
The test output depends on timezone, but `session_timezone` is
randomized in CI. Pin it to UTC so the reference file is stable.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…mezone

Use `9999999999999999999` (19 nines) as the fractional part instead of
`1234567890123456789`, which fits in `Int64` and does not trigger the
overflow. Also pass the timezone explicitly as the third argument to
`parseDateTime64BestEffort` instead of using `SET session_timezone` to
avoid environment-dependent failures.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov and others added 5 commits March 28, 2026 20:24
… values

The test reference was generated on a CET machine, producing timestamps
1 hour ahead of UTC. The Fast test CI runner uses UTC, causing a mismatch.
Fix by setting `session_timezone = 'UTC'` and updating the reference to
the correct UTC values.

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

clickhouse-gh Bot commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.70% 76.70% +0.00%

Changed lines: 100.00% (20/20) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

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

Copy link
Copy Markdown
Member Author

The bugfix validation runs under debug, not ubsan. However, this is not a bug fix. I will change the category.

@alexey-milovidov alexey-milovidov self-assigned this Apr 10, 2026
@alexey-milovidov
alexey-milovidov merged commit 3ca0aa3 into master Apr 10, 2026
158 of 160 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-parse-datetime-overflow branch April 10, 2026 00:29
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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