Repository navigation
Validate compressed block headers when identifying a file's codec - #109157
Conversation
|
Workflow [PR], commit [3471d93] Summary: ✅
AI ReviewSummaryThis PR hardens Findings
Final VerdictStatus: Minimum required actions:
|
| 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
| size_compressed, | ||
| DBMS_MAX_COMPRESSED_SIZE); | ||
|
|
||
| if (size_compressed < header_size) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
|
Updating the branch to fix the |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 109/110 (99.09%) · Uncovered code |
Validate compressed block headers when identifying a file's codec
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):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
clickhouse-compressor --statand default-codec detection for old parts now report corrupted block headers as corruption instead of a misleading end-of-file error.Version info
26.7.1.586(included in26.7and later)