Skip to content

Commit f388f6c

Browse files
committed
fix: NPE due to checking if error is nil when it can be a value
Fixes #468
1 parent c90c673 commit f388f6c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

callbacks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func callFunction(f reflect.Value, bindings bindings) error {
8181
return err
8282
}
8383
ferr := out[0]
84-
if ferrv := reflect.ValueOf(ferr); !ferrv.IsValid() || ferrv.IsNil() {
84+
if ferrv := reflect.ValueOf(ferr); !ferrv.IsValid() || ((ferrv.Kind() == reflect.Interface || ferrv.Kind() == reflect.Pointer) && ferrv.IsNil()) {
8585
return nil
8686
}
8787
return ferr.(error) //nolint:forcetypeassert

options_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,22 @@ func TestFlagNamer(t *testing.T) {
127127
assert.NoError(t, err)
128128
assert.Equal(t, "SOMEFLAG", app.Model.Flags[1].Name)
129129
}
130+
131+
type npError string
132+
133+
func (e npError) Error() string {
134+
return "ERROR: " + string(e)
135+
}
136+
137+
func TestCallbackNonPointerError(t *testing.T) {
138+
method := func() error {
139+
return npError("failed")
140+
}
141+
142+
var cli struct{}
143+
144+
p, err := New(&cli)
145+
assert.NoError(t, err)
146+
err = callFunction(reflect.ValueOf(method), p.bindings)
147+
assert.EqualError(t, err, "ERROR: failed")
148+
}

0 commit comments

Comments
 (0)