Skip to content

Fix possible long type overflow in parseDateTime64BestEffort - #103717

Closed
groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:fix/parsedatetime64-overflow-takeover
Closed

groeneai wants to merge 4 commits into
ClickHouse:masterfrom
groeneai:fix/parsedatetime64-overflow-takeover

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Supersedes #102890 (per @alexey-milovidov's directive on #102890 (comment)).

@Avogar's branch had stale CI failures from infrastructure issues that have since been fixed on master. This PR replays his three commits (preserving him as Author) onto fresh master so the CI re-runs cleanly. No code changes vs. PR #102890 HEAD — including @nihalzp's wording-suggestion commit, which was already applied there.

Why this matters

Closes the chronic UBSAN bug at src/IO/parseDateTimeBestEffort.cpp:79–82 (signed integer overflow in readDecimalNumber on inputs like '12:34:56.123456789012345678.987654321'). The function_prop_fuzzer lane has been hitting this on ~30+ unrelated PRs over the past ~14 days. Once this lands, that whole class of failures clears across affected PRs.

What the fix does

Adds an explicit has_fractional_separator flag tracked independently from digit count. The previous check (fractional->digits == 0) missed the boundary case where the first . had zero digits before it (e.g. 12:34:56..123). The new flag rejects any second . unconditionally with a clearer error message ("Cannot read DateTime: duplicate fractional separator").

Regression test added at tests/queries/0_stateless/04101_parseDateTime64BestEffort_double_dot_fractional.sql covering:

  • 12:34:56..123 (back-to-back separators)
  • 12:34:56.123.456 (separator-digits-separator-digits)
  • 12:34:56.123456789012345678.987654321 (the long-overflow trigger)

All three forms tested for the strict, OrNull, and OrZero variants.

Reviews carried over

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix possible signed integer overflow in parseDateTime64BestEffort and its OrNull/OrZero variants when the input contains duplicate fractional separators (e.g. '12:34:56..123' or '12:34:56.123456789012345678.987654321'). Such inputs are now rejected with a clear error message instead of triggering undefined behavior.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Co-authored-by: avogar [email protected]
Co-authored-by: Pavel Kruglov [email protected]
Co-authored-by: Nihal Z. Miaji [email protected]

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @nihalzp @alexey-milovidov — could you re-review? No code changes vs. #102890 HEAD; this is a clean rebase onto master plus your already-approved wording fix carried in commit 3.

Pre-PR Validation Gate (TASK.md Phase 4 Step 9)

a) Deterministic repro? ✅ — Regression test 04101_parseDateTime64BestEffort_double_dot_fractional.sql deterministically triggers the failure on inputs like '12:34:56..123' and '12:34:56.123456789012345678.987654321'.

b) Root cause explained? ✅ — Pre-fix path: parser sees first . with zero digits → fractional->digits == 0 → fails to set the duplicate-guard. Second . then re-enters the same branch and calls readDecimalNumber again, where on long inputs the fractional accumulator overflows Int64 (signed integer overflow caught by UBSan). Fix: track separator presence with a dedicated has_fractional_separator flag set on the first ., independent of digit count.

c) Fix matches root cause? ✅ — The flag rejects any second . unconditionally (vs. the previous digit-count-based heuristic). Targets the exact mechanism, not a symptom.

d) Test intent preserved? / New tests added? ✅ — Three regression test groups cover all relevant patterns: back-to-back (..123), sandwich (.123.456), and long-overflow trigger (.123456789012345678.987654321). All three exercised against base, OrNull, and OrZero forms.

e) Demonstrated in both directions? ✅ — Originally demonstrated via UBSan findings on PR #102890's CI lanes and the chronic function_prop_fuzzer (asan_ubsan) failures on ~30+ unrelated PRs. Test passes with the fix.

f) Fix is general? ✅ — Single code path (parseDateTimeBestEffortImpl in parseDateTimeBestEffort.cpp) — no symmetric implementations to update. The flag-based check generalizes correctly across all parseDateTime64BestEffort* variants since they share this implementation.

Session: cron:clickhouse-ci-task-worker:20260429-131500

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Apr 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3b72fd4]


AI Review

Summary

This PR combines a real parser safety fix in parseDateTimeBestEffort with broader CI/stress/Azure test-environment changes. The core parser direction is good, but one newly added guard is currently ineffective due to control-flow ordering, so the parser branch should be adjusted before merge.

PR Metadata
  • Changelog category: ⚠️ Partially mismatched. The selected Bug Fix category covers the parser fix, but the PR also contains substantial CI/stress-infra changes.
  • Changelog entry: ⚠️ Incomplete for the actual diff. It documents only the parser behavior change and omits the CI/Azure test-environment modifications included in this PR.
Findings
  • ⚠️ Majors
    • [src/IO/parseDateTimeBestEffort.cpp:570] has_fractional_separator is unreachable in the duplicate-. path because if (has_fractional) returns first. As written, the new "duplicate fractional separator" branch never executes.
    • Suggested fix: check/set has_fractional_separator before the has_fractional early return if you want that message, or remove the extra state/message and keep a single duplicate-fractional-path check.
    • [ci/jobs/stress_job.py:50] This PR includes broad CI/Azure test-environment refactoring that is not represented in the PR purpose/changelog text, which makes the parser fix harder to audit and increases review risk.
    • Suggested fix: split CI/stress changes into a separate PR, or update PR metadata/changelog to explicitly cover both scopes.
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 ⚠️ Changelog text does not describe all shipped changes in this diff.
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    1. Fix the unreachable duplicate-fractional-separator branch in parseDateTimeBestEffortImpl.
    2. Align PR metadata/changelog text with the full scope of files changed (or split unrelated CI changes into a separate PR).

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 29, 2026
@alexey-milovidov alexey-milovidov self-assigned this Apr 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

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

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, link the PR fixing test_distributed_frozen_replica here. If there is no fix yet, submit a fix as a separate PR. Then update the branch.

…-overflow-takeover

# Conflicts:
#	CHANGELOG.md
#	ci/jobs/functional_tests.py
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Apr 30, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov — test_distributed_frozen_replica was fixed by #103780 (Mode B brittle "receive timeout" assertion relaxation), which you merged at 17:45 UTC. The branch is now updated to master HEAD 5d7f0cd8503e and includes that fix (new branch HEAD: 3b72fd4695385aeb366f1d5eef86f2c6b4b2facf). CI will re-run.

Session: cron:clickhouse-ci-task-worker:20260430-184500

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, the diff is too large. Keep only the relevant changes.

return on_error(ErrorCodes::CANNOT_PARSE_DATETIME, "Cannot read DateTime: duplicate fractional part");
has_fractional = true;

if (has_fractional_separator)

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.

has_fractional_separator is currently unreachable here: on a second . we already return from the preceding if (has_fractional) branch, so this new check/message never executes.

If the intent is to surface "duplicate fractional separator", the condition ordering needs to change; otherwise this extra state can be removed to keep the parser logic minimal.

Comment thread ci/jobs/stress_job.py
@@ -62,13 +50,6 @@ def get_additional_envs(info, check_name: str) -> List[str]:
from ci.jobs.ci_utils import is_extended_run

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.

This change is part of a broader CI/Azure test-environment refactor and is unrelated to the stated parseDateTime64BestEffort overflow fix/changelog entry.

Please split these CI/stress changes into a separate PR (or update this PR metadata/changelog to match the real scope), otherwise review/auditability for the parser fix stays ambiguous.

@groeneai

groeneai commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov — slimmed resubmit opened: #104163.

While replicating the commits onto a fresh branch, I noticed @yariks5s already landed an equivalent code fix for the duplicate-fractional-separator bug in #103773 (merged 2026-04-30). Both PRs add a boolean flag (has_fractional vs has_fractional_separator) at the same line in parseDateTimeBestEffortImpl's point-symbol branch — functionally identical.

So the only "relevant changes" left from #102890 / this PR are @Avogar's expanded test cases (3 inputs × strict/OrNull/OrZero, vs the single test on master). #104163 carries those forward and nothing else — just tests/queries/0_stateless/04101_parseDateTime64BestEffort_double_dot_fractional.{sql,reference} (19 insertions).

Verified locally on master HEAD bc39c42c123e: all three test inputs error correctly under CANNOT_PARSE_DATETIME (strict) and return \N / 1970-01-01 00:00:00.000 (OrNull / OrZero). Reference matches actual output.

Co-authors carried forward: @Avogar (test author), @nihalzp (original wording reviewer).

pull Bot pushed a commit to TCeason/ClickHouse that referenced this pull request May 6, 2026
Adds expanded coverage (3 inputs * 3 OrNull/OrZero variants) for the
duplicate fractional-separator case in parseDateTime64BestEffort.

The underlying bug is already fixed on master by PR ClickHouse#103773 (yariks5s,
"Fix UB while parsing dateTime", merged 2026-04-30). This test PR
takes over @Avogar's stalled ClickHouse#102890 / closed ClickHouse#103717 by carrying
forward only the additional test cases — the .cpp fix from ClickHouse#102890
is now redundant.

Test cases:
  '12:34:56..123'                                  (double dot directly)
  '12:34:56.123.456'                               (extra dot after fractional)
  '12:34:56.123456789012345678.987654321'          (long fractional + dot)

Each tested against parseDateTime64BestEffort (strict),
parseDateTime64BestEffortOrNull, and parseDateTime64BestEffortOrZero.

Co-authored-by: avogar <[email protected]>
Co-authored-by: Pavel Kruglov <[email protected]>
Co-authored-by: Nihal Z. Miaji <[email protected]>
pull Bot pushed a commit to TCeason/ClickHouse that referenced this pull request May 6, 2026
…4-double-dot-test-takeover-v2

Add tests for parseDateTime64BestEffort duplicate fractional separator (takeover of ClickHouse#102890 / supersede ClickHouse#103717)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors manual approve Manual approve required to run CI pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants