瀏覽代碼

LibPDF: Fix some base-encoding-related crashes

Matthew Olsson 3 年之前
父節點
當前提交
49cb040c27
共有 1 個文件被更改,包括 12 次插入10 次删除
  1. 12 10
      Userland/Libraries/LibPDF/Encoding.cpp

+ 12 - 10
Userland/Libraries/LibPDF/Encoding.cpp

@@ -27,14 +27,14 @@ PDFErrorOr<NonnullRefPtr<Encoding>> Encoding::from_object(Document* document, No
     // Make a custom encoding
     // Make a custom encoding
     auto dict = obj->cast<DictObject>();
     auto dict = obj->cast<DictObject>();
 
 
-    // FIXME: If this entry is absent, the Differences entry shall describe differences
-    //        from an implicit base encoding. For a font program that is embedded in the
-    //        PDF file, the implicit base encoding shall be a font program's built-in
-    //        encoding [...]. Otherwise, for a nonsymbolic font, it shall be
-    //        StandardEncoding, and for a symbolic font, it shall be the font's built-in
-    //        encoding.
-    auto base_encoding_obj = MUST(dict->get_object(document, CommonNames::BaseEncoding));
-    auto base_encoding = TRY(Encoding::from_object(document, base_encoding_obj));
+    RefPtr<Encoding> base_encoding;
+    if (dict->contains(CommonNames::BaseEncoding)) {
+        auto base_encoding_obj = MUST(dict->get_object(document, CommonNames::BaseEncoding));
+        base_encoding = TRY(Encoding::from_object(document, base_encoding_obj));
+    } else {
+        base_encoding = Encoding::standard_encoding();
+    }
+
     auto encoding = adopt_ref(*new Encoding());
     auto encoding = adopt_ref(*new Encoding());
 
 
     // Build a String -> Character mapping for handling the differences map
     // Build a String -> Character mapping for handling the differences map
@@ -62,8 +62,10 @@ PDFErrorOr<NonnullRefPtr<Encoding>> Encoding::from_object(Document* document, No
             auto name = object->cast<NameObject>()->name();
             auto name = object->cast<NameObject>()->name();
 
 
             auto character = base_encoding_name_mapping.get(name);
             auto character = base_encoding_name_mapping.get(name);
-            VERIFY(character.has_value());
-            encoding->m_descriptors.set(current_code_point, character.value());
+            // FIXME: This should always have a value. This does cause crashes in certain
+            //        documents, so we must be missing something here.
+            if (character.has_value())
+                encoding->m_descriptors.set(current_code_point, character.value());
 
 
             current_code_point++;
             current_code_point++;
         }
         }