From 7b31495ae363bd2eefffd3701801dc640f679265 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 13 Sep 2020 19:43:06 -0700 Subject: [PATCH] Expand json rpc request input buffer if needed When a json rpc request is specified without any headers, the size of the message may exceed the buffer that was created for reading hdeaders. This would cause an exception to be thrown when creating a string from the header buffer because the number of bytes requested would exceed the capacity of the buffer. To fix this, we can expand the buffer dynamically if needed. For the common case when the headers are specified, this should be a no-op. --- .../sbt/internal/util/ReadJsonFromInputStream.scala | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/main-command/src/main/scala/sbt/internal/util/ReadJsonFromInputStream.scala b/main-command/src/main/scala/sbt/internal/util/ReadJsonFromInputStream.scala index ceaa98926..693f2df5f 100644 --- a/main-command/src/main/scala/sbt/internal/util/ReadJsonFromInputStream.scala +++ b/main-command/src/main/scala/sbt/internal/util/ReadJsonFromInputStream.scala @@ -30,7 +30,12 @@ private[sbt] object ReadJsonFromInputStream { * content-length, so this should be fine. If we ever start doing anything * with headers, we may need to adjust this buffer size. */ - val headerBuffer = new Array[Byte](128) + var headerBuffer = new Array[Byte](128) + def expandHeaderBuffer(): Unit = { + val newHeaderBuffer = new Array[Byte](headerBuffer.size * 2) + headerBuffer.view.zipWithIndex.foreach { case (b, i) => newHeaderBuffer(i) = b } + headerBuffer = newHeaderBuffer + } def getLine(): String = { val line = new String(headerBuffer, 0, index, "UTF-8") index = 0 @@ -61,7 +66,8 @@ private[sbt] object ReadJsonFromInputStream { case c => if (c == newline) getLine() else { - if (index < headerBuffer.length) headerBuffer(index) = c.toByte + if (index >= headerBuffer.length) expandHeaderBuffer() + headerBuffer(index) = c.toByte index += 1 } onCarriageReturn = false @@ -88,7 +94,8 @@ private[sbt] object ReadJsonFromInputStream { case `carriageReturn` => onCarriageReturn = true case c => onCarriageReturn = false - if (index < headerBuffer.length) headerBuffer(index) = c.toByte + if (index >= headerBuffer.length) expandHeaderBuffer() + headerBuffer(index) = c.toByte index += 1 }