Skip to content

Commit 2e6b9c1

Browse files
committed
[release/1.4.4] accounts/abi: fixed unpacking in to already slice interfaces
Previously it was assumed that wheneven type `[]interface{}` was given that the interface was empty. The abigen rightfully assumed that interface slices which already have pre-allocated variable sets to be assigned. This PR fixes that by checking that the given `[]interface{}` is larger than zero and assigns each value using the generic `set` function (this function has also been moved to abi/reflect.go) and checks whether the assignment was possible. The generic assignment function `set` now also deals with pointers (useful for interface slice mentioned above) by dereferencing the pointer until it finds a setable type. (cherry picked from commit 91a7a4a)
1 parent b38cea6 commit 2e6b9c1

File tree

3 files changed

+94
-31
lines changed

3 files changed

+94
-31
lines changed

accounts/abi/abi.go

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,16 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) error {
238238
return fmt.Errorf("abi: unmarshalling empty output")
239239
}
240240

241-
value := reflect.ValueOf(v).Elem()
242-
typ := value.Type()
241+
// make sure the passed value is a pointer
242+
valueOf := reflect.ValueOf(v)
243+
if reflect.Ptr != valueOf.Kind() {
244+
return fmt.Errorf("abi: Unpack(non-pointer %T)", v)
245+
}
246+
247+
var (
248+
value = valueOf.Elem()
249+
typ = value.Type()
250+
)
243251

244252
if len(method.Outputs) > 1 {
245253
switch value.Kind() {
@@ -268,6 +276,25 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) error {
268276
return fmt.Errorf("abi: cannot marshal tuple in to slice %T (only []interface{} is supported)", v)
269277
}
270278

279+
// if the slice already contains values, set those instead of the interface slice itself.
280+
if value.Len() > 0 {
281+
if len(method.Outputs) > value.Len() {
282+
return fmt.Errorf("abi: cannot marshal in to slices of unequal size (require: %v, got: %v)", len(method.Outputs), value.Len())
283+
}
284+
285+
for i := 0; i < len(method.Outputs); i++ {
286+
marshalledValue, err := toGoType(i, method.Outputs[i], output)
287+
if err != nil {
288+
return err
289+
}
290+
reflectValue := reflect.ValueOf(marshalledValue)
291+
if err := set(value.Index(i).Elem(), reflectValue, method.Outputs[i]); err != nil {
292+
return err
293+
}
294+
}
295+
return nil
296+
}
297+
271298
// create a new slice and start appending the unmarshalled
272299
// values to the new interface slice.
273300
z := reflect.MakeSlice(typ, 0, len(method.Outputs))
@@ -296,34 +323,6 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) error {
296323
return nil
297324
}
298325

299-
// set attempts to assign src to dst by either setting, copying or otherwise.
300-
//
301-
// set is a bit more lenient when it comes to assignment and doesn't force an as
302-
// strict ruleset as bare `reflect` does.
303-
func set(dst, src reflect.Value, output Argument) error {
304-
dstType := dst.Type()
305-
srcType := src.Type()
306-
307-
switch {
308-
case dstType.AssignableTo(src.Type()):
309-
dst.Set(src)
310-
case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
311-
if !dstType.Elem().AssignableTo(r_byte) {
312-
return fmt.Errorf("abi: cannot unmarshal %v in to array of elem %v", src.Type(), dstType.Elem())
313-
}
314-
315-
if dst.Len() < output.Type.SliceSize {
316-
return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len())
317-
}
318-
reflect.Copy(dst, src)
319-
case dstType.Kind() == reflect.Interface:
320-
dst.Set(src)
321-
default:
322-
return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
323-
}
324-
return nil
325-
}
326-
327326
func (abi *ABI) UnmarshalJSON(data []byte) error {
328327
var fields []struct {
329328
Type string

accounts/abi/abi_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,37 @@ func TestSimpleMethodUnpack(t *testing.T) {
289289
}
290290
}
291291

292+
func TestUnpackSetInterfaceSlice(t *testing.T) {
293+
var (
294+
var1 = new(uint8)
295+
var2 = new(uint8)
296+
)
297+
out := []interface{}{var1, var2}
298+
abi, err := JSON(strings.NewReader(`[{"type":"function", "name":"ints", "outputs":[{"type":"uint8"}, {"type":"uint8"}]}]`))
299+
if err != nil {
300+
t.Fatal(err)
301+
}
302+
marshalledReturn := append(pad([]byte{1}, 32, true), pad([]byte{2}, 32, true)...)
303+
err = abi.Unpack(&out, "ints", marshalledReturn)
304+
if err != nil {
305+
t.Fatal(err)
306+
}
307+
if *var1 != 1 {
308+
t.Errorf("expected var1 to be 1, got", *var1)
309+
}
310+
if *var2 != 2 {
311+
t.Errorf("expected var2 to be 2, got", *var2)
312+
}
313+
314+
out = []interface{}{var1}
315+
err = abi.Unpack(&out, "ints", marshalledReturn)
316+
317+
expErr := "abi: cannot marshal in to slices of unequal size (require: 2, got: 1)"
318+
if err == nil || err.Error() != expErr {
319+
t.Error("expected err:", expErr, "Got:", err)
320+
}
321+
}
322+
292323
func TestPack(t *testing.T) {
293324
for i, test := range []struct {
294325
typ string

accounts/abi/reflect.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package abi
1818

19-
import "reflect"
19+
import (
20+
"fmt"
21+
"reflect"
22+
)
2023

2124
// indirect recursively dereferences the value until it either gets the value
2225
// or finds a big.Int
@@ -62,3 +65,33 @@ func mustArrayToByteSlice(value reflect.Value) reflect.Value {
6265
reflect.Copy(slice, value)
6366
return slice
6467
}
68+
69+
// set attempts to assign src to dst by either setting, copying or otherwise.
70+
//
71+
// set is a bit more lenient when it comes to assignment and doesn't force an as
72+
// strict ruleset as bare `reflect` does.
73+
func set(dst, src reflect.Value, output Argument) error {
74+
dstType := dst.Type()
75+
srcType := src.Type()
76+
77+
switch {
78+
case dstType.AssignableTo(src.Type()):
79+
dst.Set(src)
80+
case dstType.Kind() == reflect.Array && srcType.Kind() == reflect.Slice:
81+
if !dstType.Elem().AssignableTo(r_byte) {
82+
return fmt.Errorf("abi: cannot unmarshal %v in to array of elem %v", src.Type(), dstType.Elem())
83+
}
84+
85+
if dst.Len() < output.Type.SliceSize {
86+
return fmt.Errorf("abi: cannot unmarshal src (len=%d) in to dst (len=%d)", output.Type.SliceSize, dst.Len())
87+
}
88+
reflect.Copy(dst, src)
89+
case dstType.Kind() == reflect.Interface:
90+
dst.Set(src)
91+
case dstType.Kind() == reflect.Ptr:
92+
return set(dst.Elem(), src, output)
93+
default:
94+
return fmt.Errorf("abi: cannot unmarshal %v in to %v", src.Type(), dst.Type())
95+
}
96+
return nil
97+
}

0 commit comments

Comments
 (0)