Skip to content

metaUtils: require Z_STREAM_END when validating uncompressed data#145

Merged
dzenanz merged 1 commit into
Kitware:masterfrom
hjmjohnson:fix-uncompress-require-stream-end
Jul 23, 2026
Merged

metaUtils: require Z_STREAM_END when validating uncompressed data#145
dzenanz merged 1 commit into
Kitware:masterfrom
hjmjohnson:fix-uncompress-require-stream-end

Conversation

@hjmjohnson

Copy link
Copy Markdown
Contributor

Require Z_STREAM_END in MET_PerformUncompression, not just the expected byte count, so a compressed stream truncated exactly at the declared output size is rejected instead of silently accepted. Fixes #144; hardens the byte-count check from #142.

Root cause

When the decompressed data fills the destination buffer exactly, the last inflate() returns Z_OK and the next call (with avail_out == 0) returns Z_BUF_ERROR. That is < 0, so the loop exits — but Z_BUF_ERROR is deliberately excluded from the "Uncompress failed" diagnostic (treated as non-fatal "buffer full"). Control reaches the final check with dest_pos == uncompressedDataSize and the function returns true, even though Z_STREAM_END was never reached and the trailing Adler-32 checksum was never validated.

Adding an explicit err != Z_STREAM_END rejection guarantees the entire stream (including its trailer) was consumed, closing the exact-size-truncation gap while keeping the informative byte-count message for the short-read case.

MET_PerformUncompression validated the number of bytes produced but not
that the zlib stream terminated with Z_STREAM_END. When the output fills
the destination buffer exactly, the next inflate() returns Z_BUF_ERROR
(deliberately excluded from the diagnostic), the loop exits, and the
byte-count check passes -- so a stream truncated at the size boundary is
accepted without validating its trailing Adler-32 checksum.

Reject the data unless inflate reported Z_STREAM_END, in addition to the
existing byte-count check. This closes the exact-size truncation gap left
by the byte-count check added in Kitware#142.

Fixes Kitware#144.
@hjmjohnson
hjmjohnson marked this pull request as ready for review July 23, 2026 22:17
@hjmjohnson

Copy link
Copy Markdown
Contributor Author

@dzenanz I don't have merge capabilities here. This was a possible failure case found by GrepTile.

@dzenanz
dzenanz merged commit 253d202 into Kitware:master Jul 23, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MET_PerformUncompression accepts exact-size truncated streams (missing Z_STREAM_END check)

2 participants