76f8c5
From 614514f577bbe676f736afcd8065892df8391315 Mon Sep 17 00:00:00 2001
76f8c5
From: Marek Kasik <mkasik@redhat.com>
76f8c5
Date: Fri, 20 Apr 2018 11:38:13 +0200
76f8c5
Subject: [PATCH] Fix crash on missing embedded file
76f8c5
76f8c5
Check whether an embedded file is actually present in the PDF
76f8c5
and show warning in that case.
76f8c5
76f8c5
https://bugs.freedesktop.org/show_bug.cgi?id=106137
76f8c5
https://gitlab.freedesktop.org/poppler/poppler/issues/236
76f8c5
---
76f8c5
 glib/poppler-attachment.cc | 26 +++++++++++++++++---------
76f8c5
 glib/poppler-document.cc   |  3 ++-
76f8c5
 2 files changed, 19 insertions(+), 10 deletions(-)
76f8c5
76f8c5
diff --git a/glib/poppler-attachment.cc b/glib/poppler-attachment.cc
76f8c5
index c6502e9d..11ba5bb5 100644
76f8c5
--- a/glib/poppler-attachment.cc
76f8c5
+++ b/glib/poppler-attachment.cc
76f8c5
@@ -111,20 +111,28 @@ _poppler_attachment_new (FileSpec *emb_file)
76f8c5
     attachment->description = _poppler_goo_string_to_utf8 (emb_file->getDescription ());
76f8c5
 
76f8c5
   embFile = emb_file->getEmbeddedFile();
76f8c5
-  attachment->size = embFile->size ();
76f8c5
+  if (embFile != NULL && embFile->isOk())
76f8c5
+    {
76f8c5
+      attachment->size = embFile->size ();
76f8c5
 
76f8c5
-  if (embFile->createDate ())
76f8c5
-    _poppler_convert_pdf_date_to_gtime (embFile->createDate (), (time_t *)&attachment->ctime);
76f8c5
-  if (embFile->modDate ())
76f8c5
-    _poppler_convert_pdf_date_to_gtime (embFile->modDate (), (time_t *)&attachment->mtime);
76f8c5
+      if (embFile->createDate ())
76f8c5
+        _poppler_convert_pdf_date_to_gtime (embFile->createDate (), (time_t *)&attachment->ctime);
76f8c5
+      if (embFile->modDate ())
76f8c5
+        _poppler_convert_pdf_date_to_gtime (embFile->modDate (), (time_t *)&attachment->mtime);
76f8c5
 
76f8c5
-  if (embFile->checksum () && embFile->checksum ()->getLength () > 0)
76f8c5
-    attachment->checksum = g_string_new_len (embFile->checksum ()->getCString (),
76f8c5
-                                             embFile->checksum ()->getLength ());
76f8c5
-  priv->obj_stream = new Object();
76f8c5
-  priv->obj_stream->initStream(embFile->stream());
76f8c5
-  // Copy the stream
76f8c5
-  embFile->stream()->incRef();
76f8c5
+      if (embFile->checksum () && embFile->checksum ()->getLength () > 0)
76f8c5
+        attachment->checksum = g_string_new_len (embFile->checksum ()->getCString (),
76f8c5
+                                                 embFile->checksum ()->getLength ());
76f8c5
+      priv->obj_stream = new Object();
76f8c5
+      priv->obj_stream->initStream(embFile->stream());
76f8c5
+      // Copy the stream
76f8c5
+      embFile->stream()->incRef();
76f8c5
+    }
76f8c5
+  else
76f8c5
+    {
76f8c5
+      g_warning ("Missing stream object for embedded file");
76f8c5
+      g_clear_object (&attachment);
76f8c5
+    }
76f8c5
 
76f8c5
   return attachment;
76f8c5
 }
76f8c5
diff --git a/glib/poppler-document.cc b/glib/poppler-document.cc
76f8c5
index b343eb90..df0aa47f 100644
76f8c5
--- a/glib/poppler-document.cc
76f8c5
+++ b/glib/poppler-document.cc
76f8c5
@@ -666,7 +666,8 @@ poppler_document_get_attachments (PopplerDocument *document)
76f8c5
       attachment = _poppler_attachment_new (emb_file);
76f8c5
       delete emb_file;
76f8c5
 
76f8c5
-      retval = g_list_prepend (retval, attachment);
76f8c5
+      if (attachment != NULL)
76f8c5
+        retval = g_list_prepend (retval, attachment);
76f8c5
     }
76f8c5
   return g_list_reverse (retval);
76f8c5
 }
76f8c5
-- 
76f8c5
2.17.0
76f8c5