Skip to content

Commit

Permalink
prevent java.lang.StringIndexOutOfBoundsException
Browse files Browse the repository at this point in the history
when text starts with `<![CDATA[` opening but does not end with `]]>`.
This also allows to add `<![CDATA[` as text content.

fixes #133

Signed-off-by: Ronny Perinke <23166289+sephiroth-j@users.noreply.github.com>
  • Loading branch information
sephiroth-j authored and lukasj committed May 11, 2023
1 parent 0e2b0e9 commit 550ca17
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -20,6 +20,7 @@ public class CDATAImpl extends TextImpl<CDATASection> implements CDATASection {

static final String cdataUC = "<![CDATA[";
static final String cdataLC = "<![cdata[";
static final String CDATA_CLOSE = "]]>";

public CDATAImpl(SOAPDocumentImpl ownerDoc, String text) {
super(ownerDoc, text);
Expand Down
Expand Up @@ -677,10 +677,11 @@ protected SOAPElement findAndConvertChildElement(NameImpl name) {

@Override
public SOAPElement addTextNode(String text) throws SOAPException {
if (text.startsWith(CDATAImpl.cdataUC)
|| text.startsWith(CDATAImpl.cdataLC))
if ((text.startsWith(CDATAImpl.cdataUC) || text.startsWith(CDATAImpl.cdataLC))
&& text.endsWith(CDATAImpl.CDATA_CLOSE))
return addCDATA(
text.substring(CDATAImpl.cdataUC.length(), text.length() - 3));
text.substring(CDATAImpl.cdataUC.length(),
text.length() - CDATAImpl.CDATA_CLOSE.length()));
return addText(text);
}

Expand Down
18 changes: 17 additions & 1 deletion saaj-ri/src/test/java/element/ElementTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -163,6 +163,22 @@ public void testAddTextNode() throws Exception {
String returnedText = element.getValue();

assertEquals(originalText, returnedText);

element.removeContents();
element.addTextNode("<![CDATA[");
assertEquals("<![CDATA[", element.getValue());

element.removeContents();
element.addTextNode("]]>");
assertEquals("]]>", element.getValue());

element.removeContents();
element.addTextNode("<![cdata[]]>");
assertNull(element.getValue());

element.removeContents();
element.addTextNode("<![cdata[test]]>");
assertEquals("test", element.getValue());
}

public void testAddChildElement() throws Exception {
Expand Down

0 comments on commit 550ca17

Please sign in to comment.