Repository navigation
Fix UBSan in FieldVisitorConvertToNumber for wide-integer Float64 boundary - #104154
Conversation
…ndary
For wide integer types (`UInt64`, `Int64`, `Int128`, `UInt128`, `Int256`,
`UInt256`), `numeric_limits<T>::max()` has more than 53 significant bits, so
converting it to `Float64` rounds UP. Example for `UInt64`:
numeric_limits<UInt64>::max() = 2^64 - 1 = 18446744073709551615
Float64(numeric_limits<UInt64>::max()) = 2^64 = 18446744073709551616
The boundary check `x > Float64(numeric_limits<T>::max())` therefore failed
to reject `Float64` values equal to that rounded-up boundary, leading to
undefined behavior in the subsequent `static_cast<T>(x)`:
src/Common/FieldVisitorConvertToNumber.h:85:22: runtime error:
1.84467e+19 is outside the range of representable values of type 'unsigned long'
Replace the buggy boundary check with `accurate::greaterOp` /
`accurate::lessOp` (in `Common/NaNUtils.h::canConvertTo`, with `DecomposedFloat`
directly to avoid a circular include), which use precision-correct
float-vs-integer comparison via `DecomposedFloat::compare`. This is the
canonical helper already used by `convertNumeric` and `FieldVisitorAccurateLess`.
The same pattern existed in three places, all fixed:
* `src/Common/FieldVisitorConvertToNumber.h` — aggregate-function parameter
parsing (`topK`, `uniqUpTo`, `groupArrayInsertAt`, etc.)
* `src/Core/SettingsFields.cpp::fieldToNumber` — `SET <integer-setting> = <Float64>`
* `src/Common/NaNUtils.h::canConvertTo` — used in `InterpreterSelectQuery`,
`UnionStep`, `PlannerJoinTree`
`bool` is special-cased: `numeric_limits<bool>` is exactly representable in
`Float64`, and `accurate::lessOp` would fail to instantiate for `bool` because
`make_unsigned_t<bool>` is ill-formed since C++17.
Closes ClickHouse#103817.
CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=103580&sha=baca42bf3124df58c66bec7dba0089cd200c24c1&name_0=PR&name_1=Unit%20tests%20%28asan_ubsan%2C%20function_prop_fuzzer%29
|
Pre-PR validation gate (per a) Deterministic repro? ✅ b) Root cause explained? ✅ For wide integer types ( c) Fix matches root cause? ✅ Replace the boundary check with d) Test intent preserved? ✅ Existing tests ( e) Both directions demonstrated? ✅
f) Fix is general? ✅ Three sites with the same buggy pattern, all fixed: Session: cron:clickhouse-ci-task-worker:20260505-214500 |
|
cc @alexey-milovidov @Algunenano — could you review this? Per directive on #103817. The fix replaces a Float64-vs-integer boundary check that silently overflowed for wide-integer types ( @Algunenano — flagging because of overlap with your open PR #102646 (NumberLiteral Field type) which touches the same files. Happy to rebase on top of #102646 if you'd prefer that ordering. |
|
Workflow [PR], commit [eae0a62] Summary: ✅ AI ReviewSummaryThis PR fixes a real UB path when converting ClickHouse Rules
Final Verdict
|
LLVM Coverage Report
Changed lines: 73.68% (14/19) · Uncovered code |
|
Hi @groeneai @alexey-milovidov — while reviewing this PR I found the following:
Could you review the test PR and add the |
Fix undefined behavior in
FieldVisitorConvertToNumber<T>::operator()(Float64)whenTis a wide integer type (UInt64,Int64,Int128,UInt128,Int256,UInt256).Bug
For these wide integer types
numeric_limits<T>::max()has more than 53 significant bits, so converting it toFloat64rounds up. Example forUInt64:The existing boundary check
x > Float64(numeric_limits<T>::max())therefore fails to rejectFloat64values equal to the rounded-up boundary. The subsequentstatic_cast<T>(x)is undefined behavior.UBSan output from issue #103817 (CI:
Unit tests (asan_ubsan, function_prop_fuzzer)on PR #103580):1.84467e+19is exactly2^64. On a release build the same code silently truncates the value (typically to0), which is also wrong —topK(1.8446744073709552e19)(...)would proceed withthreshold = 0instead of rejecting the input.Fix
Replace the buggy boundary check with
accurate::greaterOp/accurate::lessOp(fromCore/AccurateComparison.h), which usesDecomposedFloat::compareto perform precision-correct float-vs-integer comparison. This is the existing canonical helper for this kind of comparison and is already used elsewhere inconvertNumeric,FieldVisitorAccurateLess, etc.The same buggy pattern existed in two other places — fixed all three:
src/Common/FieldVisitorConvertToNumber.h— the reported site (used in aggregate function parameter parsing:topK,histogram,uniqUpTo,groupArrayInsertAt, etc.)src/Core/SettingsFields.cpp::fieldToNumber<T>— used bySET <integer_setting> = <Float64>src/Common/NaNUtils.h::canConvertTo<T>— used by callers inInterpreterSelectQuery.cpp,UnionStep.cpp,PlannerJoinTree.cppboolis special-cased —numeric_limits<bool>is exactly representable inFloat64, so the simple comparison is correct, andaccurate::lessOpwould fail to instantiate forbool(make_unsigned_t<bool>is ill-formed since C++17).Test
tests/queries/0_stateless/04205_field_visitor_convert_to_number_ubsan_103817.sqlexercises both call paths (aggregate function parameters and integer settings) at the boundary value1.8446744073709552e19(=2^64). The test fails on master without this fix (queries return wrong results in release builds, abort under UBSan) and passes with this fix (queries throwCANNOT_CONVERT_TYPE).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix undefined behavior when an out-of-range
Float64value is converted to a wide integer type (UInt64,Int64,Int128,UInt128,Int256,UInt256). Previously, values equal toFloat64(numeric_limits<T>::max())(which rounds up to a value above the actual maximum) bypassed the bounds check and produced UB in the subsequent cast. This affected aggregate-function parameter parsing (topK,histogram,uniqUpTo,groupArrayInsertAt, etc.) and integer-typed settings viaSET <setting> = <Float64>. Closes #103817.Documentation entry for user-facing changes
Version info
26.5.1.476