Skip to content

Commit cc4a06b

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 4b6dcf5 commit cc4a06b

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

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

+16-8
Original file line numberDiff line numberDiff line change
@@ -659,27 +659,35 @@ class XMLTestJVM {
659659
x.xComment
660660
}
661661

662-
@UnitTest(expected = classOf[FatalError])
663662
def xmlProcInstrFailure {
664663
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("aa"), false)
665664

666-
x.xmlProcInstr
665+
assertEquals(scala.xml.Null, x.xmlProcInstr)
667666
}
668667

669-
@Ignore("Ignored for future fix, currently throw OOE because of infinity MarkupParserCommon:66")
670668
@UnitTest(expected = classOf[FatalError])
671-
def xAttributeValueFailure {
669+
def notationDeclFailure {
672670
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
673671

674-
x.xAttributeValue
672+
x.notationDecl
673+
}
674+
675+
def pubidLiteralFailure {
676+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
677+
678+
assertEquals("", x.pubidLiteral)
679+
}
680+
681+
def xAttributeValueFailure {
682+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("'"), false)
683+
684+
assertEquals("", x.xAttributeValue)
675685
}
676686

677-
@Ignore("Ignored for future fix, currently return unexpected result")
678-
@UnitTest(expected = classOf[FatalError])
679687
def xEntityValueFailure {
680688
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
681689

682-
x.xEntityValue
690+
assertEquals("", x.xEntityValue)
683691
}
684692

685693
}

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
@@ -18,7 +18,7 @@ import Utility.SU
1818
* All members should be accessed through those.
1919
*/
2020
private[scala] trait MarkupParserCommon extends TokenTests {
21-
protected def unreachable = scala.sys.error("Cannot be reached.")
21+
protected def unreachable = truncatedError("Cannot be reached.")
2222

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

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

249249
sb append ch
250250
nextch()

0 commit comments

Comments
 (0)