Skip to content

Commit 8f4d337

Browse files
committed
PDFBOX-6132: avoid NPE, as suggested by Haoran Yan
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1931125 13f79535-47bb-0310-9956-ffa450edef68
1 parent 22d7dd3 commit 8f4d337

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,16 @@ public COSDictionary getLinearizedDictionary()
216216
for (COSObjectKey objectKey : objectKeys)
217217
{
218218
COSObject objectFromPool = getObjectFromPool(objectKey);
219-
COSBase realObject = objectFromPool.getObject();
220-
if (realObject instanceof COSDictionary)
219+
if (objectFromPool != null)
221220
{
222-
COSDictionary dic = (COSDictionary) realObject;
223-
if (dic.getItem(COSName.LINEARIZED) != null)
221+
COSBase realObject = objectFromPool.getObject();
222+
if (realObject instanceof COSDictionary)
224223
{
225-
return dic;
224+
COSDictionary dic = (COSDictionary) realObject;
225+
if (dic.getItem(COSName.LINEARIZED) != null)
226+
{
227+
return dic;
228+
}
226229
}
227230
}
228231
}
@@ -270,13 +273,16 @@ private List<COSObject> getObjectsByType(List<COSObjectKey> keys, COSName type1,
270273
for (COSObjectKey objectKey : keys)
271274
{
272275
COSObject objectFromPool = getObjectFromPool(objectKey);
273-
COSBase realObject = objectFromPool.getObject();
274-
if (realObject instanceof COSDictionary)
276+
if (objectFromPool != null)
275277
{
276-
COSName dictType = ((COSDictionary) realObject).getCOSName(COSName.TYPE);
277-
if (type1.equals(dictType) || (type2 != null && type2.equals(dictType)))
278+
COSBase realObject = objectFromPool.getObject();
279+
if (realObject instanceof COSDictionary)
278280
{
279-
retval.add(objectFromPool);
281+
COSName dictType = ((COSDictionary) realObject).getCOSName(COSName.TYPE);
282+
if (type1.equals(dictType) || (type2 != null && type2.equals(dictType)))
283+
{
284+
retval.add(objectFromPool);
285+
}
280286
}
281287
}
282288
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.pdfbox.cos;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import org.junit.jupiter.api.Test;
22+
23+
public class COSDocumentTest
24+
{
25+
@Test
26+
void testPDFBox6132()
27+
{
28+
COSDocument document = new COSDocument();
29+
Map<COSObjectKey, Long> xrefTable = new HashMap<>();
30+
xrefTable.put(null, 10L);
31+
document.addXRefTable(xrefTable);
32+
document.getObjectsByType(COSName.T);
33+
document.getLinearizedDictionary();
34+
}
35+
}

0 commit comments

Comments
 (0)