LibCompress: Error on truncated uncompressed DEFLATE blocks

This commit is contained in:
Tim Schumacher 2023-04-12 16:03:36 +02:00 committed by Tim Flynn
parent f56b897622
commit 4098335600
Notes: sideshowbarker 2024-07-17 02:08:15 +09:00
2 changed files with 15 additions and 0 deletions

View File

@ -95,3 +95,15 @@ TEST_CASE(gzip_round_trip)
EXPECT(!uncompressed.is_error());
EXPECT(uncompressed.value() == original);
}
TEST_CASE(gzip_truncated_uncompressed_block)
{
Array<u8, 38> const compressed {
0x1F, 0x8B, 0x08, 0x13, 0x5D, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x85, 0x1C,
0x1C, 0xFF, 0xDB, 0xFB, 0xFF, 0xDB
};
auto const decompressed_or_error = Compress::GzipDecompressor::decompress_all(compressed);
EXPECT(decompressed_or_error.is_error());
}

View File

@ -238,6 +238,9 @@ ErrorOr<bool> DeflateDecompressor::UncompressedBlock::try_read_more()
if (m_bytes_remaining == 0)
return false;
if (m_decompressor.m_input_stream->is_eof())
return Error::from_string_literal("Input data ends in the middle of an uncompressed DEFLATE block");
Array<u8, 4096> temporary_buffer;
auto readable_bytes = temporary_buffer.span().trim(min(m_bytes_remaining, m_decompressor.m_output_buffer.empty_space()));
auto read_bytes = TRY(m_decompressor.m_input_stream->read_some(readable_bytes));