Skip to content
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

Fix NPE when adding illegal child to nameless PdfLayer #43

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions itext/src/main/java/com/itextpdf/text/pdf/PdfLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ String getTitle() {
* @param child the child layer
*/
public void addChild(PdfLayer child) {
if (child.parent != null)
throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.layer.1.already.has.a.parent", child.getAsString(PdfName.NAME).toUnicodeString()));
if (child.parent != null) {
String name = null;
PdfString pdfName = child.getAsString(PdfName.NAME);
if (pdfName != null)
name = pdfName.toUnicodeString();
throw new IllegalArgumentException(MessageLocalization.getComposedMessage("the.layer.1.already.has.a.parent", name));
}
child.parent = this;
if (children == null)
children = new ArrayList<PdfLayer>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ public void layerCheckTest2() throws IOException, DocumentException {
document.close();
}

@Test(expected = IllegalArgumentException.class)
public void layer_fixNPEOnAddChildThatAlreadyHasAParent() {
PdfLayer child = new PdfLayer("child");
new PdfLayer("parent").addChild(child);
new PdfLayer("new parent").addChild(child);
}

@Test
public void egsCheckTest1() throws DocumentException, IOException {
Document document = new Document();
Expand Down