Skip to content

Commit 56e862e

Browse files
belinguereshboutemy
authored andcommitted
#34 fixed NullPointerException in MXParser
Modified MXParser to throw an EOFException with a more informative message than the NPE. Added some tests.
1 parent 7e54bff commit 56e862e

File tree

2 files changed

+153
-19
lines changed

2 files changed

+153
-19
lines changed

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

+45-19
Original file line numberDiff line numberDiff line change
@@ -3711,31 +3711,57 @@ else if ( expand )
37113711
StringBuilder expectedTagStack = new StringBuilder();
37123712
if ( depth > 0 )
37133713
{
3714-
// final char[] cbuf = elRawName[depth];
3715-
// final String startname = new String(cbuf, 0, elRawNameEnd[depth]);
3716-
expectedTagStack.append( " - expected end tag" );
3717-
if ( depth > 1 )
3714+
if ( elRawName == null || elRawName[depth] == null )
37183715
{
3719-
expectedTagStack.append( "s" ); // more than one end tag
3716+
String tagName = new String( buf, posStart + 1, pos - posStart - 1 );
3717+
expectedTagStack.append( " - expected the opening tag <" ).append( tagName ).append( "...>" );
37203718
}
3721-
expectedTagStack.append( " " );
3722-
for ( int i = depth; i > 0; i-- )
3723-
{
3724-
String tagName = new String( elRawName[i], 0, elRawNameEnd[i] );
3725-
expectedTagStack.append( "</" ).append( tagName ).append( '>' );
3726-
}
3727-
expectedTagStack.append( " to close" );
3728-
for ( int i = depth; i > 0; i-- )
3719+
else
37293720
{
3730-
if ( i != depth )
3721+
// final char[] cbuf = elRawName[depth];
3722+
// final String startname = new String(cbuf, 0, elRawNameEnd[depth]);
3723+
expectedTagStack.append( " - expected end tag" );
3724+
if ( depth > 1 )
3725+
{
3726+
expectedTagStack.append( "s" ); // more than one end tag
3727+
}
3728+
expectedTagStack.append( " " );
3729+
3730+
for ( int i = depth; i > 0; i-- )
3731+
{
3732+
if ( elRawName == null || elRawName[i] == null )
3733+
{
3734+
String tagName = new String( buf, posStart + 1, pos - posStart - 1 );
3735+
expectedTagStack.append( " - expected the opening tag <" ).append( tagName ).append( "...>" );
3736+
}
3737+
else
3738+
{
3739+
String tagName = new String( elRawName[i], 0, elRawNameEnd[i] );
3740+
expectedTagStack.append( "</" ).append( tagName ).append( '>' );
3741+
}
3742+
}
3743+
expectedTagStack.append( " to close" );
3744+
for ( int i = depth; i > 0; i-- )
37313745
{
3732-
expectedTagStack.append( " and" ); // more than one end tag
3746+
if ( i != depth )
3747+
{
3748+
expectedTagStack.append( " and" ); // more than one end tag
3749+
}
3750+
if ( elRawName == null || elRawName[i] == null )
3751+
{
3752+
String tagName = new String( buf, posStart + 1, pos - posStart - 1 );
3753+
expectedTagStack.append( " start tag <" ).append( tagName ).append( ">" );
3754+
expectedTagStack.append( " from line " ).append( elRawNameLine[i] );
3755+
}
3756+
else
3757+
{
3758+
String tagName = new String( elRawName[i], 0, elRawNameEnd[i] );
3759+
expectedTagStack.append( " start tag <" ).append( tagName ).append( ">" );
3760+
expectedTagStack.append( " from line " ).append( elRawNameLine[i] );
3761+
}
37333762
}
3734-
String tagName = new String( elRawName[i], 0, elRawNameEnd[i] );
3735-
expectedTagStack.append( " start tag <" ).append( tagName ).append( ">" );
3736-
expectedTagStack.append( " from line " ).append( elRawNameLine[i] );
3763+
expectedTagStack.append( ", parser stopped on" );
37373764
}
3738-
expectedTagStack.append( ", parser stopped on" );
37393765
}
37403766
throw new EOFException( "no more data available" + expectedTagStack.toString()
37413767
+ getPositionDescription() );

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

+108
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.junit.Assert.assertTrue;
2121
import static org.junit.Assert.fail;
2222

23+
import java.io.EOFException;
2324
import java.io.IOException;
2425
import java.io.StringReader;
2526

@@ -496,4 +497,111 @@ public void testSubsequentMalformedProcessingInstructionNoClosingQuestionMark()
496497
}
497498
}
498499

500+
public void testMalformedXMLRootElement()
501+
throws Exception
502+
{
503+
String input = "<Y";
504+
505+
MXParser parser = new MXParser();
506+
parser.setInput( new StringReader( input ) );
507+
508+
try
509+
{
510+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
511+
512+
fail( "Should throw EOFException" );
513+
}
514+
catch ( EOFException e )
515+
{
516+
assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <Y...>" ) );
517+
}
518+
}
519+
520+
public void testMalformedXMLRootElement2()
521+
throws Exception
522+
{
523+
String input = "<hello";
524+
525+
MXParser parser = new MXParser();
526+
parser.setInput( new StringReader( input ) );
527+
528+
try
529+
{
530+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
531+
532+
fail( "Should throw EOFException" );
533+
}
534+
catch ( EOFException e )
535+
{
536+
assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <hello...>" ) );
537+
}
538+
}
539+
540+
public void testMalformedXMLRootElement3()
541+
throws Exception
542+
{
543+
String input = "<hello><how";
544+
545+
MXParser parser = new MXParser();
546+
parser.setInput( new StringReader( input ) );
547+
548+
try
549+
{
550+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
551+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
552+
553+
fail( "Should throw EOFException" );
554+
}
555+
catch ( EOFException e )
556+
{
557+
assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <how...>" ) );
558+
}
559+
}
560+
561+
public void testMalformedXMLRootElement4()
562+
throws Exception
563+
{
564+
String input = "<hello>some text<how";
565+
566+
MXParser parser = new MXParser();
567+
parser.setInput( new StringReader( input ) );
568+
569+
try
570+
{
571+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
572+
assertEquals( XmlPullParser.TEXT, parser.nextToken() );
573+
assertEquals( "some text", parser.getText() );
574+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
575+
576+
fail( "Should throw EOFException" );
577+
}
578+
catch ( EOFException e )
579+
{
580+
assertTrue( e.getMessage().contains( "no more data available - expected the opening tag <how...>" ) );
581+
}
582+
}
583+
584+
public void testMalformedXMLRootElement5()
585+
throws Exception
586+
{
587+
String input = "<hello>some text</hello";
588+
589+
MXParser parser = new MXParser();
590+
parser.setInput( new StringReader( input ) );
591+
592+
try
593+
{
594+
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
595+
assertEquals( XmlPullParser.TEXT, parser.nextToken() );
596+
assertEquals( "some text", parser.getText() );
597+
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
598+
599+
fail( "Should throw EOFException" );
600+
}
601+
catch ( EOFException e )
602+
{
603+
assertTrue( e.getMessage().contains( "no more data available - expected end tag </hello> to close start tag <hello>" ) );
604+
}
605+
}
606+
499607
}

0 commit comments

Comments
 (0)