You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2024/02/12 07:17:44 UTC

svn commit: r1915733 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java

Author: lehmi
Date: Mon Feb 12 07:17:44 2024
New Revision: 1915733

URL: http://svn.apache.org/viewvc?rev=1915733&view=rev
Log:
PDFBOX-5714: fix key replacement, avoid NPE

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java?rev=1915733&r1=1915732&r2=1915733&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Mon Feb 12 07:17:44 2024
@@ -1071,39 +1071,41 @@ public class COSWriter implements ICOSVi
      */
     private COSObjectKey getObjectKey( COSBase obj )
     {
-        COSBase actual = obj;
-        if( actual instanceof COSObject )
+        COSObjectKey key = obj.getKey();
+        COSBase actual;
+        if (obj instanceof COSObject)
         {
             actual = ((COSObject) obj).getObject();
-            if (reuseObjectNumbers)
+            if (actual == null)
             {
-                COSObjectKey key = obj.getKey();
-                if (key != null)
-                {
-                    COSObjectKey actualKey = actual != null ? actual.getKey() : null;
-                    // check if the key of the referenced object and the indirect object is the same
-                    if (actualKey != null && !key.equals(actualKey))
-                    {
-                        // update the object key of the indirect object
-                        key = actualKey;
-                        obj.setKey(key);
-                    }
-                    objectKeys.put(obj, key);
-                    return key;
-                }
+                objectKeys.put(obj, key);
+                return key;
             }
         }
-        // PDFBOX-4540: because objectKeys is accessible from outside, it is possible
-        // that a COSObject obj is already in the objectKeys map.
-        COSObjectKey newKey = objectKeys.computeIfAbsent(actual,
+        else
+        {
+            actual = obj;
+        }
+        COSObjectKey actualKey = objectKeys.computeIfAbsent(actual,
                 k -> new COSObjectKey(++number, 0));
-        COSObjectKey actualKey = actual.getKey();
-        // if the returned key is new update the existing key of the given object
-        if (actualKey != null && !actualKey.equals(newKey))
+        // check if the returned key and the origin key of the given object are the same
+        if (key == null || (actualKey != null && !key.equals(actualKey)))
         {
-            actual.setKey(newKey);
+            if (key != null)
+                System.out.println("key fixed: " + key + " -> " + actualKey);
+            else
+                System.out.println("key initialized: " + actualKey);
+            // update the object key given object/referenced object
+            key = actualKey;
+            actual.setKey(actualKey);
+            if (obj instanceof COSObject)
+            {
+                // update the object key of the indirect object
+                obj.setKey(key);
+                objectKeys.put(obj, key);
+            }
         }
-        return newKey;
+        return key;
     }
 
     @Override