Hi,

I may find a potential NullPointerException (NPE) in COSDocument. Specifically, 
when a cross-reference table (xrefTable) containing a null key is added via 
addXRefTable(), calling getLinearizedDictionary() results in an unhandled NPE 
at line 219 of COSDocument.java.



Reproduce:
Run the following test case:
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.cos.COSObjectKey;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.Map;
​
public class TestClass {
   @Test
   public void test() {
       COSDocument document = new COSDocument();

       Map<COSObjectKey, Long> xrefTable = new HashMap<>();
       xrefTable.put(null, 10L);
       document.addXRefTable(xrefTable);

       document.getLinearizedDictionary();
  }
}

Observed Behavior:
The test fails with the following exception:
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TestClass
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.477 s 
<<< FAILURE! -- in TestClass
[ERROR] TestClass.test -- Time elapsed: 0.442 s <<< ERROR!
java.lang.NullPointerException: Cannot invoke 
"org.apache.pdfbox.cos.COSObject.getObject()" because "objectFromPool" is null
      at 
org.apache.pdfbox.cos.COSDocument.getLinearizedDictionary(COSDocument.java:219)
      at TestClass.test(TestClass.java:17)
      at java.base/java.lang.reflect.Method.invoke(Method.java:569)
      at 
java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
      at 
java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
      at 
java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
      at 
java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
      at 
java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
​
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   TestClass.test:17 ? NullPointer Cannot invoke 
"org.apache.pdfbox.cos.COSObject.getObject()" because "objectFromPool" is null
[INFO]
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

Environment:

  *   PDFBox version: latest (3.0.6)
Apache Maven 3.9.12 (848fbb4bf2d427b72bdb2471c22fced7ebd9a7a1)
Maven home: /maven/apache-maven-3.9.12
Java version: 17.0.17, vendor: Debian, runtime: 
/usr/lib/jvm/java-17-openjdk-amd64
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "5.15.0-138-generic", arch: "amd64", family: "unix"


Reply via email to