Skip to content

Commit 06b53fe

Browse files
authored
Merge pull request #2380 from CortexFoundation/dev
error when packing negative values in unsigned types
2 parents ab14940 + 2e0a690 commit 06b53fe

3 files changed

Lines changed: 32 additions & 17 deletions

File tree

accounts/abi/error_handling.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright 2016 The go-ethereum Authors
2-
// This file is part of The go-ethereum library.
2+
// This file is part of the go-ethereum library.
33
//
44
// The go-ethereum library is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU Lesser General Public License as published by
@@ -12,7 +12,7 @@
1212
// GNU Lesser General Public License for more details.
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
15-
// along with The go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

1717
package abi
1818

@@ -23,15 +23,16 @@ import (
2323
)
2424

2525
var (
26-
errBadBool = errors.New("abi: improperly encoded boolean value")
27-
errBadUint8 = errors.New("abi: improperly encoded uint8 value")
28-
errBadUint16 = errors.New("abi: improperly encoded uint16 value")
29-
errBadUint32 = errors.New("abi: improperly encoded uint32 value")
30-
errBadUint64 = errors.New("abi: improperly encoded uint64 value")
31-
errBadInt8 = errors.New("abi: improperly encoded int8 value")
32-
errBadInt16 = errors.New("abi: improperly encoded int16 value")
33-
errBadInt32 = errors.New("abi: improperly encoded int32 value")
34-
errBadInt64 = errors.New("abi: improperly encoded int64 value")
26+
errBadBool = errors.New("abi: improperly encoded boolean value")
27+
errBadUint8 = errors.New("abi: improperly encoded uint8 value")
28+
errBadUint16 = errors.New("abi: improperly encoded uint16 value")
29+
errBadUint32 = errors.New("abi: improperly encoded uint32 value")
30+
errBadUint64 = errors.New("abi: improperly encoded uint64 value")
31+
errBadInt8 = errors.New("abi: improperly encoded int8 value")
32+
errBadInt16 = errors.New("abi: improperly encoded int16 value")
33+
errBadInt32 = errors.New("abi: improperly encoded int32 value")
34+
errBadInt64 = errors.New("abi: improperly encoded int64 value")
35+
errInvalidSign = errors.New("abi: negatively-signed value cannot be packed into uint parameter")
3536
)
3637

3738
// formatSliceString formats the reflection kind with the given slice size
@@ -84,6 +85,6 @@ func typeCheck(t Type, value reflect.Value) error {
8485
}
8586

8687
// typeErr returns a formatted type casting error.
87-
func typeErr(expected, got any) error {
88+
func typeErr(expected, got interface{}) error {
8889
return fmt.Errorf("abi: cannot use %v as type %v as argument", got, expected)
8990
}

accounts/abi/pack.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright 2016 The go-ethereum Authors
2-
// This file is part of The go-ethereum library.
2+
// This file is part of the go-ethereum library.
33
//
44
// The go-ethereum library is free software: you can redistribute it and/or modify
55
// it under the terms of the GNU Lesser General Public License as published by
@@ -12,7 +12,7 @@
1212
// GNU Lesser General Public License for more details.
1313
//
1414
// You should have received a copy of the GNU Lesser General Public License
15-
// along with The go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

1717
package abi
1818

@@ -37,7 +37,16 @@ func packBytesSlice(bytes []byte, l int) []byte {
3737
// t.
3838
func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
3939
switch t.T {
40-
case IntTy, UintTy:
40+
case UintTy:
41+
// make sure to not pack a negative value into a uint type.
42+
if reflectValue.Kind() == reflect.Ptr {
43+
val := new(big.Int).Set(reflectValue.Interface().(*big.Int))
44+
if val.Sign() == -1 {
45+
return nil, errInvalidSign
46+
}
47+
}
48+
return packNum(reflectValue), nil
49+
case IntTy:
4150
return packNum(reflectValue), nil
4251
case StringTy:
4352
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
@@ -57,7 +66,7 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
5766
reflectValue = mustArrayToByteSlice(reflectValue)
5867
}
5968
if reflectValue.Type() != reflect.TypeOf([]byte{}) {
60-
return []byte{}, errors.New("Bytes type is neither slice nor array")
69+
return []byte{}, errors.New("bytes type is neither slice nor array")
6170
}
6271
return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil
6372
case FixedBytesTy, FunctionTy:
@@ -66,7 +75,7 @@ func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
6675
}
6776
return common.RightPadBytes(reflectValue.Bytes(), 32), nil
6877
default:
69-
return []byte{}, fmt.Errorf("Could not pack element, unknown type: %v", t.T)
78+
return []byte{}, fmt.Errorf("could not pack element, unknown type: %v", t.T)
7079
}
7180
}
7281

accounts/abi/pack_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ func TestMethodPack(t *testing.T) {
178178
if !bytes.Equal(packed, sig) {
179179
t.Errorf("expected %x got %x", sig, packed)
180180
}
181+
182+
// test that we can't pack a negative value for a parameter that is specified as a uint
183+
if _, err := abi.Pack("send", big.NewInt(-1)); err == nil {
184+
t.Fatal("expected error when trying to pack negative big.Int into uint256 value")
185+
}
181186
}
182187

183188
func TestPackNumber(t *testing.T) {

0 commit comments

Comments
 (0)