From 1a58fee0fd9aa4dd7c1ef136c4dd7b3884120aec Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 21 Oct 2023 10:42:16 -0400 Subject: [PATCH] LibPDF: Don't assert on named simple color space If a PDF uses `/CustomName cs` and `/CustomName` then points at just a name like `/DeviceGray` instead of an array, that's ok. Just using `/DeviceGray cs` is simpler, so this extra level of indirection is somewhat rare in practice, but it's valid and it does happen. So support it. We already have a helper that does the right thing that we just need to call. Together with #21524 and #21525, reduces number of crashes on 300 random PDFs from the web (the first 300 from 0000.zip from https://pdfa.org/new-large-scale-pdf-corpus-now-publicly-available/) from 29 (9%) to 25 (8%). --- Userland/Libraries/LibPDF/Renderer.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 0f4ab8e111a..33d5af3df94 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -975,12 +975,16 @@ PDFErrorOr> Renderer::get_color_space_from_resources(V dbgln("missing key {}", color_space_name); return Error::rendering_unsupported_error("Missing entry for color space name"); } - auto color_space_array = TRY(color_space_resource_dict->get_array(m_document, color_space_name)); - return ColorSpace::create(m_document, color_space_array); + return get_color_space_from_document(TRY(color_space_resource_dict->get_object(m_document, color_space_name))); } PDFErrorOr> Renderer::get_color_space_from_document(NonnullRefPtr color_space_object) { + // "A color space is defined by an array object whose first element is a name object identifying the color space family. + // The remaining array elements, if any, are parameters that further characterize the color space; + // their number and types vary according to the particular family. + // For families that do not require parameters, the color space can be specified simply by the family name itself instead of an array." + // Pattern cannot be a name in these cases if (color_space_object->is()) { return ColorSpace::create(color_space_object->cast()->name());