Skip to content

Commit 54a439c

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 599bd30 commit 54a439c

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,27 +786,35 @@ class XMLTestJVM {
786786
x.xComment
787787
}
788788

789-
@UnitTest(expected = classOf[FatalError])
790789
def xmlProcInstrFailure {
791790
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("aa"), false)
792791

793-
x.xmlProcInstr
792+
assertEquals(scala.xml.Null, x.xmlProcInstr)
794793
}
795794

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

801-
x.xAttributeValue
799+
x.notationDecl
800+
}
801+
802+
def pubidLiteralFailure {
803+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
804+
805+
assertEquals("", x.pubidLiteral)
806+
}
807+
808+
def xAttributeValueFailure {
809+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("'"), false)
810+
811+
assertEquals("", x.xAttributeValue)
802812
}
803813

804-
@Ignore("Ignored for future fix, currently return unexpected result")
805-
@UnitTest(expected = classOf[FatalError])
806814
def xEntityValueFailure {
807815
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
808816

809-
x.xEntityValue
817+
assertEquals("", x.xEntityValue)
810818
}
811819

812820
}

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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)