Skip to content

Commit

Permalink
Fix off-by-one errors in the byte reader
Browse files Browse the repository at this point in the history
  • Loading branch information
JD557 committed Mar 1, 2024
1 parent ade1823 commit 23af162
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ private[minart] object ByteReader {
val byteArr = Array.ofDim[Byte](n)
var read = bytes.read(byteArr)
while (read >= 0 && read < n) {
byteArr(read) = bytes.read().toByte
read += 1
val byte = bytes.read()
if (byte == -1) read = -1
else {
byteArr(read) = byte.toByte
read += 1
}
}
bytes -> byteArr.map(b => java.lang.Byte.toUnsignedInt(b))
}
Expand All @@ -43,8 +47,12 @@ private[minart] object ByteReader {
val byteArr = Array.ofDim[Byte](n)
var read = bytes.read(byteArr)
while (read >= 0 && read < n) {
byteArr(read) = bytes.read().toByte
read += 1
val byte = bytes.read()
if (byte == -1) read = -1
else {
byteArr(read) = byte.toByte
read += 1
}
}
bytes -> byteArr
}
Expand Down Expand Up @@ -107,7 +115,7 @@ private[minart] object ByteReader {
else {
hasBuffer = false
b(0) = buffer.toByte
inner.read(b, 1, b.size - 1)
inner.read(b, 1, b.size - 1) + 1
}
}
override def reset(): Unit = ()
Expand Down

0 comments on commit 23af162

Please sign in to comment.