From e2ea9e87b59d42ef92ddee477358d7eab8431e7e Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Tue, 14 Jan 2025 10:56:07 +0800 Subject: [PATCH] accounts/abi: fix panic in MethodById lookup (#17798) --- accounts/abi/abi.go | 3 +++ accounts/abi/abi_test.go | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go index 0ca97525cd..0c68a0a1ee 100644 --- a/accounts/abi/abi.go +++ b/accounts/abi/abi.go @@ -140,6 +140,9 @@ func (abi *ABI) UnmarshalJSON(data []byte) error { // MethodById looks up a method by the 4-byte id // returns nil if none found func (abi *ABI) MethodById(sigdata []byte) (*Method, error) { + if len(sigdata) < 4 { + return nil, fmt.Errorf("data too short (% bytes) for abi method lookup", len(sigdata)) + } for _, method := range abi.Methods { if bytes.Equal(method.Id(), sigdata[:4]) { return &method, nil diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go index 968cdb6941..e952c0bc7f 100644 --- a/accounts/abi/abi_test.go +++ b/accounts/abi/abi_test.go @@ -716,7 +716,16 @@ func TestABI_MethodById(t *testing.T) { t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, hexutil.Encode(m.Id())) } } - + // Also test empty + if _, err := abi.MethodById([]byte{0x00}); err == nil { + t.Errorf("Expected error, too short to decode data") + } + if _, err := abi.MethodById([]byte{}); err == nil { + t.Errorf("Expected error, too short to decode data") + } + if _, err := abi.MethodById(nil); err == nil { + t.Errorf("Expected error, nil is short to decode data") + } } func TestUnpackRevert(t *testing.T) {