web3swift
web3swift copied to clipboard
Decoding issue: unable to decode Data.SubSequence
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.
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.
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!
It was new to me too... I only came across it when I did a little looking into this issue.
@JeneaVranceanu Is it relevant yet? Can I close it?
@JeneaVranceanu ping