From 8de7a91571a30682aea95338b8184c33802a62dd Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Fri, 16 Sep 2022 19:21:29 +0200 Subject: [PATCH] LibCompress+Tests: Demonstrate and fix faulty metadata length The test-case is heavily inspired by: https://github.com/google/brotli/blob/master/tests/testdata/x.compressed.01 Or in words: A metadata meta-block containing `Y` (which should be ignored), and then the actual data (a single `Z`). The bug used to skip one metadata byte too few, and thus read garbage. --- Tests/LibCompress/TestBrotli.cpp | 5 +++++ Tests/LibCompress/brotli-test-files/single-z.txt | 1 + Tests/LibCompress/brotli-test-files/single-z.txt.br | Bin 0 -> 8 bytes Userland/Libraries/LibCompress/Brotli.cpp | 2 +- 4 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 Tests/LibCompress/brotli-test-files/single-z.txt create mode 100644 Tests/LibCompress/brotli-test-files/single-z.txt.br diff --git a/Tests/LibCompress/TestBrotli.cpp b/Tests/LibCompress/TestBrotli.cpp index bff42326f50..5c4e9788123 100644 --- a/Tests/LibCompress/TestBrotli.cpp +++ b/Tests/LibCompress/TestBrotli.cpp @@ -75,6 +75,11 @@ TEST_CASE(brotli_decompress_katica_regular_10_font) run_test("KaticaRegular10.font"sv); } +TEST_CASE(brotli_single_z) +{ + run_test("single-z.txt"sv); +} + TEST_CASE(brotli_decompress_zero_one_bin) { // This makes sure that the tests will run both on target and in Lagom. diff --git a/Tests/LibCompress/brotli-test-files/single-z.txt b/Tests/LibCompress/brotli-test-files/single-z.txt new file mode 100644 index 00000000000..0f137124110 --- /dev/null +++ b/Tests/LibCompress/brotli-test-files/single-z.txt @@ -0,0 +1 @@ +Z \ No newline at end of file diff --git a/Tests/LibCompress/brotli-test-files/single-z.txt.br b/Tests/LibCompress/brotli-test-files/single-z.txt.br new file mode 100644 index 0000000000000000000000000000000000000000..8eb59f49399e5d9d3a4b00c3d3ebd65feaecbe36 GIT binary patch literal 8 PcmdO4h-6^kh++l+1Wf?z literal 0 HcmV?d00001 diff --git a/Userland/Libraries/LibCompress/Brotli.cpp b/Userland/Libraries/LibCompress/Brotli.cpp index faae273bbec..2f150fea816 100644 --- a/Userland/Libraries/LibCompress/Brotli.cpp +++ b/Userland/Libraries/LibCompress/Brotli.cpp @@ -605,7 +605,7 @@ ErrorOr BrotliDecompressionStream::read(Bytes output_buffer) return Error::from_string_literal("invalid reserved bit"); size_t skip_bytes = TRY(m_input_stream.read_bits(2)); - size_t skip_length = TRY(m_input_stream.read_bits(8 * skip_bytes)); + size_t skip_length = 1 + TRY(m_input_stream.read_bits(8 * skip_bytes)); u8 remainder = m_input_stream.align_to_byte_boundary(); if (remainder != 0)