From 5d84640d1f666bb60a0660bd06a27f6884fa02d8 Mon Sep 17 00:00:00 2001 From: Konstantinos Chalkias Date: Tue, 9 Oct 2018 09:48:54 +0100 Subject: [PATCH] Add missing validation in the OpaqueBytesSubSequence.init (#4047) --- .../main/kotlin/net/corda/core/utilities/ByteArrays.kt | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/core/src/main/kotlin/net/corda/core/utilities/ByteArrays.kt b/core/src/main/kotlin/net/corda/core/utilities/ByteArrays.kt index 0407bd95bb..7190e0c770 100644 --- a/core/src/main/kotlin/net/corda/core/utilities/ByteArrays.kt +++ b/core/src/main/kotlin/net/corda/core/utilities/ByteArrays.kt @@ -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) } }