Skip to content

Reject truncated sparse values in native protocol input with INCORRECT_DATA - #128363

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:sparse-truncated-values-native-incorrect-data
Oct 31, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:sparse-truncated-values-native-incorrect-data

Conversation

@groeneai

@groeneai groeneai commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #102975

Changelog category (leave one):

  • Improvement

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

A sparse column whose values are truncated in data sent over the native protocol is now rejected with INCORRECT_DATA instead of LOGICAL_ERROR.

Description

native_reader_fuzzer aborted in the nightly libFuzzer tests run (report) at the values/offsets size check of SerializationSparse::deserializeBinaryBulkWithMultipleStreams, which always throws LOGICAL_ERROR.

The same check is reachable from any TCP client: TCPHandler::initBlockInput reads INSERT data with NativeReader at the client's protocol revision, so the client chooses the SPARSE kind. If the values payload is shorter than the offsets declare, SerializationNumber::deserializeBinaryBulk short-reads on EOF without throwing, and the Sparse check is the first to see the mismatch. A debug or sanitizer server aborts; a release server answers LOGICAL_ERROR.

The check now throws INCORRECT_DATA when settings.native_format is set, as SerializationTuple, SerializationNullable, SerializationObject, SerializationVariant and the truncated-elements check of SerializationReplicated already do. Other readers (MergeTree and others) keep LOGICAL_ERROR, where this check catches real internal bugs.

#102975 added this ternary and reverted it because no test could reach the site from input data. The new test 05473_sparse_truncated_values_native_protocol reaches it from a raw native-protocol client (the offsets declare 2 values, 1 is sent) and expects code 117 with the server alive.

Validation: on a master debug build the test aborts the server with Offsets size: 2, values size: 2; with the fix it passes 50/50 randomized runs. Replaying the exact 183-byte fuzzer input over TCP aborts master with Offsets size: 66, values size: 5 and returns code 117 with the fix.


Workflow [PR]
Sync PR [sync-upstream/pr/128363]

Version info

  • Merged into: 26.11.1.976-master (included in 26.11 and later)

A TCP client chooses the serialization kind of the INSERT data it sends
(NativeReader, client protocol revision >= 54454). For a SPARSE column whose
values payload is shorter than its offsets declare, primitive bulk readers
such as SerializationNumber::deserializeBinaryBulk short-read on EOF without
throwing, so the values/offsets size check in
SerializationSparse::deserializeBinaryBulkWithMultipleStreams is the first to
see the mismatch. It always threw LOGICAL_ERROR, which aborts debug and
sanitizer servers (found by native_reader_fuzzer in the nightly libFuzzer
run).

Throw INCORRECT_DATA when settings.native_format is set, like the sibling
checks in SerializationTuple, SerializationNullable, SerializationObject,
SerializationVariant and SerializationReplicated. Other readers keep
LOGICAL_ERROR, where the check catches internal bugs. ClickHouse#102975 added this
ternary and reverted it for lack of a reachable test; the new test
05473_sparse_truncated_values_native_protocol reaches it from a raw
native-protocol client.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review (1 nit).

# Sev Finding Verdict Evidence / action
1 💡 The test copies the raw native-protocol client of 04616_replicated_serialization_oob_index_native_protocol AGREE, noted-not-blocking No shared native-protocol helper exists in tests/queries/0_stateless/helpers/; per-test copies are the in-tree convention. One scenario, no matrix.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261031-105700

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05473_sparse_truncated_values_native_protocol aborts a master debug server every run (Offsets size: 2, values size: 2); the 183-byte fuzzer input over TCP aborts it with Offsets size: 66, values size: 5.
b Root cause explained? Yes. A TCP client picks the SPARSE kind for INSERT data; a values payload that ends early short-reads on EOF without throwing, so the Sparse size check, hard-wired to LOGICAL_ERROR, fires first.
c Fix matches root cause? Yes. The check now reports INCORRECT_DATA for Native input (settings.native_format), the pattern its siblings already use. No guard, catch or widened bound.
d Test intent preserved / new tests added? Yes. New stateless test; the pinned sizes in the message prove the values short-read by exactly one.
e Both directions demonstrated? Yes. Master debug: server aborts. Fix: code 117, server alive, 50/50 randomized runs OK; fuzzer bytes give 117 too.
f Fix is general across code paths? Yes. Tuple, Nullable, Object, Variant and Replicated already use the same ternary; a truncated offsets stream already ends in CANNOT_READ_ALL_DATA.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Sparse over Nullable(UInt64) gives code 117 with the same sizes; the check compares sizes, so the value type does not matter.
h Backward compatible? (maintainer-approved exception only) Yes. Only the error code of an already failing path changes; non-Native readers keep LOGICAL_ERROR.
i Invariants and contracts preserved? Yes. Same throw site and message; the non-throwing path is untouched.

Session id: cron:clickhouse-impl-slot-4:20261031-102900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @antaljanosbenjamin, could you review this and add the can be tested label so CI runs? A TCP client can send a SPARSE column whose values are shorter than its offsets declare, which reaches the LOGICAL_ERROR size check in SerializationSparse (found by native_reader_fuzzer); it now throws INCORRECT_DATA for native input, the ternary #102975 reverted for lack of a test, which this PR adds.

@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a608265]

Summary: ✅


AI Review

Summary

The change makes SerializationSparse reject truncated values in Native input with INCORRECT_DATA, preserving LOGICAL_ERROR for other readers. No actionable findings at head a608265f8800ec7d52ef9d346ad9fda2017db2b4. The new regression test checks the specific size mismatch, server liveness, and absence of inserted rows, and passed in CI's Fast test job.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). labels Oct 31, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing a608265f8 with master 5332ba80c (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

8 translation units recompiled, 7 s compile time in total, 8 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Oct 31, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 31, 2026
Merged via the queue into ClickHouse:master with commit defab2b Oct 31, 2026
529 of 531 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 31, 2026
vdimir added a commit that referenced this pull request Nov 18, 2026
The change is taken as in the original (1a44640, #130895); only the following is adapted.

- `src/Formats/NativeReader.h`, `src/Formats/NativeReader.cpp`: the new `allowed_kinds_` parameter and the
  `allowed_kinds(allowed_kinds_)` initializer go after `block_missing_values_`. The master-only `type_cache_`
  parameter and initializer are not added: `NativeReaderTypeCache` (#129831) is not on the branch, and the
  change does not use it. The branch keeps its own `format_settings_ = std::nullopt` default.
- `src/Processors/Formats/Impl/NativeFormat.cpp` (`makeReader`) and `src/Server/TCPHandler.cpp`
  (`initBlockInput`): the `/*type_cache_=*/ nullptr,` argument is dropped, because the constructor on the
  branch has no such parameter. These two files merged without a conflict, but would not compile as merged.
- `tests/queries/0_stateless/04616_replicated_serialization_oob_index_native_protocol.python` and
  `.reference`: the huge-element-count scenario (`replicatedBlockWithHugeElementCount`,
  `hugeElementCountScenario`, its call in `main`, its reference line) is left out. It comes from #129932,
  which is not on the branch: the test there never had the scenario or the `expectException` helper, and
  `SerializationReplicated` has no 'Suspiciously many elements' check. The new
  `replicatedTupleElementScenario` and `replicatedOnInsertScenario` are kept, with their reference lines.
- `tests/queries/0_stateless/05473_sparse_truncated_values_native_protocol.python`, `.reference`, `.sh`:
  left out. The original only edits this test, and it comes from #128363, which is not on the branch
  (modify/delete conflict).
- `docs/reference/interfaces/specs/NativeFormat.mdx`: git applied the hunks of
  `docs/resources/develop-contribute/native-protocol/format.mdx` here, where the same document lives on
  the branch. The added and removed lines are the same as in the original.

Everything else merged without conflicts and has the same changed lines as the original.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
vdimir added a commit that referenced this pull request Nov 18, 2026
The change is taken as in the original (1a44640, #130895); only the following is adapted.

- `src/Formats/NativeReader.h`, `src/Formats/NativeReader.cpp`: the new `allowed_kinds_` parameter and the
  `allowed_kinds(allowed_kinds_)` initializer go after `block_missing_values_`. The master-only `type_cache_`
  parameter and initializer are not added: `NativeReaderTypeCache` (#129831) is not on the branch, and the
  change does not use it. The branch keeps its own `format_settings_ = std::nullopt` default.
- `src/Processors/Formats/Impl/NativeFormat.cpp` (`makeReader`) and `src/Server/TCPHandler.cpp`
  (`initBlockInput`): the `/*type_cache_=*/ nullptr,` argument is dropped, because the constructor on the
  branch has no such parameter. These two files merged without a conflict, but would not compile as merged.
- `tests/queries/0_stateless/04616_replicated_serialization_oob_index_native_protocol.python` and
  `.reference`: the huge-element-count scenario (`replicatedBlockWithHugeElementCount`,
  `hugeElementCountScenario`, its call in `main`, its reference line) is left out. It comes from #129932,
  which is not on the branch: the test there never had the scenario or the `expectException` helper, and
  `SerializationReplicated` has no 'Suspiciously many elements' check. The new
  `replicatedTupleElementScenario` and `replicatedOnInsertScenario` are kept, with their reference lines.
- `tests/queries/0_stateless/05473_sparse_truncated_values_native_protocol.python`, `.reference`, `.sh`:
  left out. The original only edits this test, and it comes from #128363, which is not on the branch
  (modify/delete conflict).

Everything else merged without conflicts and has the same changed lines as the original.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
vdimir added a commit that referenced this pull request Nov 18, 2026
The change is taken as in the original (1a44640, #130895); only the following is adapted.

- `src/Formats/NativeReader.h`, `src/Formats/NativeReader.cpp`: the new `allowed_kinds_` parameter and the
  `allowed_kinds(allowed_kinds_)` initializer go after `block_missing_values_`. The master-only `type_cache_`
  parameter and initializer are not added: `NativeReaderTypeCache` (#129831) is not on the branch, and the
  change does not use it. The branch keeps its own `format_settings_ = std::nullopt` default.
- `src/Processors/Formats/Impl/NativeFormat.cpp` (`makeReader`) and `src/Server/TCPHandler.cpp`
  (`initBlockInput`): the `/*type_cache_=*/ nullptr,` argument is dropped, because the constructor on the
  branch has no such parameter. These two files merged without a conflict, but would not compile as merged.
- `tests/queries/0_stateless/04616_replicated_serialization_oob_index_native_protocol.python` and
  `.reference`: the huge-element-count scenario (`replicatedBlockWithHugeElementCount`,
  `hugeElementCountScenario`, its call in `main`, its reference line) is left out. It comes from #129932,
  which is not on the branch: the test there never had the scenario or the `expectException` helper, and
  `SerializationReplicated` has no 'Suspiciously many elements' check. The new
  `replicatedTupleElementScenario` and `replicatedOnInsertScenario` are kept, with their reference lines.
- `tests/queries/0_stateless/05473_sparse_truncated_values_native_protocol.python`, `.reference`, `.sh`:
  left out. The original only edits this test, and it comes from #128363, which is not on the branch
  (modify/delete conflict).
- `docs/reference/interfaces/specs/NativeFormat.mdx`: git applied the hunks of
  `docs/resources/develop-contribute/native-protocol/format.mdx` here, where the same document lives on
  the branch. The added and removed lines are the same as in the original.

Everything else merged without conflicts and has the same changed lines as the original.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
vdimir added a commit that referenced this pull request Nov 18, 2026
The change is taken as in the original (1a44640, #130895), with the adaptations below.

Adapted:
- `src/Formats/NativeReader.h`, `src/Formats/NativeReader.cpp`: the new `allowed_kinds_` parameter and the
  `allowed_kinds(allowed_kinds_)` initializer go after `block_missing_values_`, without the master-only
  `type_cache_` (`NativeReaderTypeCache`, #129831). The branch keeps its own
  `format_settings_ = std::nullopt` default.
- `src/Processors/Formats/Impl/NativeFormat.cpp` (`makeReader`): the `/*type_cache_=*/ nullptr,` argument is
  dropped. The file merged without a conflict, but would not compile as merged.
- `src/Server/TCPHandler.cpp`: the original only adds the `ISerialization::KindSet{ISerialization::Kind::DEFAULT}`
  argument; `getNativeWireFormatSettings` (#123649) is on its context lines and is not on the branch.
  `skipDataPacket` passes no format settings here, so an explicit `std::nullopt` stands in that position.
  `initBlockInput` keeps `getFormatSettings(state.query_context)`, with an explicit `nullptr` for
  `block_missing_values_` and without `type_cache_`. Both comments of the original are kept.
- `src/Interpreters/AsynchronousInsertQueue.cpp`: `IColumn::convertToFullIfWrapped` (#107773) is not on the
  branch, and `convertToFullIfNeeded` also strips `LowCardinality`. The check uses a file-local
  `hasWrappedColumn` instead: a `ColumnBLOB`, `ColumnConst`, `ColumnReplicated` or `ColumnSparse` at any
  nesting depth, `LowCardinality` left alone, nothing converted. The exception and its message are as in the
  original. The only producers of `pushQueryWithBlock` on the branch are the two calls in
  `TCPHandler::processAsyncInsertQuery`: a header without columns, and the squashed blocks that
  `initBlockInput` reads, which now accepts only the default kind.

Left out:
- `tests/queries/0_stateless/04616_replicated_serialization_oob_index_native_protocol.python` and `.reference`:
  the scenarios the branch does not have: invalid index type size, index row count mismatch, sparse over
  replicated (#114186) and huge element count (#129932). The rewrite of the two existing scenarios and the new
  `replicatedTupleElementScenario` and `replicatedOnInsertScenario` are kept, with their reference lines.
- `tests/queries/0_stateless/05473_sparse_truncated_values_native_protocol.python`, `.reference`, `.sh`: the
  original only edits this test, and it comes from #128363, which is not on the branch (modify/delete
  conflict).
- `docs/resources/develop-contribute/native-protocol/format.mdx`: the branch has no English specification of
  the `Native` format, so the hunks have nowhere to go (modify/delete conflict).

`tests/queries/0_stateless/helpers/fake_native_server.py` is as in the original here. It does not work with
the client of this branch, which sends a `Ping` before each query; the next commit adapts it.

Everything else merged without conflicts and has the same changed lines as the original.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-datatype-wrapper Type modifiers/wrappers (Nullable, LowCardinality, etc.). pr-improvement Pull request with some product improvements 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