Skip to content

Commit c1db5fd

Browse files
committed
Merge pull request #23 from dacr/master
SI-4267 - fix to avoid XMLEventReader swallows IO/Parse exception
2 parents 567cc3c + 3db4895 commit c1db5fd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/main/scala/scala/xml/pull/XMLEventReader.scala

+9-1
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,19 @@ class XMLEventReader(src: Source)
9292

9393
override def run() {
9494
curInput = input
95-
interruptibly { this.initialize.document() }
95+
try {
96+
interruptibly { this.initialize.document() }
97+
} catch {
98+
case e:Exception => setEvent(ExceptionEvent(e))
99+
}
96100
setEvent(POISON)
97101
}
98102
}
99103
}
100104

105+
// An internal class used to propagate exception from helper threads to API end users.
106+
private case class ExceptionEvent(exception:Exception) extends XMLEvent
107+
101108
// An iterator designed for one or more producers to generate
102109
// elements, and a single consumer to iterate. Iteration will continue
103110
// until closeIterator() is called, after which point producers
@@ -143,6 +150,7 @@ trait ProducerConsumerIterator[T >: Null] extends Iterator[T] {
143150
def next() = {
144151
if (eos()) throw new NoSuchElementException("ProducerConsumerIterator")
145152
if (buffer == null) fillBuffer()
153+
if (buffer.isInstanceOf[ExceptionEvent]) throw buffer.asInstanceOf[ExceptionEvent].exception
146154

147155
drainBuffer()
148156
}

src/test/scala/scala/xml/pull/XMLEventReaderTest.scala

+19-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,22 @@ class XMLEventReaderTest {
4040
er.stop // allow thread to be garbage-collected
4141
}
4242

43-
}
43+
@Test(expected = classOf[Exception])
44+
def missingTagTest: Unit = {
45+
val data=
46+
"""<?xml version="1.0" ?>
47+
|<verbosegc xmlns="http://www.ibm.com/j9/verbosegc">
48+
|
49+
|<initialized id="1" timestamp="2013-10-04T00:11:08.389">
50+
|</initialized>
51+
|
52+
|<exclusive-start id="2" timestamp="2013-10-04T00:11:09.185" intervalms="796.317">
53+
|<response-info timems="0.007" idlems="0.007" threads="0" />
54+
|</exclusive-start>
55+
|""".stripMargin
56+
57+
val er = new XMLEventReader(Source.fromString(data))
58+
while(er.hasNext) er.next()
59+
er.stop()
60+
}
61+
}

0 commit comments

Comments
 (0)