Add missing validation in the OpaqueBytesSubSequence.init (#4047)

This commit is contained in:
Konstantinos Chalkias 2018-10-09 09:48:54 +01:00 committed by GitHub
parent d1adb09ca9
commit 5d84640d1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,7 +29,7 @@ sealed class ByteSequence(private val _bytes: ByteArray, val offset: Int, val si
*/
abstract val bytes: ByteArray
/** Returns a [ByteArrayInputStream] of the bytes */
/** Returns a [ByteArrayInputStream] of the bytes. */
fun open() = ByteArrayInputStream(_bytes, offset, size)
/**
@ -41,8 +41,6 @@ sealed class ByteSequence(private val _bytes: ByteArray, val offset: Int, val si
*/
@Suppress("MemberVisibilityCanBePrivate")
fun subSequence(offset: Int, size: Int): ByteSequence {
require(offset >= 0)
require(offset + size <= this.size)
// Intentionally use bytes rather than _bytes, to mirror the copy-or-not behaviour of that property.
return if (offset == 0 && size == this.size) this else of(bytes, this.offset + offset, size)
}
@ -108,7 +106,7 @@ sealed class ByteSequence(private val _bytes: ByteArray, val offset: Int, val si
return Integer.signum(unsignedThis - unsignedOther)
}
}
// First min bytes is the same, so now resort to size
// First min bytes is the same, so now resort to size.
return Integer.signum(this.size - other.size)
}
@ -190,12 +188,12 @@ fun ByteArray.toHexString(): String = DatatypeConverter.printHexBinary(this)
fun String.parseAsHex(): ByteArray = DatatypeConverter.parseHexBinary(this)
/**
* Class is public for serialization purposes
* Class is public for serialization purposes.
*/
@KeepForDJVM
class OpaqueBytesSubSequence(override val bytes: ByteArray, offset: Int, size: Int) : ByteSequence(bytes, offset, size) {
init {
require(offset >= 0 && offset < bytes.size)
require(size >= 0 && size <= bytes.size)
require(size >= 0 && offset + size <= bytes.size)
}
}