Skip to content

SI-4520 - reasonable exception instead of OOE in XML parser #32

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/scala/scala/xml/parsing/MarkupParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package scala
package xml
package parsing

import java.io.{ByteArrayOutputStream, PrintStream}

import scala.io.Source
import scala.xml.dtd._
import Utility.Escapes.{ pairs => unescape }
Expand Down Expand Up @@ -936,7 +938,12 @@ trait MarkupParser extends MarkupParserCommon with TokenTests {
handle.notationDecl(notat, extID)
}

def reportSyntaxError(pos: Int, str: String) { curInput.reportError(pos, str) }
def reportSyntaxError(pos: Int, str: String) {
val msg = new ByteArrayOutputStream()
curInput.reportError(pos, str, new PrintStream(msg))
throw FatalError(msg.toString("UTF-8"))
}

def reportSyntaxError(str: String) { reportSyntaxError(pos, str) }
def reportValidationError(pos: Int, str: String) { reportSyntaxError(pos, str) }

Expand Down
53 changes: 49 additions & 4 deletions src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,16 @@ Ours is the portal of hope, come as you are."
try {
xml.parsing.ConstructingParser.fromSource(io.Source.fromString("<foo>"), false).document()
} catch {
case e: Exception => println(e.getMessage)
case e: Exception =>
e.printStackTrace()
println(e.getMessage)
}
}
}
out.flush()
assertEquals(
""":1:5: '/' expected instead of '' ^
:1:5: name expected, but char '' cannot start a name ^
expected closing tag of foo

""", out.toString)
}

Expand Down Expand Up @@ -815,7 +816,7 @@ expected closing tag of foo
import scala.xml.parsing._
@UnitTest
def dontLoop: Unit = {
val xml = "<!DOCTYPE xmeml SYSTEM> <xmeml> <sequence> </sequence> </xmeml> "
val xml = "<!DOCTYPE xmeml SYSTEM 'uri'> <xmeml> <sequence> </sequence> </xmeml> "
val sink = new PrintStream(new ByteArrayOutputStream())
(Console withOut sink) {
(Console withErr sink) {
Expand All @@ -824,4 +825,48 @@ expected closing tag of foo
}
}

@UnitTest(expected = classOf[FatalError])
def shouldThrowFatalErrorWhenCantFindRequestedXToken {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("<a/>"), false)

x.xToken('b')
}

@UnitTest(expected = classOf[FatalError])
def shouldThrowFatalErrorWhenCantFindRequestedXCharData {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("<a/>"), false)

x.xCharData
}

@UnitTest(expected = classOf[FatalError])
def shouldThrowFatalErrorWhenCantFindRequestedXComment {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("<a/>"), false)

x.xComment
}

@UnitTest(expected = classOf[FatalError])
def shouldThrowFatalErrorWhenCantFindRequestedXmlProcInstr {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("<a/>"), false)

x.xmlProcInstr()
}

@Ignore("Ignored for future fix, currently throw OOE because of infinity MarkupParserCommon:66")
@UnitTest(expected = classOf[FatalError])
def shouldThrowFatalErrorWhenCantFindRequestedXAttributeValue {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("<a/>"), false)

x.xAttributeValue()
}

@Ignore("Ignored for future fix, currently return unexpected result")
@UnitTest(expected = classOf[FatalError])
def shouldThrowFatalErrorWhenCantFindRequestedXEntityValue {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("<a/>"), false)

assertEquals("a/>", x.xEntityValue())
}

}