Skip to content

Commit 239e3ed

Browse files
committed
Fix OOM in parser
Add eof checks to: - MarkupParser.xEntityValue - MarkupParser.xComment - MarkupParser.systemLiteral - MarkupParser.pubidLiteral - MarkupParser.notationDecl - MarkupParserCommon.xAttributeValue - MarkupParserCommon.xTakeUntil
1 parent 6f76dd1 commit 239e3ed

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

jvm/src/test/scala/scala/xml/XMLTest.scala

+23-11
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,8 @@ class XMLTestJVM {
766766
assertEquals(x, XML.loadString(formatted))
767767
}
768768

769-
@UnitTest(expected = classOf[FatalError])
770-
def xTokenFailure {
769+
@UnitTest
770+
def xTokenTest {
771771
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("a"), false)
772772
assertEquals((): Unit, x.xToken('b'))
773773
}
@@ -786,27 +786,39 @@ class XMLTestJVM {
786786
x.xComment
787787
}
788788

789-
@UnitTest(expected = classOf[FatalError])
790-
def xmlProcInstrFailure {
789+
@UnitTest
790+
def xmlProcInstrTest {
791791
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("aa"), false)
792792

793793
assertEquals(new UnprefixedAttribute("aa", Text(""), Null), x.xmlProcInstr)
794794
}
795795

796-
@Ignore("Ignored for future fix, currently throw OOE because of infinity MarkupParserCommon:66")
797796
@UnitTest(expected = classOf[FatalError])
798-
def xAttributeValueFailure {
797+
def notationDeclFailure {
799798
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
800799

801-
x.xAttributeValue
800+
x.notationDecl
802801
}
803802

804-
@Ignore("Ignored for future fix, currently return unexpected result")
805-
@UnitTest(expected = classOf[FatalError])
806-
def xEntityValueFailure {
803+
@UnitTest
804+
def pubidLiteralTest {
805+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
806+
807+
assertEquals("", x.pubidLiteral)
808+
}
809+
810+
@UnitTest
811+
def xAttributeValueTest {
812+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("'"), false)
813+
814+
assertEquals("", x.xAttributeValue)
815+
}
816+
817+
@UnitTest
818+
def xEntityValueTest {
807819
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
808820

809-
x.xEntityValue
821+
assertEquals("", x.xEntityValue)
810822
}
811823

812824
}

shared/src/main/scala/scala/xml/parsing/MarkupParser.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ trait MarkupParser extends MarkupParserCommon with TokenTests {
397397
} else sb.append(ch)
398398
nextch()
399399
}
400-
throw truncatedError("broken comment")
400+
truncatedError("broken comment")
401401
}
402402

403403
/* todo: move this into the NodeBuilder class */
@@ -928,7 +928,7 @@ trait MarkupParser extends MarkupParserCommon with TokenTests {
928928
new PublicID(pubID, sysID)
929929
} else {
930930
reportSyntaxError("PUBLIC or SYSTEM expected")
931-
scala.sys.error("died parsing notationdecl")
931+
truncatedError("died parsing notationdecl")
932932
}
933933
xSpaceOpt()
934934
xToken('>')

shared/src/main/scala/scala/xml/parsing/MarkupParserCommon.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Utility.SU
1919
* All members should be accessed through those.
2020
*/
2121
private[scala] trait MarkupParserCommon extends TokenTests {
22-
protected def unreachable = scala.sys.error("Cannot be reached.")
22+
protected def unreachable = truncatedError("Cannot be reached.")
2323

2424
// type HandleType // MarkupHandler, SymbolicXMLBuilder
2525
type InputType // Source, CharArrayReader
@@ -62,7 +62,7 @@ private[scala] trait MarkupParserCommon extends TokenTests {
6262
val buf = new StringBuilder
6363
while (ch != endCh && !eof) {
6464
// well-formedness constraint
65-
if (ch == '<') return errorAndResult("'<' not allowed in attrib value", "")
65+
if (ch == '<') reportSyntaxError("'<' not allowed in attrib value")
6666
else if (ch == SU) truncatedError("")
6767
else buf append ch_returning_nextch
6868
}
@@ -241,11 +241,11 @@ private[scala] trait MarkupParserCommon extends TokenTests {
241241
val head = until.head
242242
val rest = until.tail
243243

244-
while (true) {
244+
while (!eof) {
245245
if (ch == head && peek(rest))
246246
return handler(positioner(), sb.toString)
247247
else if (ch == SU || eof)
248-
truncatedError("") // throws TruncatedXMLControl in compiler
248+
truncatedError(s"died parsing until $until") // throws TruncatedXMLControl in compiler
249249

250250
sb append ch
251251
nextch()

0 commit comments

Comments
 (0)