diff --git a/shared/src/main/scala/scala/xml/Utility.scala b/shared/src/main/scala/scala/xml/Utility.scala index c812e4b6..ad39b994 100755 --- a/shared/src/main/scala/scala/xml/Utility.scala +++ b/shared/src/main/scala/scala/xml/Utility.scala @@ -392,7 +392,7 @@ object Utility extends AnyRef with parsing.TokenTests { val hex: Boolean = (ch() == 'x') && { nextch(); true } val base: Int = if (hex) 16 else 10 var i: Int = 0 - while (ch() != ';') { + while (ch() != ';' && ch() != 0) { ch() match { case '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => i = i * base + ch().asDigit @@ -410,6 +410,6 @@ object Utility extends AnyRef with parsing.TokenTests { } nextch() } - new String(Array(i), 0, 1) + if (i != 0) new String(Array(i), 0, 1) else "" } } diff --git a/shared/src/test/scala/scala/xml/UtilityTest.scala b/shared/src/test/scala/scala/xml/UtilityTest.scala index 219dbdd2..87384d3e 100644 --- a/shared/src/test/scala/scala/xml/UtilityTest.scala +++ b/shared/src/test/scala/scala/xml/UtilityTest.scala @@ -219,4 +219,15 @@ class UtilityTest { val x: Elem =
{Text(" My name ")}{Text(" is ")}{Text(" Harry ")}
assertEquals(
My name is Harry
, Utility.trim(x)) } + + @Test + def issue306InvalidUnclosedCharRef(): Unit = { + val entity = "&# test " + val it: Iterator[Char] = entity.iterator + var c = it.next() + val next = () => if (it.hasNext) c = it.next() else c = 0.asInstanceOf[Char] + val result = Utility.parseCharRef({ () => c }, next, _ => {}, _ => {}) + assertEquals("", result) + } + }