From 955ac2d58f26646181be64f2432f1a3f7e8cccc7 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 20 Mar 2024 11:51:03 +0100 Subject: [PATCH] abi decoding of custom error fails on trailing bytes --- ethers/errors.nim | 2 ++ testmodule/testErrorDecoding.nim | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/ethers/errors.nim b/ethers/errors.nim index 00b6ccc..fa2c3bb 100644 --- a/ethers/errors.nim +++ b/ethers/errors.nim @@ -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] = diff --git a/testmodule/testErrorDecoding.nim b/testmodule/testErrorDecoding.nim index 6112d28..a843e76 100644 --- a/testmodule/testErrorDecoding.nim +++ b/testmodule/testErrorDecoding.nim @@ -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: