Skip to content

Validate compressed block headers when identifying a file's codec - #109157

Merged
rienath merged 11 commits into
masterfrom
block-walk-prelim
Jul 7, 2026
Merged

rienath merged 11 commits into
masterfrom
block-walk-prelim

Conversation

@rienath

@rienath rienath commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

ClickHouse can tell which codec wrote a compressed file from its block headers alone, without decompressing the data. On a corrupted file this used to fail with a confusing end-of-file error or silently succeed. It now reports corruption, naming the bad size.

Adaptive codec selection (RFC #105404) is why this matters more now. It mixes codecs block by block, and each block's codec is recorded only in its own header. A planned table function walks every block of a part to report codec usage. Today the walk is done by clickhouse-compressor --stat. That will make it reachable from SQL, so it must diagnose corruption rather than stumble over it.

Changelog category (leave one):

  • Improvement

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

clickhouse-compressor --stat and default-codec detection for old parts now report corrupted block headers as corruption instead of a misleading end-of-file error.

Version info

  • Merged into: 26.7.1.586 (included in 26.7 and later)

@clickhouse-gh

clickhouse-gh Bot commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3471d93]

Summary: ✅


AI Review

Summary

This PR hardens getCompressionCodecForFile against some malformed size headers and adds focused unit tests, but the contract in the PR description is still not fully met. The current code still silently accepts several impossible block shapes and still leaks EOF-style failures for truncated files, so clickhouse-compressor --stat and old-part default codec detection can both keep treating corrupted input as valid.

Findings

⚠️ Majors

  • [src/Compression/getCompressionCodecForFile.cpp:64-86] [dismissed by author -- https://github.com/Validate compressed block headers when identifying a file's codec #109157#discussion_r3513152866] Multiple headers are still accepted even when they cannot describe any valid compressed block. codecs_count == 0 still returns CompressionCodecMultiple({}), and size_compressed == nested_block_offset still returns CODEC(...) with no nested block header at all. clickhouse-compressor --stat therefore still prints a codec for malformed data, and IMergeTreeDataPart::detectDefaultCompressionCodec can still derive CODEC() from a corrupted old part and propagate that bogus default codec. Suggested fix: reject codecs_count == 0 and require enough bytes for the nested block header before constructing the codec chain, then add regressions for both cases.
  • [src/Compression/getCompressionCodecForFile.cpp:42-50,89-90] [dismissed by author -- https://github.com/Validate compressed block headers when identifying a file's codec #109157#discussion_r3528697687] A non-Multiple block with size_compressed == ICompressionCodec::getHeaderSize() and size_decompressed > 0 is still treated as valid. With skip_to_next_block = true, clickhouse-compressor --stat does ignore(0) and cleanly reports a codec for a header-only block; with skip_to_next_block = false, old-part default codec detection returns the codec immediately. That is still silent acceptance of a corrupt size header, which is the specific contract this PR is trying to tighten. Suggested fix: reject zero-payload blocks here and add a regression that exercises both skip modes.
  • [src/Compression/getCompressionCodecForFile.cpp:83-90] [dismissed by author -- https://github.com/Validate compressed block headers when identifying a file's codec #109157#discussion_r3529095448] Truncated files still escape the new corruption path. If the header advertises extra payload bytes that are not physically present, clickhouse-compressor --stat still throws ATTEMPT_TO_READ_AFTER_EOF from ignore, and the skip_to_next_block = false caller never checks the advertised payload length at all, so IMergeTreeDataPart::detectDefaultCompressionCodec silently accepts the same truncated block. The PR description says corrupted files should stop surfacing EOF or silent success here, and the current code still does both. Suggested fix: translate short reads/skips against the declared size_compressed into CORRUPTED_DATA in this helper and add a regression for the truncated-file case.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  • Reject malformed Multiple wrappers that cannot contain a valid nested block.
  • Stop accepting header-only or truncated blocks as valid codec detections, and cover those cases with focused regressions.

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Jul 2, 2026
const size_t codecs_count = static_cast<UInt8>(compressed_buffer[0]);
const size_t bytes_needed = static_cast<UInt32>(header_size) + 1 + codecs_count;

if (size_compressed < bytes_needed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still accepts malformed Multiple headers that CompressionCodecMultiple::doDecompressData rejects immediately. If codecs_count == 0, or if there are fewer than ICompressionCodec::getHeaderSize() bytes left after the methods list, getCompressionCodecForFile will return CODEC() / CODEC(...) and clickhouse-compressor --stat keeps walking even though no decoder can read that block. Can we reject those shapes here as CORRUPTED_DATA before constructing the codec chain?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional. This function never decompresses, so it validates only what the block walk relies on: the size bounds and that the codec list fits the declared size. Decompressibility of the body is the decompressor's job. Duplicating the decoder's structural rules here would just be a second copy to keep in sync, for no added protection

@Michicosun Michicosun self-assigned this Jul 6, 2026
size_compressed,
DBMS_MAX_COMPRESSED_SIZE);

if (size_compressed < header_size)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_compressed == header_size is still a false negative here. Once size_decompressed > 0 is required, a block that contains only the 9-byte header and no payload cannot have been produced by any codec, but this guard lets it through. In clickhouse-compressor --stat, ignore(size_compressed - header_size) then becomes ignore(0), so a file consisting of just checksum+header is reported as a valid codec and EOFs cleanly instead of surfacing corruption; old-part default codec detection has the same hole with skip_to_next_block = false. Can we reject the zero-payload case here and add a regression for it?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional. Please see #109157 (comment). We only verify header validity here, not the compressed block itself. That the decompressor does and I'd rather keep it there to separate concerns


if (skip_to_next_block)
read_buffer.ignore(size_compressed - (read_buffer.count() - starting_bytes));
read_buffer.ignore(size_compressed - header_size);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clickhouse-compressor --stat can still leak the old ATTEMPT_TO_READ_AFTER_EOF here when the file is physically shorter than the declared size_compressed. A block with size_compressed = 13, size_decompressed = 1, and only the 9-byte header present reaches this ignore(4) and throws EOF, even though the PR description/changelog say corrupted size headers now report corruption instead of EOF. Can we translate short reads/skips in this helper into CORRUPTED_DATA that mentions the declared size, and add a regression for that truncated-file case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are only validating the header in this file. ATTEMPT_TO_READ_AFTER_EOF is correct diagnosis

/// Decompression rejects blocks with decompressed size 0, so identification must too.
constexpr unsigned char block[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /// 16-byte checksum (ignored)
0x82, /// LZ4 method byte

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size_decompressed is still 0 in both this fixture and ThrowsOnCorruptSizeEvenWithoutSkip, so these tests would keep passing even if the new size_compressed < header_size guard disappeared or moved below the zero-decompressed-size check. Since they are meant to pin the undersized-size_compressed regression in both skip modes, can we make size_decompressed non-zero here (as you already did for the Multiple regression) so they actually prove that branch?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@alexey-milovidov

Copy link
Copy Markdown
Member

Updating the branch to fix the 04001_join_reorder_through_expression test, which was broken today in master.

@clickhouse-gh

clickhouse-gh Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.60% 85.60% +0.00%
Functions 92.70% 92.60% -0.10%
Branches 77.80% 77.80% +0.00%

Changed lines: Changed C/C++ lines covered: 109/110 (99.09%) · Uncovered code

Full report · Diff report

@rienath
rienath added this pull request to the merge queue Jul 7, 2026
Merged via the queue into master with commit 6a27522 Jul 7, 2026
343 of 344 checks passed
@rienath
rienath deleted the block-walk-prelim branch July 7, 2026 11:03
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 7, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Validate compressed block headers when identifying a file's codec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

4 participants