Repository navigation
Cherry pick #115702 to 26.3: Fix integer overflow in String deserialization - #118146
Merged
robot-clickhouse-ci-2 merged 21 commits intoSep 5, 2026
Merged
Conversation
…n every deserialization path `SerializationString::deserializeBinary` for a `Field` had no hard upper bound of its own, and that path is reachable for the values of the paths that go into the shared data of a `JSON` column, so a malformed nested string could still be passed to `std::string::resize` when `format_binary_max_string_size` is disabled. Both overloads now share `checkStringSize`, which enforces the setting and `SerializationString::MAX_STRING_SIZE`. `SerializationDynamic::deserializeBinary` and `SerializationObject::deserializeBinary` for a column also recurse into each other on the main column-building path of `RowBinary` and `Native`, so the `checkStackSize` call is now made there as well and not only in the `Field` overloads.
…e redundant stack checks The list of types of a flattened `Dynamic` column is now checked for duplicates while it is read, instead of in a second pass over the list, and a test for a list with a duplicate type is added. `SerializationObject::deserializeBinary` does not recurse into itself: every nested value goes through `SerializationDynamic::deserializeBinary`, the only place where the type of a nested value comes from the data. So the check of the stack size there bounds the whole mutual recursion, and the checks in `SerializationObject` are removed. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… a per-row cost The skipped prefix of a seeked read (`rows_offset > 0`) summed the sizes from the sizes stream into `bytes_to_skip` without any check, so a corrupted stream could wrap the sum around and make the read start from a wrong byte boundary in the data stream instead of rejecting the part. The prefix now goes through the same validation as the sizes of the materialized rows, and both cases are covered by unit tests over hand-crafted sizes streams. The per-row checks that the previous commits added to `appendStringSizesToColumnStringOffsets` slowed reading of short strings down by 15-55% (https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115702&sha=5ba92a1a90a0c126424a7d9c8fbcb2f82c750c25&name_0=PR, https://performance.ci.clickhouse.com/runs?q=115702), because the accumulation of the offsets is one of the hot loops of reading a `String` column. The loop now OR-folds the sizes into a single value that bounds each of them from above and checks it once per chunk sized so that a chunk of validated sizes cannot wrap `UInt64`; in the astronomically rare case when the OR exceeds the limit (which can also combine the bits of legitimate sizes), the accumulation is rolled back and redone with the exact per-size validation. In a microbenchmark the OR-folding loop matches the unchecked one (0.63 ns/row), while the per-row-checked one takes 0.94 ns/row.
…ization-overflow # Conflicts: # src/DataTypes/Serializations/SerializationString.cpp
…lization A typed path of a `JSON` type recurses back into `SerializationObject::deserializeBinary` directly, without passing through `SerializationDynamic`, and the nesting depth of such a type comes from the data (the binary encoding of a `Dynamic` value starts with the encoding of its type). So the stack size checks in `SerializationObject::deserializeBinary` were not redundant after all: they are restored, and `05033_row_binary_huge_string_size` now covers a `Dynamic` value with a deeply nested typed-path `JSON` type, which has to be rejected with `TOO_DEEP_RECURSION` instead of exhausting the stack. Also adapt the new tests to the current master: `rows_offset` was removed from the deserialization API in #115731, which makes the skipped-prefix validation and its unit test moot. https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=115702&sha=latest&name_0=PR Co-Authored-By: Claude Fable 5 <[email protected]>
`deserializeStringOffsetsAndGetDataSize` reads the size stream in two layouts. The `position_independent_encoding` one carries per-row sizes and now rejects a size above `SerializationString::MAX_STRING_SIZE`, but the other one - the cumulative byte offsets that `Native` carries (`NativeReader` sets `position_independent_encoding` to `false`) - only checked that the offsets increase monotonically. A monotonically increasing stream can still put a single string above the limit while the total stays below the limit on the size of the whole column, so the hard cap was bypassable there. The difference between two consecutive offsets is the size of a string, so it is checked against the same limit, which also subsumes the monotonicity check: the difference of a non-monotonic pair wraps around and lands far above it. The difference between the last offset of the column and the first one of the stream is now covered as well; it was outside the range that `std::adjacent_find` scanned.
…the containers The type of a `Dynamic` value comes from the data, and after `decodeDataType` returns it the value is read by `field_type->getDefaultSerialization()->deserializeBinary(...)`, which recurses through `SerializationArray`, `SerializationTuple` and `SerializationMap` for a nested container type. Those did not check the stack, so the guard added to `SerializationDynamic` and `SerializationObject` only covered the types that pass through `Dynamic` or `JSON` again. In practice the depth was already bounded: `decodeDataTypeImpl` and `IDataType::getDefaultSerialization` check the stack on the way in, and their frames are larger than the ones of the value deserialization, so they throw first. That is a property of the generated code rather than of the contract, so the containers now check the stack themselves. The check is a thread-local load and two comparisons, once per container value.
…offsets validation The offsets of a `String` column in the `Native` format are now validated not only for monotonicity but also for the size of every string being within `MAX_STRING_SIZE`, so the corrupted pair `[0xfffffffffffffff0, 0x20]` of the test is rejected with `TOO_LARGE_STRING_SIZE` on the very first offset, before the pair is found to be non-monotonic. Split the test into three payloads so that both rejections stay covered: a non-monotonic pair, an offset far past the data, and a monotonic pair whose difference is above the limit.
…formed size stream is rejected The new validation appends offsets to the destination column before the batch is known to be valid, so a rejection could leave `offsets.back() > chars.size()` behind for a caller that catches the exception and reuses the column. Add a rollback guard in `deserializeBinaryBulkWithSizeStream` that restores both `offsets` and `chars` to their pre-call sizes on every exception path, and a gtest for it.
…ding and the rollback `appendStringSizesToColumnStringOffsets` accumulates the offsets in 128 bits, which cannot overflow, so the total is checked once at the end instead of bounding every size on the way. Below a total within `MAX_TOTAL_STRING_SIZE` every offset is exact, so a valid stream is accumulated exactly as before. This replaces the chunked OR-folding of the sizes and the rollback that redid the accumulation with the exact per-size validation, at the same speed: on short strings the loop takes 0.33 ns/row against 0.33 ns/row for the OR-folded one and 0.30 ns/row for the unchecked one, while the per-size validation takes 0.47 ns/row. The `Native` path, which carries cumulative offsets instead of sizes, checks that they increase monotonically with `std::adjacent_find`, the way `SerializationArray` does. The total of a monotonic stream of offsets is already checked by the caller, so the sizes it describes are bounded as well and the OR-folding of the differences is not needed there either. `ColumnStringRollbackGuard` is removed: `deserializeBinaryBulk*` does not restore the destination column, a caller that needs it intact uses `ColumnCheckpoint`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…rom the data A type that nests deeply has to be decoded before any value of it is read, and `decodeDataTypeImpl` checks the stack while decoding it, so the guards in `SerializationArray`, `SerializationTuple`, `SerializationMap` and `SerializationObject` cannot be reached: the decoding throws first. They are removed, together with their cost of a thread-local load and two comparisons per container value. `SerializationDynamic::deserializeBinary` keeps its check. There the depth comes from the number of values rather than from one deep type: every nested value carries its own type tag, so nothing bounds the recursion but the size of the input. The tests of the two recursion cases are unchanged and still pass: the deeply nested `JSON` value is rejected by the check in `SerializationDynamic`, and the deeply nested type of a typed path by the one in `decodeDataTypeImpl`, both with `TOO_DEEP_RECURSION`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Master already has `05034_capnproto_deep_schema`, so the two tests of this pull request take the next free numbers.
…is pull request The check and its test are already in #117765, in the same place and in a stricter form: that pull request also rejects `Nothing` in the list of types and a structure version that does not belong to the format being read. Keeping both would only conflict, and this pull request is about the deserialization of `String`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…-overflow Fix integer overflow in String deserialization
Resolve the conflicts of the cherry-pick of #115702 against 26.3: - `SerializationDynamic.cpp`: keep the 26.3 `decodeDataType(istr)` call and add the `checkStackSize` guards. - `SerializationString.cpp`: 26.3 has no `MAX_TOTAL_STRING_SIZE` and no Native offsets stream (`deserializeStringOffsetsAndGetDataSize`), so add the constant, the `checkStringSize` helper and the 128-bit accumulator in `appendStringSizesToColumnStringOffsets`; the offsets-stream hunk does not apply. - `gtest_string_serialization.cpp`: port `WithSizeStreamHugeSizeIsRejected` to the 26.3 signatures (`ColumnPtr &`, `rows_offset`), drop the offsets-stream test. - `src/Formats/tests/gtest_native_string_size_stream.cpp` does not exist on 26.3: dropped.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #115702
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job