mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 14:46:42 +00:00
accounts/abi: return an error if attempting to pack a negative big.Int into a uint val
This commit is contained in:
parent
485ff4bbff
commit
c3379da2f5
2 changed files with 20 additions and 0 deletions
|
|
@ -33,11 +33,24 @@ func packBytesSlice(bytes []byte, l int) []byte {
|
||||||
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
|
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateNum(t Type, reflectValue reflect.Value) error {
|
||||||
|
if t.T == UintTy && reflectValue.Kind() == reflect.Ptr {
|
||||||
|
val := new(big.Int).Set(reflectValue.Interface().(*big.Int))
|
||||||
|
if val.Sign() == -1 {
|
||||||
|
return fmt.Errorf("cannot pack negative big.Int value to a uint type")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// packElement packs the given reflect value according to the abi specification in
|
// packElement packs the given reflect value according to the abi specification in
|
||||||
// t.
|
// t.
|
||||||
func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
|
func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
|
||||||
switch t.T {
|
switch t.T {
|
||||||
case IntTy, UintTy:
|
case IntTy, UintTy:
|
||||||
|
if err := validateNum(t, reflectValue); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return packNum(reflectValue), nil
|
return packNum(reflectValue), nil
|
||||||
case StringTy:
|
case StringTy:
|
||||||
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
|
return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
|
||||||
|
|
|
||||||
|
|
@ -177,6 +177,13 @@ func TestMethodPack(t *testing.T) {
|
||||||
if !bytes.Equal(packed, sig) {
|
if !bytes.Equal(packed, sig) {
|
||||||
t.Errorf("expected %x got %x", sig, packed)
|
t.Errorf("expected %x got %x", sig, packed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = abi.Pack("send", big.NewInt(-1))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error when trying to pack negative big.Int into uint256 value")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: examine what happens if we supply a negative non-big int to a method expect smaller uint val
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPackNumber(t *testing.T) {
|
func TestPackNumber(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue