On Thu, 2016-09-15 at 16:07 +0000, Thomas Szendefi wrote:
> I have that code running with 0.9.4, but it prints the following
> error message on the console for almost every PDF I try:
> PoDoFo encounter an error. Error: 5 ePdfError_UnexpectedEOF
>         Error Description: End of file was reached unxexpectedly.
>         Callstack:
>         #0 Error Source: /proj/sc5/src/mu/components/libs/podofo-
> 0.9.4/src/base/PdfParserObject.cpp:199
>                 Information: Expected variant.
>  
> (Thrown in the very beginning inside PdfSigIncMemDocument::Write())

        Hi,
there is some issue in the code which can lead to crash for certain PDF
files. I didn't investigate it into more deep, I only managed to write
a workaround to not cause the crash and to not throw the above
exception.

You can find the workaround attached. I hesitate to commit it to PoDoFo
sources, but maybe I'll just do it for the time being.

As I understand the issue, the incSign document stole the page to its
content fromt he underlying memDocument, which results into the delayed
load to use wrong owner, thus fails to load. As the page is in both
documents a double-free happens when both incSign and the memDocument
are freed after the signing.

I've even written a patch to extend the incremental signing drawing, to
be more generic, but it changes API.

The only correct way to fix this is to write complete incremental
editing for PoDoFo, but that's quite out of my hands at the moment. As
I wrote above, maybe I'll commit both changes for the time being, thus
the PoDoFo will be usable, until someone brave will implement the full
incremental editing.
        Bye,
        zyx

P.S.: with the attached patch I can sign your simple.pdf

-- 
http://www.litePDF.cz                                 i...@litepdf.cz
Index: src/doc/PdfSigIncMemDocument.cpp
===================================================================
--- src/doc/PdfSigIncMemDocument.cpp	(revision 1771)
+++ src/doc/PdfSigIncMemDocument.cpp	(working copy)
@@ -127,6 +127,7 @@ PdfSigIncMemDocument::PdfSigIncMemDocume
    m_n2XObj = NULL;
    m_pFont = NULL;
    m_bLinearized = false;
+   m_stolenPage = NULL;
 }
 
 PdfSigIncMemDocument::PdfSigIncMemDocument(const char* pszInpFilename)
@@ -140,6 +141,7 @@ PdfSigIncMemDocument::PdfSigIncMemDocume
    m_n2XObj = NULL;
    m_pFont = NULL;
    m_bLinearized = false;
+   m_stolenPage = NULL;
 
    m_Document->Load(pszInpFilename);
 }
@@ -152,6 +154,11 @@ PdfSigIncMemDocument::~PdfSigIncMemDocum
          m_pAcroForms = NULL;
    }
 
+   if (m_stolenPage && m_Document && m_stolenPage->GetOwner()) {
+       m_stolenPage->GetOwner()->RemoveObject(m_stolenPage->Reference());
+   }
+   m_stolenPage = NULL;
+
    if(m_InpFilename)
       podofo_free( m_InpFilename );
    if(m_pSignField)
@@ -188,6 +195,16 @@ void PdfSigIncMemDocument::Initialize()
       delete m_Document;
       m_Document = new PdfExMemDocument(inpDvc);
    }
+
+   /* Preload whole file into the memory */
+   TIVecObjects it, end = m_Document->GetObjects().end();
+   for (it = m_Document->GetObjects().begin(); it != end; it++) {
+      PdfObject *obj = *it;
+
+      if (obj)
+         obj->DelayedStreamLoad();
+   }
+
    CreateVisualSignRect();
    m_LastXRefOffset = m_Document->GetXRefOffset();
    
@@ -481,6 +498,9 @@ bool PdfSigIncMemDocument::AddPageToIncD
       if(!obj) {
          this->GetObjects().push_back(objPage);
          this->GetObjects().Sort();
+         if (objPage->GetOwner()) {
+             m_stolenPage = objPage;
+         }
       }
    } else
       bResult = false;
Index: src/doc/PdfSigIncMemDocument.h
===================================================================
--- src/doc/PdfSigIncMemDocument.h	(revision 1771)
+++ src/doc/PdfSigIncMemDocument.h	(working copy)
@@ -89,6 +89,7 @@ class PODOFO_DOC_API PdfSigIncMemDocumen
     PdfSigIncSignatureField *m_pSignField;
 
     bool m_bLinearized;
+    PdfObject *m_stolenPage;
        
 protected:
    void CreateAnnotation(PdfSignOutputDevice* pDevice, PdfPage* pPage);
------------------------------------------------------------------------------
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to