Repository navigation
Reject out-of-range Decimal scale when reading a Field - #131154
Conversation
A `Decimal` `Field` read from serialized data carries its own scale, and three readers accepted it without a range check: the binary `Field` reader (`getBinaryValue`), the text dump reader (`readQuoted` behind `Field::restoreFromDump`, where an exponent-form value like `Decimal64_'1e-1000'` builds a scale of 1000), and `decodeDecimal` in `FieldBinaryEncoding`. An out-of-range scale later overruns a fixed-size buffer in `writeDecimalFractional`. Reject `scale > DecimalUtils::max_precision<T>` with `ARGUMENT_OUT_OF_BOUND` in all three readers, as `DataTypeDecimalBase` does, and converge the four binary `Decimal` cases into `readDecimalFieldBinary`. The serialized layout is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [f39e7f6] Summary: ❌
AI ReviewSummaryThis PR hardens three Findings
Final Verdict
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units8 translation units recompiled, 10 s compile time in total, 8 of them have a recent master baseline. |
|
@tiandiwonder @leshikus Why did you ignore the AI review? It's clearly pointing to an issue that cannot be ignored: #131286 |
…d-scale-bound Reject out-of-range `Decimal` scale when reading a `Field`
|
@Algunenano the comment from the author says the issue will be fixed in a follow-up PR, - this is IMO a valid point; was there any other review? |
|
What's the point of leaving a crash pending? What's the hurry? |
|
@Algunenano the AI review comment says nothing about the crash, it's about signed-overflow UB, which is not critical because it does not allow bypassing the load-bearing The root cause of #131286 is the missing scale range check in another place that has nothing to do with signed-overflow UB, and the AI review comment does not help to find this crash, it's just noise. |
Stable 25.8 Backport of ClickHouse#131154 - Reject out-of-range `Decimal` scale when reading a `Field`
Antalya 25.8 Backport of ClickHouse#131154 - Reject out-of-range `Decimal` scale when reading a `Field`
`readDigits` added the parsed exponent to the count of digits after the point
without a check, and `readDecimalText` then computed `digits + exponent`,
`scale + exponent` and `-exponent` in `int32_t`. For an exponent close to the
`Int32` limits these overflow (undefined behaviour); in practice they wrap, so
`toDecimal64OrNull('1e2147483647', 2)` passed the "too big" check and returned
`0` instead of `NULL`, and `toDecimal64('1e2147483647', 2)` returned `0` instead
of throwing `ARGUMENT_OUT_OF_BOUND`.
The `Field` dump reader `readQuoted` had the same problem after the scale bound
was added in ClickHouse#131154: it negated an exponent of `INT32_MIN`, and for a positive
exponent beyond the precision `scaleMultiplier` saturates instead of
overflowing, so `Decimal64_'1e100'` became the maximum `Int64`.
Now `readDigits` fails with `CANNOT_PARSE_NUMBER` when the exponent does not fit
`int32_t`, `readDecimalText` does its bound arithmetic in 64 bits, and
`readQuoted` checks the exponent against `max_precision` before negating or
scaling (`ARGUMENT_OUT_OF_BOUND` / `DECIMAL_OVERFLOW`).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Antalya 26.6 Backport of ClickHouse#131154 - Reject out-of-range `Decimal` scale when reading a `Field`
Stable 25.3 Backport of ClickHouse#131154 - Reject out-of-range `Decimal` scale when reading a `Field`
FIPS 25.3 Backport of ClickHouse#131154 - Reject out-of-range `Decimal` scale when reading a `Field`
Stable 24.8 Backport of ClickHouse#131154 - Reject out-of-range `Decimal` scale when reading a `Field`
|
Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2252 Labels added:
These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels. |
Problem
A
DecimalFieldread from serialized data carries its ownscale, and three readers accepted it without a range check:Fieldreader (getBinaryValue/readFieldBinaryinsrc/Core/Field.cpp), used e.g. by theBINARYsettings format;Fielddump reader (readQuotedbehindField::restoreFromDump), where an exponent-form value such asDecimal64_'1e-1000'builds a scale of 1000 — reachable e.g. through aCUSTOM-flagged setting parsed bySettingFieldCustom::parseFromString;decodeDecimalinsrc/Common/FieldBinaryEncoding.cpp(binary-encodedFields insideDynamicvalues andAggregateFunctiontype parameters).An out-of-range scale is later passed to
writeDecimalFractional(src/IO/WriteHelpers.h), which sizes and fills a fixed-size stack buffer from it and writes out of bounds, so the server can crash when such a value is formatted.Solution
Reject
scale > DecimalUtils::max_precision<T>withARGUMENT_OUT_OF_BOUNDin all three readers, the same boundDataTypeDecimalBaseenforces. The fourDecimalcases ofgetBinaryValueare converged into onereadDecimalFieldBinary<T>helper. Rejecting at the read side means the decimal text writer is never reached with a bad scale. The serialized layout is unchanged.Unit tests build the malformed bytes directly (the writers cannot produce an out-of-range scale):
Field.ReadBinaryDecimalOutOfRangeScaleThrows,Field.RestoreFromDumpDecimalOutOfRangeScaleThrows,FieldBinaryEncoding.DecodeDecimalOutOfRangeScaleThrowsandSettings.CustomFlaggedDecimalWithOutOfRangeScaleRejected, each next to a valid round-trip case.Field.ReadBinaryDeeplyNestedThrowsNotOverflowadditionally covers the existingcheckStackSizeingetBinaryValue.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a possible server crash when reading a
Decimalvalue with an out-of-range scale from serialized data; such a value is now rejected with an error.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/131154]
Version info
26.11.1.2170-master(included in26.11and later)