Skip to content

NPE caused by undefined object #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -1084,10 +1089,17 @@ private COSObjectKey getObjectKey( COSBase obj )
return key;
}
}
// check is null avoids objectKeys.computeIfAbsent NPE
if ( actual == null )
{
return null;
}
else
{
actual = obj;
}
// PDFBOX-4540: because objectKeys is accessible from outside, it is possible
// that a COSObject obj is already in the objectKeys map.
COSObjectKey actualKey = objectKeys.computeIfAbsent(actual,
k -> new COSObjectKey(++number, 0));
// check if the returned key and the origin key of the given object are the same
Expand Down Expand Up @@ -1382,6 +1394,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));
Expand Down