Skip to content

Commit

Permalink
fixed #22
Browse files Browse the repository at this point in the history
* return an error in `Func.DecodeArgs` if the input is <4
  • Loading branch information
lmittmann committed Mar 12, 2023
1 parent 0966bb1 commit cc1c654
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (f *Func) EncodeArgs(args ...any) ([]byte, error) {

// DecodeArgs ABI-decodes the given input to the given args.
func (f *Func) DecodeArgs(input []byte, args ...any) error {
if len(input) < 4 {
return errors.New("w3: insufficient input length")
}
return _abi.Arguments(f.Args).Decode(input[4:], args...)
}

Expand Down
20 changes: 17 additions & 3 deletions func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package w3

import (
"bytes"
"errors"
"math/big"
"strconv"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/lmittmann/w3/internal"
"github.com/lmittmann/w3/w3types"
)

Expand Down Expand Up @@ -136,6 +138,7 @@ func TestFuncDecodeArgs(t *testing.T) {
Input []byte
Args []any
WantArgs []any
WantErr error
}{
{
Func: MustNewFunc("test(address)", ""),
Expand Down Expand Up @@ -200,15 +203,26 @@ func TestFuncDecodeArgs(t *testing.T) {
Arg1: big.NewInt(42),
}},
},
{ // https://github.com/lmittmann/w3/issues/22
Func: MustNewFunc("transfer(address recipient, uint256 amount)", "bool success"),
Input: B("0x"),
Args: []any{new(common.Address), new(big.Int)},
WantErr: errors.New("w3: insufficient input length"),
},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if err := test.Func.DecodeArgs(test.Input, test.Args...); err != nil {
t.Fatalf("Failed to decode args: %v", err)
err := test.Func.DecodeArgs(test.Input, test.Args...)
if diff := cmp.Diff(test.WantErr, err, internal.EquateErrors()); diff != "" {
t.Fatalf("Err: (-want, +got)\n%s", diff)
}
if err != nil {
return
}

if diff := cmp.Diff(test.WantArgs, test.Args, cmp.AllowUnexported(big.Int{})); diff != "" {
t.Fatalf("(-want, +got)\n%s", diff)
t.Fatalf("Args: (-want, +got)\n%s", diff)
}
})
}
Expand Down

0 comments on commit cc1c654

Please sign in to comment.