From f7e8816b8c9e28a71de6326ab54b4148e915e0b8 Mon Sep 17 00:00:00 2001 From: Quan guanyu Date: Thu, 1 Feb 2024 17:33:39 +0800 Subject: [PATCH] fix obj ref be used but obj not define in doc --- .../org/apache/pdfbox/pdfwriter/COSWriter.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java b/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java index d7d70315860..1b88f5d51bf 100644 --- a/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java +++ b/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java @@ -636,9 +636,14 @@ private boolean isNeedToBeUpdated(COSBase base) */ public void doWriteObject( COSBase obj ) throws IOException { - writtenObjects.add( obj ); // find the physical reference - currentObjectKey = getObjectKey( obj ); + COSObjectKey currentObj = getObjectKey( obj ); + if ( currentObj == null ) + { + return; + } + currentObjectKey = currentObj; + writtenObjects.add( obj ); doWriteObject(currentObjectKey, obj); } @@ -1093,6 +1098,11 @@ private COSObjectKey getObjectKey( COSBase obj ) } } } + // check is null avoids objectKeys.computeIfAbsent NPE + if ( actual == null ) + { + return null; + } // 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, @@ -1382,6 +1392,10 @@ public void visitFromNull(COSNull obj) throws IOException public void writeReference(COSBase obj) throws IOException { COSObjectKey key = getObjectKey(obj); + if ( key == null ) + { + return; + } getStandardOutput().write(String.valueOf(key.getNumber()).getBytes(StandardCharsets.ISO_8859_1)); getStandardOutput().write(SPACE); getStandardOutput().write(String.valueOf(key.getGeneration()).getBytes(StandardCharsets.ISO_8859_1));