Author: msahyoun
Date: Mon Feb 12 22:29:21 2018
New Revision: 1824081

URL: http://svn.apache.org/viewvc?rev=1824081&view=rev
Log:
PDFBOX-4107: avoid potential NPE; avoid potential ClassCastException

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1824081&r1=1824080&r2=1824081&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
 (original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
 Mon Feb 12 22:29:21 2018
@@ -711,8 +711,9 @@ public class PDFMergerUtility
             throws IOException
     {
         List<PDField> srcFields = srcAcroForm.getFields();
+        COSArray destFields = null;
 
-        if (srcFields != null)
+        if (srcFields != null && srcFields.size() > 0)
         {
             // if a form is merged multiple times using PDFBox the newly 
generated
             // fields starting with dummyFieldName may already exist. We need 
to determine the last unique 
@@ -729,7 +730,18 @@ public class PDFMergerUtility
                 }
             }
 
-            COSArray destFields = (COSArray) 
destAcroForm.getCOSObject().getItem(COSName.FIELDS);
+            // get the destinations root fields. Could be that the entry 
doesn't exist
+            // or is of wrong type
+            COSBase base = destAcroForm.getCOSObject().getItem(COSName.FIELDS);
+            if (base instanceof COSArray)
+            {
+                destFields = (COSArray) base;
+            }
+            else
+            {
+                destFields = new COSArray();
+            }
+            
             for (PDField srcField : srcAcroForm.getFields())
             {
                 COSDictionary dstField = (COSDictionary) 
cloner.cloneForNewDocument(srcField.getCOSObject());


Reply via email to