Skip to content

Fix undefined behaviour in DateTimeBestEffort parsing - #100948

Merged
Avogar merged 10 commits into
ClickHouse:masterfrom
bacek:bacek/datatime-ub
Apr 2, 2026
Merged

Avogar merged 10 commits into
ClickHouse:masterfrom
bacek:bacek/datatime-ub

Conversation

@bacek

@bacek bacek commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

parseDateTimeBestEffort accumulated fractional-second digits into an Int64 without capping the count, causing signed integer overflow.

Fix: cap fractional digit count to std::numeric_limits::digits10 at all three call sites. Also consistently use if constexpr (strict) since strict is a template parameter.

Related CI failure: https://github.com/ClickHouse/ClickHouse/actions/runs/23661784718/job/68936469304?pr=100435#step:4:11003

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 undefined behavior (signed integer overflow) in parseDateTimeBestEffort when parsing datetime strings with more than 18 fractional-second digits.

Version info

  • Merged into: 26.4.1.530
  • Backported to: 26.3.4.6, 26.2.8.9, 26.1.8.16, 25.8.22.22

1. Follow same pattern which already existed.
2. Consistently use "if constexpr (strict)"
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Mar 27, 2026
@bacek

bacek commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

@Avogar fyi

@clickhouse-gh

clickhouse-gh Bot commented Mar 27, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [12d6c92]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, flaky check) failure
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
Stateless tests (amd_asan_ubsan, flaky check) failure
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
Stateless tests (amd_tsan, flaky check) failure
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
Stateless tests (amd_msan, flaky check) failure
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
Stateless tests (amd_debug, flaky check) failure
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
03668_shard_join_in_reverse_order FAIL cidb, issue ISSUE EXISTS
Stress test (arm_msan) failure
Server died FAIL cidb IGNORED
MemorySanitizer: use-of-uninitialized-value (STID: 1003-358c) FAIL cidb, issue ISSUE EXISTS

AI Review

Summary

This PR fixes undefined behavior in parseDateTimeBestEffort by capping parsed fractional digits before converting into Int64, and adds regression tests for 9/10-digit Unix timestamps and datetime-string inputs with long fractional tails. I did not find additional high-confidence correctness, safety, or performance issues beyond the already discussed inline bot comments.

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

@alexey-milovidov

Copy link
Copy Markdown
Member

Duplicate of #100368?

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 27, 2026
@bacek

bacek commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

Duplicate of #100368?

Yep. I throw a constexpr all over the place for a good measure, but it's not strictly required.

@bacek bacek closed this Mar 27, 2026
Comment thread src/IO/parseDateTimeBestEffort.cpp Resolved
Test that parsing datetime strings with many fractional digits (more than
`Int64::digits10 = 18`) does not cause signed integer overflow in
`readDecimalNumber`. Covers 10-digit and 9-digit unix timestamps with
fractional parts, and regular datetime strings.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@bacek bacek reopened this Mar 27, 2026
@bacek

bacek commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

Duplicate of #100368?

Actually, I think my Claude wrote better test :)

…comment

`readDigits` reads up to `UInt64::digits10 = 19` digits into its buffer,
so a 19-digit fractional tail is consumed and truncated to 18 (silently
dropped), while a 20-digit tail leaves the 20th digit in the stream and
causes a parse error. Add the 19-digit case to lock in the expected
truncation behavior and update the comment to reflect the actual boundary.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-ci labels Mar 27, 2026
The previous test used fractional values that fit in Int64 even without the
cap, so the bugfix validator (which runs the test against master) correctly
reported "failed to reproduce the bug".

Use 19 nines as the fractional part: without the cap, `readDecimalNumber`
processes chunks 4+4+4+4+3 and the final step `9999999999999999 * 1000`
overflows Int64. With the fix the count is capped to 18 (chunks 4+4+4+4+2)
and no overflow occurs.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@bacek

bacek commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

#100965 is fix for test failure

@bacek

bacek commented Mar 28, 2026

Copy link
Copy Markdown
Contributor Author

Fix for flaky test #100988

@bacek

bacek commented Mar 29, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov I have no idea what to do with that CI failure. Everything else looks green.

@alexey-milovidov

Copy link
Copy Markdown
Member

We recently enabled server-side AST fuzzer in Stress tests, it might be related.
It is extremely powerful, and finds quickly what is usually found once a week..month...
I will take a look.

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

bacek commented Mar 29, 2026

Copy link
Copy Markdown
Contributor Author
● The root cause is clear. The failures are caused by skip_indices column being missing from system.query_log:                                    
                                                                                                                                                  
  Code: 47. DB::Exception: Unknown expression identifier `skip_indices` in scope                                                                  
  SELECT formatQuerySingleLine(query), skip_indices FROM system.query_log ...                                                                     
  (comment: 03632_skip_indices_logged_in_query_log.sql)                                            
                                                                                                                                                  
  Test 03632_skip_indices_logged_in_query_log queries system.query_log.skip_indices, but that column doesn't exist in this PR's binary. This test 
  was likely added to master by another PR after this branch diverged, and that PR also added the column to system.query_log. After this test     
  failure the stress test runner killed the server, producing the cascade of:                                                                     
  - Cannot start clickhouse-server (post-run restart check)                                                                                       
  - Parse failure error                                                                                                                           
  - Check failed     

Sigh. Wasn't that feature removed and that test shouldn't exist? Or it's some stale caches?

@bacek

bacek commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov I have no idea what else I can do to land it.

Merged via the queue into ClickHouse:master with commit 483148b Apr 2, 2026
153 of 159 checks passed
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to release branches 26.3, 26.2, 26.1, 25.12, 25.11, 25.10, 25.9, 25.8, but no backport label was found.

Why: This fixes UB (signed integer overflow) producing wrong results in parseDateTimeBestEffort, which is core DateTime parsing code present in all stable releases. Users providing datetime strings with 19+ fractional digits get silently wrong results.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

@Avogar Avogar added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Apr 2, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 2, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Apr 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Apr 2, 2026
Backport #100948 to 26.3: Fix undefined behaviour in DateTimeBestEffort parsing
@bacek
bacek deleted the bacek/datatime-ub branch April 2, 2026 19:37
Avogar added a commit that referenced this pull request Apr 7, 2026
Backport #100948 to 26.2: Fix undefined behaviour in DateTimeBestEffort parsing
Avogar added a commit that referenced this pull request Apr 7, 2026
Backport #100948 to 26.1: Fix undefined behaviour in DateTimeBestEffort parsing
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Apr 7, 2026
Avogar added a commit that referenced this pull request Apr 10, 2026
Backport #100948 to 25.8: Fix undefined behaviour in DateTimeBestEffort parsing
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 pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

6 participants