Author: tilman
Date: Mon Jan  5 14:28:24 2026
New Revision: 1931125

Log:
PDFBOX-6132: avoid NPE, as suggested by Haoran Yan

Added:
   pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/COSDocumentTest.java 
  (contents, props changed)
Modified:
   pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java    
Mon Jan  5 14:28:20 2026        (r1931124)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java    
Mon Jan  5 14:28:24 2026        (r1931125)
@@ -216,13 +216,16 @@ public class COSDocument extends COSBase
         for (COSObjectKey objectKey : objectKeys)
         {
             COSObject objectFromPool = getObjectFromPool(objectKey);
-            COSBase realObject = objectFromPool.getObject();
-            if (realObject instanceof COSDictionary)
+            if (objectFromPool != null)
             {
-                COSDictionary dic = (COSDictionary) realObject;
-                if (dic.getItem(COSName.LINEARIZED) != null)
+                COSBase realObject = objectFromPool.getObject();
+                if (realObject instanceof COSDictionary)
                 {
-                    return dic;
+                    COSDictionary dic = (COSDictionary) realObject;
+                    if (dic.getItem(COSName.LINEARIZED) != null)
+                    {
+                        return dic;
+                    }
                 }
             }
         }
@@ -270,13 +273,16 @@ public class COSDocument extends COSBase
         for (COSObjectKey objectKey : keys)
         {
             COSObject objectFromPool = getObjectFromPool(objectKey);
-            COSBase realObject = objectFromPool.getObject();
-            if (realObject instanceof COSDictionary)
+            if (objectFromPool != null)
             {
-                COSName dictType = ((COSDictionary) 
realObject).getCOSName(COSName.TYPE);
-                if (type1.equals(dictType) || (type2 != null && 
type2.equals(dictType)))
+                COSBase realObject = objectFromPool.getObject();
+                if (realObject instanceof COSDictionary)
                 {
-                    retval.add(objectFromPool);
+                    COSName dictType = ((COSDictionary) 
realObject).getCOSName(COSName.TYPE);
+                    if (type1.equals(dictType) || (type2 != null && 
type2.equals(dictType)))
+                    {
+                        retval.add(objectFromPool);
+                    }
                 }
             }
         }

Added: 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/COSDocumentTest.java
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ 
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/cos/COSDocumentTest.java    
    Mon Jan  5 14:28:24 2026        (r1931125)
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.cos;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.jupiter.api.Test;
+
+public class COSDocumentTest
+{
+    @Test
+    void testPDFBox6132()
+    {
+        COSDocument document = new COSDocument();
+        Map<COSObjectKey, Long> xrefTable = new HashMap<>();
+        xrefTable.put(null, 10L);
+        document.addXRefTable(xrefTable);
+        document.getObjectsByType(COSName.T);
+        document.getLinearizedDictionary();
+    }
+}

Reply via email to