File 0031-Fix-clamping-avoiding-crashes-in-PdfPagesTree-InsertPage.patch of Package podofo

Subject: Patch by Francesco Pretto: Fix clamping (avoiding crashes) in PdfPagesTree::InsertPage(const PdfRect&, int)
Url: https://sourceforge.net/p/podofo/code/1898/

This patch is from the podofo-users mailing list post specified below and fixes
a crash when called with int index 0, in PdfPagesTreeCache::AddPageObject(), by
array out-of-bounds access. The new clamping allows an index equal to number of
pages but will clamp negative input indices to 0 instead of the page count - 1,
positive input indices to the page count (meaning "insert after the last page")
which avoids the call to the latter method with a negative index (crash cause).

Metadata of the patch-source mailing list post:
Date: Fri, 23 Feb 2018 19:27:40 +0100
Message-ID: <CALas-igwWbEOr7dW1=t0c6N20Y4hNuD13seSPgaVB_0KUJ+zQg@mail.gmail.com>
Subject prefix: [PATCH 05/13],
further Subject: PdfPagesTree::InsertPage: Fix "atIndex" is really a 0-based index

--- a/podofo/trunk/src/doc/PdfPagesTree.cpp
+++ b/podofo/trunk/src/doc/PdfPagesTree.cpp
@@ -247,11 +247,13 @@
 {
     PdfPage* pPage = new PdfPage( rSize, GetRoot()->GetOwner() );
 
-	 if (atIndex < 0 || atIndex >= this->GetTotalNumberOfPages()) {
-		 atIndex = this->GetTotalNumberOfPages() - 1;
-	 }
-
-	 InsertPage( atIndex - 1, pPage );
+    int pageCount;
+    if ( atIndex < 0 )
+        atIndex = 0;
+    else if ( atIndex > ( pageCount = this->GetTotalNumberOfPages() ) )
+        atIndex = pageCount;
+
+    InsertPage( atIndex - 1, pPage );
     m_cache.AddPageObject( atIndex, pPage );
 
     return pPage;
openSUSE Build Service is sponsored by