abi decoding of custom error fails on trailing bytes

This commit is contained in:
Mark Spanbroek 2024-03-20 11:51:03 +01:00 committed by markspanbroek
parent 6d6777e8c3
commit 955ac2d58f
2 changed files with 14 additions and 0 deletions

View File

@ -29,6 +29,8 @@ func decode*[E: SolidityError](_: type E, data: seq[byte]): ?!(ref E) =
return failure "unable to decode " & $E & ": " & error.msg
success (ref E)(arguments: arguments)
else:
if data.len > 4:
return failure "unable to decode " & $E & ": unread trailing bytes found"
success (ref E)()
func encode*[E: SolidityError](_: type AbiEncoder, error: ref E): seq[byte] =

View File

@ -34,6 +34,18 @@ suite "Decoding of custom errors":
let decoded = SimpleError.decode(invalid)
check decoded.isFailure
test "returns failure when there are trailing bytes":
let invalid = @[0xc2'u8, 0xbb, 0x94, 0x7c, 0x0] # one byte too many
let decoded = SimpleError.decode(invalid)
check decoded.isFailure
test "returns failure when there are trailing bytes after arguments":
let error = (ref ErrorWithArguments)(arguments: (1.u256, true))
let encoded = AbiEncoder.encode(error)
let invalid = encoded & @[0x0'u8] # one byte too many
let decoded = ErrorWithArguments.decode(invalid)
check decoded.isFailure
test "decoding only works for SolidityErrors":
type InvalidError = ref object of CatchableError
const works = compiles: