From 8ab7fd3ae75d5c78f503245da332416cea63d118 Mon Sep 17 00:00:00 2001 From: Mike McGann Date: Wed, 15 Mar 2023 15:43:23 -0400 Subject: [PATCH 1/2] Fix infinite loop caused by unclosed character ref Resolves #306 If parseCharRef is called for an unclosed character reference it will get caught in an infinite loop if the end of file is reached. Add a check to exit the loop when an EOF is encountered. --- shared/src/main/scala/scala/xml/Utility.scala | 4 ++-- shared/src/test/scala/scala/xml/UtilityTest.scala | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) 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..2307aef4 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 issue306InvalidUnclosedEntity(): 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) + } + } From 018fa4afccc276a6acc7a133b989539026c4b367 Mon Sep 17 00:00:00 2001 From: Mike McGann Date: Mon, 27 Mar 2023 08:56:24 -0400 Subject: [PATCH 2/2] Rename test method --- shared/src/test/scala/scala/xml/UtilityTest.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/src/test/scala/scala/xml/UtilityTest.scala b/shared/src/test/scala/scala/xml/UtilityTest.scala index 2307aef4..87384d3e 100644 --- a/shared/src/test/scala/scala/xml/UtilityTest.scala +++ b/shared/src/test/scala/scala/xml/UtilityTest.scala @@ -221,7 +221,7 @@ class UtilityTest { } @Test - def issue306InvalidUnclosedEntity(): Unit = { + def issue306InvalidUnclosedCharRef(): Unit = { val entity = "&# test " val it: Iterator[Char] = entity.iterator var c = it.next()