[poppler] poppler/PDFDoc.cc poppler/PDFDoc.h utils/pdfseparate.cc utils/pdfunite.cc

Albert Astals Cid aacid at kemper.freedesktop.org
Mon Nov 3 10:20:45 PST 2014


 poppler/PDFDoc.cc    |  102 ++++++++++++++++++++-------------------------------
 poppler/PDFDoc.h     |    2 -
 utils/pdfseparate.cc |    8 +++-
 utils/pdfunite.cc    |    4 +-
 4 files changed, 50 insertions(+), 66 deletions(-)

New commits:
commit 2fd0cd652cfc03c9af35a59192729e4ed26e8146
Author: Albert Astals Cid <aacid at kde.org>
Date:   Mon Nov 3 19:20:23 2014 +0100

    Fix memory leaks when running pdfseparate
    
    Bug #84768

diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc
index 8fd5e18..e88b8f6 100644
--- a/poppler/PDFDoc.cc
+++ b/poppler/PDFDoc.cc
@@ -637,7 +637,7 @@ int PDFDoc::savePageAs(GooString *name, int pageNo)
   replacePageDict(pageNo, 
     getCatalog()->getPage(pageNo)->getRotate(),
     getCatalog()->getPage(pageNo)->getMediaBox(),
-    cropBox, NULL);
+    cropBox);
   Ref *refPage = getCatalog()->getPageRef(pageNo);
   Object page;
   getXRef()->fetch(refPage->num, refPage->gen, &page);
@@ -649,8 +649,6 @@ int PDFDoc::savePageAs(GooString *name, int pageNo)
   outStr = new FileOutStream(f,0);
 
   yRef = new XRef(getXRef()->getTrailerDict());
-  Object encrypt;
-  getXRef()->getTrailerDict()->dictLookup("Encrypt", &encrypt);
 
   if (secHdlr != NULL && !secHdlr->isUnencrypted()) {
     yRef->setEncryption(secHdlr->getPermissionFlags(), 
@@ -777,6 +775,7 @@ int PDFDoc::savePageAs(GooString *name, int pageNo)
   fclose(f);
   delete yRef;
   delete countRef;
+  delete outStr;
 
   return errNone;
 }
@@ -1505,78 +1504,59 @@ void PDFDoc::markObject (Object* obj, XRef *xRef, XRef *countRef, Guint numOffse
 
 void PDFDoc::replacePageDict(int pageNo, int rotate,
                              PDFRectangle *mediaBox, 
-                             PDFRectangle *cropBox, Object *pageCTM)
+                             PDFRectangle *cropBox)
 {
   Ref *refPage = getCatalog()->getPageRef(pageNo);
   Object page;
   getXRef()->fetch(refPage->num, refPage->gen, &page);
   Dict *pageDict = page.getDict();
+  pageDict->remove("MediaBoxssdf");
   pageDict->remove("MediaBox");
   pageDict->remove("CropBox");
   pageDict->remove("ArtBox");
   pageDict->remove("BleedBox");
   pageDict->remove("TrimBox");
   pageDict->remove("Rotate");
-  Object *mediaBoxObj = new Object();
-  mediaBoxObj->initArray(getXRef());
-  Object *murx = new Object();
-  murx->initReal(mediaBox->x1);
-  Object *mury = new Object();
-  mury->initReal(mediaBox->y1);
-  Object *mllx = new Object();
-  mllx->initReal(mediaBox->x2);
-  Object *mlly = new Object();
-  mlly->initReal(mediaBox->y2);
-  mediaBoxObj->arrayAdd(murx);
-  mediaBoxObj->arrayAdd(mury);
-  mediaBoxObj->arrayAdd(mllx);
-  mediaBoxObj->arrayAdd(mlly);
-  pageDict->add(copyString("MediaBox"), mediaBoxObj);
+  Object mediaBoxObj;
+  mediaBoxObj.initArray(getXRef());
+  Object murx;
+  murx.initReal(mediaBox->x1);
+  Object mury;
+  mury.initReal(mediaBox->y1);
+  Object mllx;
+  mllx.initReal(mediaBox->x2);
+  Object mlly;
+  mlly.initReal(mediaBox->y2);
+  mediaBoxObj.arrayAdd(&murx);
+  mediaBoxObj.arrayAdd(&mury);
+  mediaBoxObj.arrayAdd(&mllx);
+  mediaBoxObj.arrayAdd(&mlly);
+  pageDict->add(copyString("MediaBox"), &mediaBoxObj);
   if (cropBox != NULL) {
-    Object *cropBoxObj = new Object();
-    cropBoxObj->initArray(getXRef());
-    Object *curx = new Object();
-    curx->initReal(cropBox->x1);
-    Object *cury = new Object();
-    cury->initReal(cropBox->y1);
-    Object *cllx = new Object();
-    cllx->initReal(cropBox->x2);
-    Object *clly = new Object();
-    clly->initReal(cropBox->y2);
-    cropBoxObj->arrayAdd(curx);
-    cropBoxObj->arrayAdd(cury);
-    cropBoxObj->arrayAdd(cllx);
-    cropBoxObj->arrayAdd(clly);
-    pageDict->add(copyString("CropBox"), cropBoxObj);
-    pageDict->add(copyString("TrimBox"), cropBoxObj);
+    Object cropBoxObj;
+    cropBoxObj.initArray(getXRef());
+    Object curx;
+    curx.initReal(cropBox->x1);
+    Object cury;
+    cury.initReal(cropBox->y1);
+    Object cllx;
+    cllx.initReal(cropBox->x2);
+    Object clly;
+    clly.initReal(cropBox->y2);
+    cropBoxObj.arrayAdd(&curx);
+    cropBoxObj.arrayAdd(&cury);
+    cropBoxObj.arrayAdd(&cllx);
+    cropBoxObj.arrayAdd(&clly);
+    pageDict->add(copyString("CropBox"), &cropBoxObj);
+    cropBoxObj.getArray()->incRef();
+    pageDict->add(copyString("TrimBox"), &cropBoxObj);
   } else {
-    pageDict->add(copyString("TrimBox"), mediaBoxObj);
-  }
-  Object *rotateObj = new Object();
-  rotateObj->initInt(rotate);
-  pageDict->add(copyString("Rotate"), rotateObj);
-  if (pageCTM != NULL) {
-    Object *contents = new Object();
-    Ref cmRef = getXRef()->addIndirectObject(pageCTM);
-    Object *ref = new Object();
-    ref->initRef(cmRef.num, cmRef.gen);
-    pageDict->lookupNF("Contents", contents);
-    Object *newContents = new Object();
-    newContents->initArray(getXRef());
-    if (contents->getType() == objRef) {
-      newContents->arrayAdd(ref);
-      newContents->arrayAdd(contents);
-    } else {
-      newContents->arrayAdd(ref);
-      for (int i = 0; i < contents->arrayGetLength(); i++) {
-        Object *contentEle = new Object();
-        contents->arrayGetNF(i, contentEle);
-        newContents->arrayAdd(contentEle);
-      }
-    }
-    pageDict->remove("Contents");
-    pageDict->add(copyString("Contents"), newContents);
+    mediaBoxObj.getArray()->incRef();
+    pageDict->add(copyString("TrimBox"), &mediaBoxObj);
   }
+  Object rotateObj;
+  rotateObj.initInt(rotate);
+  pageDict->add(copyString("Rotate"), &rotateObj);
   getXRef()->setModifiedObject(&page, *refPage);
   page.free();
 }
diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h
index a73953c..bb72476 100644
--- a/poppler/PDFDoc.h
+++ b/poppler/PDFDoc.h
@@ -247,7 +247,7 @@ public:
   void *getGUIData() { return guiData; }
 
   // rewrite pageDict with MediaBox, CropBox and new page CTM
-  void replacePageDict(int pageNo, int rotate, PDFRectangle *mediaBox, PDFRectangle *cropBox, Object *pageCTM);
+  void replacePageDict(int pageNo, int rotate, PDFRectangle *mediaBox, PDFRectangle *cropBox);
   void markPageObjects(Dict *pageDict, XRef *xRef, XRef *countRef, Guint numOffset, int oldRefNum, int newRefNum);
   GBool markAnnotations(Object *annots, XRef *xRef, XRef *countRef, Guint numOffset, int oldPageNum, int newPageNum);
   void markAcroForm(Object *acrpForm, XRef *xRef, XRef *countRef, Guint numOffset, int oldPageNum, int newPageNum);
diff --git a/utils/pdfseparate.cc b/utils/pdfseparate.cc
index 31dc18c..bc98c7d 100644
--- a/utils/pdfseparate.cc
+++ b/utils/pdfseparate.cc
@@ -54,6 +54,7 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
 
   if (!doc->isOk()) {
     error(errSyntaxError, -1, "Could not extract page(s) from damaged file ('{0:s}')", srcFileName);
+    delete doc;
     return false;
   }
 
@@ -74,6 +75,7 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
     error(errCommandLine, -1,
           "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d}).",
           firstPage, lastPage);
+    delete doc;
     return false;
   }
   bool foundmatch = false;
@@ -97,6 +99,7 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
   if (!foundmatch && firstPage != lastPage) {
     error(errSyntaxError, -1, "'{0:s}' must contain '%%d' if more than one page should be extracted", destFileName);
     free(auxDestFileName);
+    delete doc;
     return false;
   }
 
@@ -113,6 +116,7 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
   if (p != NULL) {
     error(errSyntaxError, -1, "'{0:s}' can only contain one '%d' pattern", destFileName);
     free(auxDestFileName);
+    delete doc;
     return false;
   }
   free(auxDestFileName);
@@ -123,12 +127,12 @@ bool extractPages (const char *srcFileName, const char *destFileName) {
     int errCode = doc->savePageAs(gpageName, pageNo);
     if ( errCode != errNone) {
       delete gpageName;
-      delete gfileName;
+      delete doc;
       return false;
     }
     delete gpageName;
   }
-  delete gfileName;
+  delete doc;
   return true;
 }
 
diff --git a/utils/pdfunite.cc b/utils/pdfunite.cc
index 787b1f0..05a0560 100644
--- a/utils/pdfunite.cc
+++ b/utils/pdfunite.cc
@@ -7,7 +7,7 @@
 // Copyright (C) 2011-2014 Thomas Freitag <Thomas.Freitag at alfa.de>
 // Copyright (C) 2012 Arseny Solokha <asolokha at gmx.com>
 // Copyright (C) 2012 Fabio D'Urso <fabiodurso at hotmail.it>
-// Copyright (C) 2012 Albert Astals Cid <aacid at kde.org>
+// Copyright (C) 2012, 2014 Albert Astals Cid <aacid at kde.org>
 // Copyright (C) 2013 Adrian Johnson <ajohnson at redneon.com>
 // Copyright (C) 2013 Hib Eris <hib at hiberis.nl>
 //
@@ -116,7 +116,7 @@ int main (int argc, char *argv[])
         cropBox = docs[i]->getCatalog()->getPage(j)->getCropBox();
       docs[i]->replacePageDict(j,
 	    docs[i]->getCatalog()->getPage(j)->getRotate(),
-	    docs[i]->getCatalog()->getPage(j)->getMediaBox(), cropBox, NULL);
+	    docs[i]->getCatalog()->getPage(j)->getMediaBox(), cropBox);
       Ref *refPage = docs[i]->getCatalog()->getPageRef(j);
       Object page;
       docs[i]->getXRef()->fetch(refPage->num, refPage->gen, &page);


More information about the poppler mailing list