Author: msahyoun Date: Mon Feb 12 22:35:05 2018 New Revision: 1824082 URL: http://svn.apache.org/viewvc?rev=1824082&view=rev Log: PDFBOX-4107: avoid potential NPE; avoid potential ClassCastException
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1824082&r1=1824081&r2=1824082&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java (original) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Mon Feb 12 22:35:05 2018 @@ -708,8 +708,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 @@ -725,8 +726,19 @@ public class PDFMergerUtility nextFieldNum = Math.max(nextFieldNum, Integer.parseInt(fieldName.substring(prefixLength, fieldName.length())) + 1); } } - - 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());