Skip to content

Commit d227a49

Browse files
authored
Fix detection of invalid spaces in prolog (#20)
1 parent 27d6127 commit d227a49

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ limitations under the License.
7676
<version>5.9.2</version>
7777
<scope>test</scope>
7878
</dependency>
79+
<dependency>
80+
<groupId>org.hamcrest</groupId>
81+
<artifactId>hamcrest</artifactId>
82+
<version>2.2</version>
83+
<scope>test</scope>
84+
</dependency>
7985
</dependencies>
8086

8187
<build>

src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -2528,9 +2528,11 @@ private void parsePI() throws XmlPullParserException, IOException {
25282528
if ((buf[piTargetStart] == 'x' || buf[piTargetStart] == 'X')
25292529
&& (buf[piTargetStart + 1] == 'm' || buf[piTargetStart + 1] == 'M')
25302530
&& (buf[piTargetStart + 2] == 'l' || buf[piTargetStart + 2] == 'L')) {
2531-
if (piTargetStart > 3) { // <?xml is allowed as first characters in input ...
2531+
if (piTargetStart > 2) { // <?xml is allowed as first characters in input ...
25322532
throw new XmlPullParserException(
2533-
"processing instruction can not have PITarget with reserved xml name",
2533+
eventType == 0
2534+
? "XMLDecl is only allowed as first characters in input"
2535+
: "processing instruction can not have PITarget with reserved xml name",
25342536
this,
25352537
null);
25362538
} else {

src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
import org.codehaus.plexus.util.IOUtil;
3131
import org.codehaus.plexus.util.xml.XmlStreamReader;
3232
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.params.ParameterizedTest;
34+
import org.junit.jupiter.params.provider.ValueSource;
3335

36+
import static org.hamcrest.MatcherAssert.*;
37+
import static org.hamcrest.Matchers.*;
3438
import static org.junit.jupiter.api.Assertions.*;
3539

3640
/**
@@ -1473,4 +1477,22 @@ public void testProcessingInstructionTokenizeAfterXMLDeclAndBeforeFirstTag() thr
14731477
assertEquals(XmlPullParser.END_TAG, parser.nextToken());
14741478
assertEquals(XmlPullParser.END_DOCUMENT, parser.nextToken());
14751479
}
1480+
1481+
@ParameterizedTest
1482+
@ValueSource(strings = {" ", "\n", "\r", "\r\n", " ", "\n "})
1483+
void testBlankAtBeginning(String ws) throws XmlPullParserException, IOException {
1484+
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>nnn</test>";
1485+
1486+
MXParser parser = new MXParser();
1487+
parser.setInput(new StringReader(ws + xml));
1488+
assertThat(
1489+
assertThrows(XmlPullParserException.class, parser::next).getMessage(),
1490+
containsString("XMLDecl is only allowed as first characters in input"));
1491+
1492+
parser.setInput(new StringReader(ws + xml));
1493+
assertEquals(XmlPullParser.IGNORABLE_WHITESPACE, parser.nextToken());
1494+
assertThat(
1495+
assertThrows(XmlPullParserException.class, parser::nextToken).getMessage(),
1496+
containsString("processing instruction can not have PITarget with reserved xml name"));
1497+
}
14761498
}

0 commit comments

Comments
 (0)