Browse Source

accounts/abi: fix panic in MethodById lookup. Fixes #17797 (#17798)

Martin Holst Swende 7 years ago
parent
commit
96fd50be10
2 changed files with 13 additions and 1 deletions
  1. 3 0
      accounts/abi/abi.go
  2. 10 1
      accounts/abi/abi_test.go

+ 3 - 0
accounts/abi/abi.go

@@ -137,6 +137,9 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
 // MethodById looks up a method by the 4-byte id
 // MethodById looks up a method by the 4-byte id
 // returns nil if none found
 // returns nil if none found
 func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
 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 {
 	for _, method := range abi.Methods {
 		if bytes.Equal(method.Id(), sigdata[:4]) {
 		if bytes.Equal(method.Id(), sigdata[:4]) {
 			return &method, nil
 			return &method, nil

+ 10 - 1
accounts/abi/abi_test.go

@@ -711,5 +711,14 @@ func TestABI_MethodById(t *testing.T) {
 			t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
 			t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(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")
+	}
 }
 }