Skip to content

Commit

Permalink
Fix OOM in parser
Browse files Browse the repository at this point in the history
Add eof checks to:

- MarkupParser.xEntityValue
- MarkupParser.xComment
- MarkupParser.systemLiteral
- MarkupParser.pubidLiteral
- MarkupParser.notationDecl
- MarkupParserCommon.xAttributeValue
- MarkupParserCommon.xTakeUntil
  • Loading branch information
ashawley committed Apr 16, 2018
1 parent 4b6dcf5 commit cc4a06b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
24 changes: 16 additions & 8 deletions jvm/src/test/scala/scala/xml/XMLTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -659,27 +659,35 @@ class XMLTestJVM {
x.xComment
}

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

x.xmlProcInstr
assertEquals(scala.xml.Null, x.xmlProcInstr)
}

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

x.xAttributeValue
x.notationDecl
}

def pubidLiteralFailure {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString(""), false)

assertEquals("", x.pubidLiteral)
}

def xAttributeValueFailure {
val x = xml.parsing.ConstructingParser.fromSource(io.Source.fromString("'"), false)

assertEquals("", x.xAttributeValue)
}

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

x.xEntityValue
assertEquals("", x.xEntityValue)
}

}
4 changes: 2 additions & 2 deletions shared/src/main/scala/scala/xml/parsing/MarkupParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ trait MarkupParser extends MarkupParserCommon with TokenTests {
} else sb.append(ch)
nextch()
}
throw truncatedError("broken comment")
truncatedError("broken comment")
}

/* todo: move this into the NodeBuilder class */
Expand Down Expand Up @@ -928,7 +928,7 @@ trait MarkupParser extends MarkupParserCommon with TokenTests {
new PublicID(pubID, sysID)
} else {
reportSyntaxError("PUBLIC or SYSTEM expected")
scala.sys.error("died parsing notationdecl")
truncatedError("died parsing notationdecl")
}
xSpaceOpt()
xToken('>')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Utility.SU
* All members should be accessed through those.
*/
private[scala] trait MarkupParserCommon extends TokenTests {
protected def unreachable = scala.sys.error("Cannot be reached.")
protected def unreachable = truncatedError("Cannot be reached.")

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

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

sb append ch
nextch()
Expand Down

0 comments on commit cc4a06b

Please sign in to comment.