robertkrimen / otto

A JavaScript interpreter in Go (golang)
http://godoc.org/github.com/robertkrimen/otto
MIT License
8.01k stars 584 forks source link

fix: `panic: reflect.Value.MapIndex: value of type string is not assignable to type X` #499

Open mertyildiran opened 1 year ago

mertyildiran commented 1 year ago

Fixes the panic below:

panic: reflect.Value.MapIndex: value of type string is not assignable to type v1.ResourceName [recovered]
    panic: reflect.Value.MapIndex: value of type string is not assignable to type v1.ResourceName

goroutine 31 [running]:
github.com/robertkrimen/otto.catchPanic.func1()
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/error.go:252 +0x39c
panic({0x20fc040, 0xc005151dc0})
    /usr/local/go/src/runtime/panic.go:884 +0x212
reflect.Value.assignTo({0x20fc040?, 0xc005151db0?, 0x1?}, {0x256b4bb, 0x16}, 0x21ae680, 0x0)
    /usr/local/go/src/reflect/value.go:3145 +0x2a5
reflect.Value.MapIndex({0x228fde0?, 0xc00513e208?, 0x95?}, {0x20fc040, 0xc005151db0, 0x98})
    /usr/local/go/src/reflect/value.go:1666 +0xed
github.com/robertkrimen/otto.goMapGetOwnProperty(0xc005175620, {0x252f573, 0x6})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_map.go:51 +0xfb
github.com/robertkrimen/otto.(*_object).getOwnProperty(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:32
github.com/robertkrimen/otto.objectGetProperty(0xc005175620, {0x252f573, 0x6})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object_class.go:180 +0x30
github.com/robertkrimen/otto.(*_object).getProperty(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:37
github.com/robertkrimen/otto.objectGet(0xc005175620, {0x252f573?, 0x0?})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object_class.go:192 +0x2c
github.com/robertkrimen/otto.(*_object).get(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:42
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:200 +0x15e
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x22383dd, 0x6})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc0051755c0, 0x40?, 0xc005167da0)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:118
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x218cc21, 0x9})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc005138780, 0x73?, 0xc00513cc00)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:118
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:271 +0xbe5
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x2239848, 0xa})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc005067020, 0x73?, 0xc005072fa0)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:118
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x20b4673, 0x4})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc005066000, 0x40?, 0xc005061e40)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:118
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x1fa4b52, 0x3})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc00505df20, 0x73?, 0xc005061980)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:118
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0xc0003f6f90, 0xa})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goMapEnumerate(0x24a0d20?, 0x90?, 0xc004797960)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_map.go:71 +0xb9
github.com/robertkrimen/otto.(*_object).enumerate(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:118
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0xc001a7ec36, 0xa})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goMapEnumerate(0x24a0d20?, 0x0?, 0xc004797920)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_go_map.go:71 +0xb9
github.com/robertkrimen/otto.(*_object).enumerate(...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/object.go:118
github.com/robertkrimen/otto.builtinJSON_stringifyWalk({{0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0}, ...}, ...)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringify({0xc0046f8000, 0x0, 0x0, {0x5, {0x24a0d20, 0xc000e02f60}}, {0xc004e32ee8, 0x1, 0x1}, 0xc0002811d0})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/builtin_json.go:179 +0x5e5
github.com/robertkrimen/otto.(*_object).call(0xc000e02f00, {0x151f7ef?, {0x24a0d20?, 0xc000e02f60?}}, {0xc004e32ee8?, 0x1, 0x1}, 0x3?, {0x0, {0x0, ...}, ...})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_function.go:200 +0x878
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeCallExpression(0xc0046f8000, 0xc00045a720, {0x0, 0x0, 0x0})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:239 +0x3ff
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeExpression(0xc0046f8000, {0x2aa34c0?, 0xc00045a720})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:43 +0x52b
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeCallExpression(0xc0046f8000, 0xc00045a690, {0x0, 0x0, 0x0})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:188 +0x92c
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeExpression(0xc0046f8000, {0x2aa34c0?, 0xc00045a690})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:43 +0x52b
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatement(0xc0046f8000, {0x2aa3580?, 0xc00039d200})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:61 +0x3c9
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatementList(0xc0049db170?, {0xc00045a510?, 0x3, 0x1510725?})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:119 +0x95
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatement(0xc0046f8000, {0x2aa3460?, 0xc002419f08})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_statement.go:29 +0x185
github.com/robertkrimen/otto.(*_runtime).cmpl_call_nodeFunction(0xc0046f8000, 0xc000e03ec0, 0xc00051d290, 0xc0048a1c00, {0x0?, {0x203001?, 0x203001?}}, {0xc004dfc258, 0x1, 0x1})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate.go:60 +0x211
github.com/robertkrimen/otto.(*_object).call(0xc000e03ec0, {0x40f3e7?, {0x24a0d20?, 0xc00477cb40?}}, {0xc004dfc258?, 0x1, 0x1}, 0xc0?, {0x0, {0x0, ...}, ...})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/type_function.go:225 +0x27b
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeCallExpression(0xc0046f8000, 0xc00051d200, {0xc0049dbd88, 0x1, 0x1})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/cmpl_evaluate_expression.go:239 +0x3ff
github.com/robertkrimen/otto.Otto.Call.func2()
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/otto.go:556 +0x36
github.com/robertkrimen/otto.catchPanic(0xc001a7ec00?)
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/error.go:255 +0x62
github.com/robertkrimen/otto.Otto.Call({0x0?, 0xc0046f8000?}, {0x25560aa?, 0xc0049dbd50?}, {0x0, 0x0}, {0xc0049dbd88, 0x1, 0x1})
    /go/pkg/mod/github.com/robertkrimen/otto@v0.2.1/otto.go:555 +0x410
github.com/kubeshark/worker/vm.ItemCapturedHook.func1({0x20f8600?, 0x40d07e8}, {0x22bf940?, 0xc000437f80})
    /app/vm/hooks.go:27 +0x12d
sync.(*Map).Range(0x4ed6e5?, 0xc004e47ec0)
    /usr/local/go/src/sync/map.go:354 +0x2b8
github.com/kubeshark/worker/vm.Range(...)
    /app/vm/vm.go:89
github.com/kubeshark/worker/vm.ItemCapturedHook(0xc004952180)
    /app/vm/hooks.go:21 +0xc5
main.handleCapturedItems(0x0?)
    /app/main.go:133 +0x2f9
created by main.run
    /app/main.go:92 +0x26a

Minimal reproducible example:

package main

import (
    "fmt"
    "reflect"

    corev1 "k8s.io/api/core/v1"
    "k8s.io/apimachinery/pkg/api/resource"
)

func main() {
    test := corev1.ResourceList{
        corev1.ResourceCPU: resource.Quantity{},
    }
    example(reflect.ValueOf(test), "cpu")
}

func example(v reflect.Value, key string) {
    result := v.MapIndex(reflect.ValueOf(key))
    fmt.Printf("Value: %+v \n", result.Interface())
    fmt.Printf("Kind: %+v \n", result.Kind())
    fmt.Printf("Kind2: %+v \n", reflect.ValueOf(result.Interface()).Kind())
}
stevenh commented 4 months ago

@mertyildiran checking if you could add a test for this as its been a while?