Skip to content

Fix undefined behavior on an out-of-range quota limit - #113178

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-quota-limit-float-overflow-ub
Aug 4, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-quota-limit-float-overflow-ub

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Documentation entry for user-facing changes

...

A quota limit of a type whose output_denominator is not 1 (execution_time, which is kept in nanoseconds) is scaled with floating point arithmetic and then converted to QuotaValue (UInt64) with a plain static_cast. Converting a Float64 that does not fit into UInt64 is undefined behavior:

src/Parsers/Access/ParserCreateQuotaQuery.cpp:169:49: runtime error: 1.84467e+19 is outside the range of representable values of type 'unsigned long'

CREATE QUOTA q FOR INTERVAL 1 day MAX execution_time = 1e19 is enough to trigger it. The AST fuzzer finds it regularly - it has failed AST fuzzer (arm_asan_ubsan) on five unrelated pull requests over the last two weeks, most recently on https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=80263&sha=2717d0f07a49ac6dd54318fe870ed0a3d9a2a7d1&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29

The scaling is moved into a new QuotaTypeInfo::scaleToValue, which rejects a non-finite or out-of-range result with BAD_ARGUMENTS before the cast. It is used both by the parser and by QuotaTypeInfo::stringToValue, which had the same unchecked cast. The range check uses accurate::greaterOp rather than a naive comparison, because Float64(std::numeric_limits<UInt64>::max()) rounds up - the same reasoning as the one already documented in FieldVisitorConvertToNumber.

Related: #80263
Related: #103817

A quota limit of a type whose `output_denominator` is not 1 (`execution_time`, kept in nanoseconds) is scaled
with floating point arithmetic and then converted to `QuotaValue` (`UInt64`) with a plain `static_cast`.
Converting a `Float64` that does not fit into `UInt64` is undefined behavior, which UBSan reports as:

```
src/Parsers/Access/ParserCreateQuotaQuery.cpp:169:49: runtime error: 1.84467e+19 is outside the range of representable values of type 'unsigned long'
```

The AST fuzzer hits it regularly, e.g. on
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=80263&sha=2717d0f07a49ac6dd54318fe870ed0a3d9a2a7d1&name_0=PR&name_1=AST%20fuzzer%20%28arm_asan_ubsan%29
(#80263), and on five unrelated pull requests over the last two
weeks.

The scaling is moved into a new `QuotaTypeInfo::scaleToValue`, which rejects a non-finite or out-of-range
result with `BAD_ARGUMENTS` before the cast. It is used both by the parser and by
`QuotaTypeInfo::stringToValue`, which had the same unchecked cast. The range check uses `accurate::greaterOp`
rather than a naive comparison, because `Float64(std::numeric_limits<UInt64>::max())` rounds up - the same
reasoning as in `FieldVisitorConvertToNumber`.
@clickhouse-gh

clickhouse-gh Bot commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2410297]

Summary: ✅


AI Review

Summary

This PR moves quota-limit scaling into QuotaTypeInfo::scaleToValue, reuses it from both SQL parsing and config parsing, and makes the new stateless regression test parallel-safe by giving each run a unique quota name. I did not find a remaining correctness issue in the implementation or test, but the PR metadata does not match the actual change.

PR Metadata
  • Changelog category is not correct. CI Fix or Improvement is for CI-only changes, while this PR changes server behavior by rejecting out-of-range quota limits before the UInt64 cast.
  • Exact replacement:
### Changelog category (leave one):
- Not for changelog (changelog entry is not required)
Final Verdict

✅ No inline code findings. ⚠️ Update the PR metadata before merge.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Aug 3, 2026
alexey-milovidov and others added 2 commits August 3, 2026 19:25
The flaky check runs a new test many times in parallel with itself, and
quotas are server-global, so the fixed name `quota_04695` collided across
concurrent runs (`ACCESS_ENTITY_ALREADY_EXISTS`, `UNKNOWN_QUOTA`, and an
empty result from `system.quota_limits` after a concurrent `DROP QUOTA`).
Convert the test to a shell test with the quota name suffixed by the unique
test database name, following `04321_multiple_quotas_per_user.sh`.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=113178&sha=7a44665199d7d4a6559d2f5809a10bc948cb7a68&name_0=PR&name_1=Stateless%20tests%20%28amd_asan_ubsan%2C%20flaky%20check%29
#113178

Co-Authored-By: Claude Fable 5 <[email protected]>
@Manerone Manerone self-assigned this Aug 4, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 4, 2026
Merged via the queue into master with commit 862963a Aug 4, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-quota-limit-float-overflow-ub branch August 4, 2026 15:09
alexey-milovidov added a commit that referenced this pull request Aug 4, 2026
…-ubsan-overflow

Master merged a narrower fix for the same undefined behavior (`QuotaTypeInfo::scaleToValue`,
from PR #113178). This branch computes the scaled
value exactly from the literal text and range-checks every path, superseding that guard, so the
conflict is resolved by keeping this branch's `parseMaxValue` and `stringToValue` and removing
the now-unused `scaleToValue`. The new test `04695_quota_limit_out_of_range` from master is
kept and passes under this branch's behavior.
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 4, 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.

3 participants