web3swift icon indicating copy to clipboard operation
web3swift copied to clipboard

Decoding issue: unable to decode Data.SubSequence

Open JeneaVranceanu opened this issue 3 years ago • 5 comments
trafficstars

The issue Passing a result of Data(...).dropFirst(n) to ABIDecoder.decode results in

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

The reason is that dropFirst/Last does not return a new Data object but returns a wrapper that represents a slice of original Data. Thus, doing data.dropFirst(4) returns a slice that will crash if we attempt to read any of the first 4 bytes using the subscript operator.

This is not critical as the slice can be used as argument for initializer of Data by the user of the web3swift library to avoid this issue (that is Data(otherData.dropFirst(n)) will fix the issue, but it's a wrong solution). The error we receive in case of such a crash isn't obvious.

Solution Update the decoding part so that it is able to handle Data.SubSequence by using lower and upper bounds of Data.indices.

JeneaVranceanu avatar Apr 19 '22 15:04 JeneaVranceanu

Interesting, however the correct solution may still be to generate a new Data object, as once we get into slices, we are no longer guaranteed to have contiguous indexes, it is entirely possible that the slice has chunks removed from the middle as well, not just the beginning or end.

mloit avatar Apr 19 '22 19:04 mloit

Hm, haven't thought of a Data object with missing middle bytes. I'll take some time this week to check what we can do about this whole situation. Thanks for the hint!

JeneaVranceanu avatar Apr 20 '22 06:04 JeneaVranceanu

It was new to me too... I only came across it when I did a little looking into this issue.

mloit avatar Apr 20 '22 16:04 mloit

@JeneaVranceanu Is it relevant yet? Can I close it?

yaroslavyaroslav avatar Oct 09 '22 06:10 yaroslavyaroslav

@JeneaVranceanu ping

yaroslavyaroslav avatar Nov 14 '22 10:11 yaroslavyaroslav