From 43cd3d7dbda0322d5825a72eafdb5106ea6b3d82 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 6 Dec 2023 19:11:06 -0500 Subject: [PATCH] LibPDF: Tolerate palettes that are one byte too long Fixes these errors from `Meta/test_pdf.py path/to/0000`, with 0000 being 0000.zip from the PDF/A corpus in unzipped: Malformed PDF file: Indexed color space lookup table doesn't match size, in 4 files, on 8 pages, 73 times path/to/0000/0000206.pdf 2 4 (2x) 5 (3x) 6 (4x) path/to/0000/0000364.pdf 5 6 path/to/0000/0000918.pdf 5 path/to/0000/0000683.pdf 8 --- Userland/Libraries/LibPDF/ColorSpace.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibPDF/ColorSpace.cpp b/Userland/Libraries/LibPDF/ColorSpace.cpp index 8af8ae1716..a324fabd64 100644 --- a/Userland/Libraries/LibPDF/ColorSpace.cpp +++ b/Userland/Libraries/LibPDF/ColorSpace.cpp @@ -669,8 +669,15 @@ PDFErrorOr> IndexedColorSpace::create(Document* docume return Error { Error::Type::MalformedPDF, "Indexed color space expects stream or string for third arg" }; } - if (static_cast(lookup.size()) != (hival + 1) * base->number_of_components()) + size_t needed_size = (hival + 1) * base->number_of_components(); + if (lookup.size() - 1 == needed_size) { + // FIXME: Could do this if lookup.size() > needed_size generally, but so far I've only seen files that had one byte too much. + lookup.resize(needed_size); + } + if (lookup.size() != needed_size) { + dbgln("lookup size {} doesn't match hival {} and base components {}", lookup.size(), hival, base->number_of_components()); return Error { Error::Type::MalformedPDF, "Indexed color space lookup table doesn't match size" }; + } auto color_space = adopt_ref(*new IndexedColorSpace(move(base))); color_space->m_hival = hival;