Skip to content

Commit d1d1280

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 0c8158e commit d1d1280

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
@@ -769,27 +769,35 @@ class XMLTestJVM {
769769
x.xComment
770770
}
771771

772-
@UnitTest(expected = classOf[FatalError])
773772
def xmlProcInstrFailure {
774773
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("aa"), false)
775774

776-
x.xmlProcInstr
775+
assertEquals(scala.xml.Null, x.xmlProcInstr)
777776
}
778777

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

784-
x.xAttributeValue
782+
x.notationDecl
783+
}
784+
785+
def pubidLiteralFailure {
786+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
787+
788+
assertEquals("", x.pubidLiteral)
789+
}
790+
791+
def xAttributeValueFailure {
792+
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("'"), false)
793+
794+
assertEquals("", x.xAttributeValue)
785795
}
786796

787-
@Ignore("Ignored for future fix, currently return unexpected result")
788-
@UnitTest(expected = classOf[FatalError])
789797
def xEntityValueFailure {
790798
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)
791799

792-
x.xEntityValue
800+
assertEquals("", x.xEntityValue)
793801
}
794802

795803
}

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)