From 40983356003f9412694953014ae9a4f2c2bfac7a Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 12 Apr 2023 16:03:36 +0200 Subject: [PATCH] LibCompress: Error on truncated uncompressed DEFLATE blocks --- Tests/LibCompress/TestGzip.cpp | 12 ++++++++++++ Userland/Libraries/LibCompress/Deflate.cpp | 3 +++ 2 files changed, 15 insertions(+) diff --git a/Tests/LibCompress/TestGzip.cpp b/Tests/LibCompress/TestGzip.cpp index 39082ca3572..be90cd88e83 100644 --- a/Tests/LibCompress/TestGzip.cpp +++ b/Tests/LibCompress/TestGzip.cpp @@ -95,3 +95,15 @@ TEST_CASE(gzip_round_trip) EXPECT(!uncompressed.is_error()); EXPECT(uncompressed.value() == original); } + +TEST_CASE(gzip_truncated_uncompressed_block) +{ + Array 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()); +} diff --git a/Userland/Libraries/LibCompress/Deflate.cpp b/Userland/Libraries/LibCompress/Deflate.cpp index 92fa5884142..bdb7135f3e1 100644 --- a/Userland/Libraries/LibCompress/Deflate.cpp +++ b/Userland/Libraries/LibCompress/Deflate.cpp @@ -238,6 +238,9 @@ ErrorOr 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 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));