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
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
1717package abi
1818
@@ -37,7 +37,16 @@ func packBytesSlice(bytes []byte, l int) []byte {
3737// t.
3838func 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
0 commit comments