diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/DataStreams.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/DataStreams.kt index 94a4586f6..bcef9aad4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/DataStreams.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/DataStreams.kt @@ -7,6 +7,7 @@ import java.io.InputStream import java.io.OutputStream import java.util.* import java.util.function.Predicate +import kotlin.NoSuchElementException import kotlin.math.absoluteValue interface IStreamCodec { @@ -137,6 +138,10 @@ fun InputStream.readDouble() = Double.fromBits(readLong()) fun InputStream.readVarIntLE(): Int { val readFirst = read() + if (readFirst < 0) { + throw NoSuchElementException("Reached end of stream") + } + if (readFirst and 64 == 0) { return if (readFirst and 128 != 0) -(readFirst and 63) else readFirst and 63 } @@ -149,6 +154,11 @@ fun InputStream.readVarIntLE(): Int { while (nextBit != 0) { result = result or (read shl i) read = read() + + if (read < 0) { + throw NoSuchElementException("Reached end of stream") + } + nextBit = read and 128 read = read and 127 @@ -175,6 +185,10 @@ fun OutputStream.writeVarIntLE(value: Int) { fun InputStream.readVarLongLE(): Long { val readFirst = read() + if (readFirst < 0) { + throw NoSuchElementException("Reached end of stream") + } + if (readFirst and 64 == 0) { return if (readFirst and 128 != 0) -(readFirst and 63).toLong() else (readFirst and 63).toLong() } @@ -187,6 +201,11 @@ fun InputStream.readVarLongLE(): Long { while (nextBit != 0) { result = result or (read shl i).toLong() read = read() + + if (read < 0) { + throw NoSuchElementException("Reached end of stream") + } + nextBit = read and 128 read = read and 127