Fix UBSan error in parseReadableSize for values near UInt64 max by alexey-milovidov · Pull Request #101097 · ClickHouse/ClickHouse · GitHub
Skip to content

Fix UBSan error in parseReadableSize for values near UInt64 max#101097

Merged
nickitat merged 9 commits into
masterfrom
fix-parseReadableSize-ubsan
Apr 16, 2026
Merged

Fix UBSan error in parseReadableSize for values near UInt64 max#101097
nickitat merged 9 commits into
masterfrom
fix-parseReadableSize-ubsan

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 29, 2026

Copy link
Copy Markdown
Member

The FunctionsStress unit test triggers a UBSan error in parseReadableSize:

/ClickHouse/src/Functions/parseReadableSize.cpp:219:36: runtime error: 1.84467e+19 is outside the range of representable values of type 'unsigned long'

The overflow check compared num_bytes_with_decimals against UInt64 max, but std::ceil was applied after the check. Due to floating-point precision, a value could pass the check but then std::ceil would round it up beyond UInt64 max, causing undefined behavior in the cast.

Fix: apply std::ceil before the range check.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101042&sha=6bce0d39d7fb522ace5a37cb78c047754d7f0a09&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%29
#101042

Changelog category (leave one):

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

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.999

Move `std::ceil` before the overflow range check so that values rounded
up beyond `std::numeric_limits<UInt64>::max()` are caught and throw an
exception instead of causing undefined behavior in the cast.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101042&sha=6bce0d39d7fb522ace5a37cb78c047754d7f0a09&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%29

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@clickhouse-gh

clickhouse-gh Bot commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Mar 29, 2026
alexey-milovidov and others added 4 commits March 29, 2026 21:16
Also fix the overflow check to use a direct comparison against 2^64 as
a Float64 literal, using >= instead of >. The previous > comparison
against `std::numeric_limits<UInt64>::max()` was ineffective because
UInt64 max (2^64-1) rounds up to 2^64 when implicitly converted to
Float64, making `2^64 > 2^64` always false.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
18.4 EB = 18,400,000,000,000,000,000 which is less than UInt64 max
(18,446,744,073,709,551,615), so `parseReadableSize('18.4 EB')` should
succeed. Changed to test 18.5 EB for the overflow case instead.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@nickitat nickitat self-assigned this Mar 30, 2026
@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.

Comment thread src/Functions/parseReadableSize.cpp
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The failures of "Flaky check" in "functions_bad_arguments" will be fixed by #101994.

@alexbakharew

Copy link
Copy Markdown
Contributor

Hi, can we merge it once the PR is green?

@clickhouse-gh

clickhouse-gh Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.00% +0.00%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

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

Full report · Diff report

@nickitat nickitat added this pull request to the merge queue Apr 16, 2026
Merged via the queue into master with commit e24c790 Apr 16, 2026
317 of 318 checks passed
@nickitat nickitat deleted the fix-parseReadableSize-ubsan branch April 16, 2026 16:47
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 16, 2026
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.

4 participants