From 19f6aa9b1581662b03318bdd3bbe59c19e494992 Mon Sep 17 00:00:00 2001 From: rnentjes Date: Fri, 13 Sep 2024 20:13:37 +0200 Subject: [PATCH] Fix off-by-one error --- .../nl/astraeus/tba/ByteArrayHandler.kt | 20 ++++++++-------- .../kotlin/nl/astraeus/tba/SlicedByteArray.kt | 4 +++- .../kotlin/nl/astraeus/tba/TypedByteArray.kt | 3 ++- .../tba/MutableByteArrayHandlerTest.kt | 23 +++++++++++++++++++ 4 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/commonTest/kotlin/nl/astraeus/tba/MutableByteArrayHandlerTest.kt diff --git a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt index ae817c7..6630dad 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/ByteArrayHandler.kt @@ -1,12 +1,15 @@ package nl.astraeus.tba class MutableByteArrayHandler( - initialSize: Int = 1024, - buffer: SlicedByteArray = SlicedByteArray.wrap(ByteArray(initialSize), 0, initialSize), -) : ByteArrayHandler(initialSize, buffer) { + buffer: SlicedByteArray +) : ByteArrayHandler(buffer) { - constructor(buffer: ByteArray, range: IntRange = buffer.indices) : this( - buffer = SlicedByteArray.wrap(buffer, range.first, range.last - range.first) + constructor( + buffer: ByteArray, + offset: Int = 0, + length: Int = buffer.size + ) : this( + SlicedByteArray.wrap(buffer, offset, length) ) operator fun set(index: Int, value: Byte) { @@ -30,12 +33,11 @@ class MutableByteArrayHandler( } open class ByteArrayHandler( - size: Int = 1024, - var buffer: SlicedByteArray = SlicedByteArray.wrap(ByteArray(size), 0, size), + var buffer: SlicedByteArray ) { constructor(buffer: ByteArray, range: IntRange = buffer.indices) : this( - buffer = SlicedByteArray.wrap(buffer, range.first, range.last - range.first) + buffer = SlicedByteArray.wrap(buffer, range.first, range.last - range.first + 1) ) operator fun get(index: Int): Byte { @@ -70,7 +72,7 @@ open class ByteArrayHandler( fun slice(range: IntRange): ByteArrayHandler { return ByteArrayHandler( - buffer = SlicedByteArray(this.buffer.data, range.first, range.last - range.first) + buffer = SlicedByteArray(this.buffer.data, range.first, range.last - range.first + 1) ) } diff --git a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt index 70c47db..0d24260 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/SlicedByteArray.kt @@ -8,6 +8,8 @@ class SlicedByteArray( val size: Int get() = length + constructor(size: Int) : this(ByteArray(size), 0, size) + operator fun get(index: Int): Byte { return data[offset + index] } @@ -83,7 +85,7 @@ class SlicedByteArray( fun getClob(index: Int): String { val length = getInt(index) check(offset + index + length <= offset + this.length) { "Index out of bounds" } - val str = data.copyOfRange(offset + index + 4, offset + index + 4 + length).decodeToString() + val str = data.copyOfRange(offset + index + 4, offset + index + 4 + length + 1).decodeToString() return str } diff --git a/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt b/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt index ff50c92..99b5846 100644 --- a/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt +++ b/src/commonMain/kotlin/nl/astraeus/tba/TypedByteArray.kt @@ -2,7 +2,7 @@ package nl.astraeus.tba open class TypedByteArray( val definition: ByteArrayDefinition, - var data: MutableByteArrayHandler = MutableByteArrayHandler(definition.size), + var data: MutableByteArrayHandler = MutableByteArrayHandler(SlicedByteArray(definition.size)), ) { val typeMap = mutableMapOf() val indexMap = mutableMapOf() @@ -22,6 +22,7 @@ open class TypedByteArray( constructor(vararg types: Type): this(ByteArrayDefinition(*types)) constructor(data: ByteArray): this(ByteArrayDefinition()) { + check(data.size == definition.size) { "Data size ${data.size} does not match definition size ${definition.size}" } this.data = MutableByteArrayHandler(buffer = data) } } diff --git a/src/commonTest/kotlin/nl/astraeus/tba/MutableByteArrayHandlerTest.kt b/src/commonTest/kotlin/nl/astraeus/tba/MutableByteArrayHandlerTest.kt new file mode 100644 index 0000000..a5d2159 --- /dev/null +++ b/src/commonTest/kotlin/nl/astraeus/tba/MutableByteArrayHandlerTest.kt @@ -0,0 +1,23 @@ +package nl.astraeus.tba + +import kotlin.test.Test +import kotlin.test.assertEquals + +class MutableByteArrayHandlerTest { + + @Test + fun testSimpleTypedByteArray() { + val mbah = MutableByteArrayHandler(SlicedByteArray(10)) + + assertEquals(10, mbah.buffer.size) + assertEquals(10, mbah.buffer.length) + } + + @Test + fun testSimpleTypedByteArray2() { + val mbah = MutableByteArrayHandler(ByteArray(468)) + + assertEquals(468, mbah.buffer.size) + assertEquals(468, mbah.buffer.length) + } +}