Skip to content

Commit 009416e

Browse files
committed
Throw specialized exception when bounds invalid
Inpspired by ethereum/ethereumj#1073 Cannot really use the exact test-case there as it is lgpl and we are MIT But the input here is basically doing the same.
1 parent 94e7923 commit 009416e

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

rlp/src/main/kotlin/org/kethereum/functions/rlp/RLPDecoder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ private data class LengthAndOffset(val length: Int, val offset: Int)
1414

1515
private fun ByteArray.decodeRLPWithSize(offset: Int = 0): DecodeResult {
1616

17+
if (offset >= size) {
18+
throw IllegalRLPException("Cannot decode RLP at offset=$offset and size=$size")
19+
}
20+
1721
val value = this[offset].toInt() and 0xFF
1822
return when {
1923
value < ELEMENT_OFFSET -> DecodeResult(value.toRLP(), 1)

rlp/src/main/kotlin/org/kethereum/functions/rlp/RLPModel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ data class RLPElement(val bytes: ByteArray) : RLPType() {
2020
override fun hashCode() = Arrays.hashCode(bytes)
2121
}
2222

23-
data class RLPList(val element: List<RLPType>) : RLPType()
23+
data class RLPList(val element: List<RLPType>) : RLPType()
24+
25+
class IllegalRLPException(msg:String) : IllegalArgumentException(msg)

rlp/src/test/kotlin/org/kethereum/functions/rlp/TheRLPDecoder.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.kethereum.functions.rlp
33
import org.assertj.core.api.Assertions.assertThat
44
import org.junit.Test
55
import org.walleth.khex.hexToByteArray
6+
import kotlin.test.assertFailsWith
67

78
class TheRLPDecoder {
89
@Test
@@ -13,6 +14,17 @@ class TheRLPDecoder {
1314
}
1415
}
1516

17+
@Test
18+
fun testRLPException() {
19+
20+
val hex = "f912345678"
21+
22+
assertFailsWith<IllegalRLPException> {
23+
hex.hexToByteArray().decodeRLP()
24+
}
25+
}
26+
27+
1628
@Test
1729
fun testSizeOverflow() {
1830

0 commit comments

Comments
 (0)