Skip to content

Commit 7f07463

Browse files
authored
Fix Fetch and OpDeref (#467)
* Fix OpDeref allowing to deref pointer of interfaces Loop over `Ptr` and `Interface` kind and get `Elem`. Also return directly the `nil` otherwise we might are returning the pointer nil value and not the base nil. * Fix deref interface in Fetch We were only de-referencing pointers, but we can have pointer of interface or interface obfuscating pointers... So before trying to fetch anything, just deference fully the variable.
1 parent e3c2d0e commit 7f07463

File tree

3 files changed

+90
-20
lines changed

3 files changed

+90
-20
lines changed

expr_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,21 @@ func TestExpr_fetch_from_func(t *testing.T) {
12041204
assert.Contains(t, err.Error(), "cannot fetch Value from func()")
12051205
}
12061206

1207+
func TestExpr_fetch_from_interface(t *testing.T) {
1208+
type FooBar struct {
1209+
Value string
1210+
}
1211+
foobar := &FooBar{"waldo"}
1212+
var foobarAny any = foobar
1213+
var foobarPtrAny any = &foobarAny
1214+
1215+
res, err := expr.Eval("foo.Value", map[string]any{
1216+
"foo": foobarPtrAny,
1217+
})
1218+
assert.NoError(t, err)
1219+
assert.Equal(t, "waldo", res)
1220+
}
1221+
12071222
func TestExpr_map_default_values(t *testing.T) {
12081223
env := map[string]any{
12091224
"foo": map[string]string{},

test/deref/deref_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,66 @@ func TestDeref_multiple_pointers(t *testing.T) {
137137
require.Equal(t, 44, output)
138138
})
139139
}
140+
141+
func TestDeref_pointer_of_interface(t *testing.T) {
142+
v := 42
143+
a := &v
144+
b := any(a)
145+
c := any(&b)
146+
t.Run("returned as is", func(t *testing.T) {
147+
output, err := expr.Eval(`c`, map[string]any{
148+
"c": c,
149+
})
150+
require.NoError(t, err)
151+
require.Equal(t, c, output)
152+
require.IsType(t, (*interface{})(nil), output)
153+
})
154+
t.Run("+ works", func(t *testing.T) {
155+
output, err := expr.Eval(`c+2`, map[string]any{
156+
"c": c,
157+
})
158+
require.NoError(t, err)
159+
require.Equal(t, 44, output)
160+
})
161+
}
162+
163+
func TestDeref_nil(t *testing.T) {
164+
var b *int = nil
165+
c := &b
166+
t.Run("returned as is", func(t *testing.T) {
167+
output, err := expr.Eval(`c`, map[string]any{
168+
"c": c,
169+
})
170+
require.NoError(t, err)
171+
require.Equal(t, c, output)
172+
require.IsType(t, (**int)(nil), output)
173+
})
174+
t.Run("== nil works", func(t *testing.T) {
175+
output, err := expr.Eval(`c == nil`, map[string]any{
176+
"c": c,
177+
})
178+
require.NoError(t, err)
179+
require.Equal(t, true, output)
180+
})
181+
}
182+
183+
func TestDeref_nil_in_pointer_of_interface(t *testing.T) {
184+
var a *int32 = nil
185+
b := any(a)
186+
c := any(&b)
187+
t.Run("returned as is", func(t *testing.T) {
188+
output, err := expr.Eval(`c`, map[string]any{
189+
"c": c,
190+
})
191+
require.NoError(t, err)
192+
require.Equal(t, c, output)
193+
require.IsType(t, (*interface{})(nil), output)
194+
})
195+
t.Run("== nil works", func(t *testing.T) {
196+
output, err := expr.Eval(`c == nil`, map[string]any{
197+
"c": c,
198+
})
199+
require.NoError(t, err)
200+
require.Equal(t, true, output)
201+
})
202+
}

vm/runtime/runtime.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import (
88
"reflect"
99
)
1010

11+
func deref(kind reflect.Kind, value reflect.Value) (reflect.Kind, reflect.Value) {
12+
for kind == reflect.Ptr || kind == reflect.Interface {
13+
value = value.Elem()
14+
kind = value.Kind()
15+
}
16+
return kind, value
17+
}
18+
1119
func Fetch(from, i any) any {
1220
v := reflect.ValueOf(from)
1321
kind := v.Kind()
@@ -28,10 +36,8 @@ func Fetch(from, i any) any {
2836
// Structs, maps, and slices can be access through a pointer or through
2937
// a value, when they are accessed through a pointer we don't want to
3038
// copy them to a value.
31-
if kind == reflect.Ptr {
32-
v = reflect.Indirect(v)
33-
kind = v.Kind()
34-
}
39+
// De-reference everything if necessary (interface and pointers)
40+
kind, v = deref(kind, v)
3541

3642
// TODO: We can create separate opcodes for each of the cases below to make
3743
// the little bit faster.
@@ -145,27 +151,13 @@ func Deref(i any) any {
145151

146152
v := reflect.ValueOf(i)
147153

148-
if v.Kind() == reflect.Interface {
154+
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
149155
if v.IsNil() {
150-
return i
156+
return nil
151157
}
152158
v = v.Elem()
153159
}
154160

155-
loop:
156-
for v.Kind() == reflect.Ptr {
157-
if v.IsNil() {
158-
return i
159-
}
160-
indirect := reflect.Indirect(v)
161-
switch indirect.Kind() {
162-
case reflect.Struct, reflect.Map, reflect.Array, reflect.Slice:
163-
break loop
164-
default:
165-
v = v.Elem()
166-
}
167-
}
168-
169161
if v.IsValid() {
170162
return v.Interface()
171163
}

0 commit comments

Comments
 (0)