emv-bertlv icon indicating copy to clipboard operation
emv-bertlv copied to clipboard

Cannot parse an empty data source

Open IonutNegru87 opened this issue 3 years ago • 1 comments

In the scenario where the data source is empty the library crashes throwing Method java.lang.IndexOutOfBoundsException exception.

Code used to test this:

assertNull(BerTlv.parse("".toByteArray()))

I think this can be easily fixed by this change in the BerTlv.java class:

@JvmStatic
fun parse(data: ByteArray): BerTlv {
      if (data.isNotEmpty()) {
        return parseList(ByteBuffer.wrap(data), true)[0]
    } else {
        null
    }
}

IonutNegru87 avatar Nov 21 '22 12:11 IonutNegru87

Meanwhile I've created an extension function that does the validation before using the static BerTlv.parse() method:

fun String.parseBerTlv(): BerTlv? {
    return if (ISOUtil.isValidHexString(this)) {
        BerTlv.parse(this.decodeHex())
    } else {
        null
    }
}

IonutNegru87 avatar Nov 22 '22 09:11 IonutNegru87