Skip to content

Commit 96fd50b

Browse files
holimangballet
authored andcommitted
accounts/abi: fix panic in MethodById lookup. Fixes #17797 (#17798)
1 parent dc5d643 commit 96fd50b

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎accounts/abi/abi.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
137137
// MethodById looks up a method by the 4-byte id
138138
// returns nil if none found
139139
func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
140+
if len(sigdata) < 4 {
141+
return nil, fmt.Errorf("data too short (% bytes) for abi method lookup", len(sigdata))
142+
}
140143
for _, method := range abi.Methods {
141144
if bytes.Equal(method.Id(), sigdata[:4]) {
142145
return &method, nil

‎accounts/abi/abi_test.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,5 +711,14 @@ func TestABI_MethodById(t *testing.T) {
711711
t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
712712
}
713713
}
714-
714+
// Also test empty
715+
if _, err := abi.MethodById([]byte{0x00}); err == nil {
716+
t.Errorf("Expected error, too short to decode data")
717+
}
718+
if _, err := abi.MethodById([]byte{}); err == nil {
719+
t.Errorf("Expected error, too short to decode data")
720+
}
721+
if _, err := abi.MethodById(nil); err == nil {
722+
t.Errorf("Expected error, nil is short to decode data")
723+
}
715724
}

0 commit comments

Comments
 (0)