Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mertyildiran
Copy link

@mertyildiran mertyildiran commented Jun 16, 2023

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/[email protected]/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/[email protected]/type_go_map.go:51 +0xfb
github.com/robertkrimen/otto.(*_object).getOwnProperty(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/object.go:32
github.com/robertkrimen/otto.objectGetProperty(0xc005175620, {0x252f573, 0x6})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/object_class.go:180 +0x30
github.com/robertkrimen/otto.(*_object).getProperty(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/object.go:37
github.com/robertkrimen/otto.objectGet(0xc005175620, {0x252f573?, 0x0?})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/object_class.go:192 +0x2c
github.com/robertkrimen/otto.(*_object).get(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/builtin_json.go:200 +0x15e
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x22383dd, 0x6})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc0051755c0, 0x40?, 0xc005167da0)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x218cc21, 0x9})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc005138780, 0x73?, 0xc00513cc00)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/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/[email protected]/builtin_json.go:271 +0xbe5
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x2239848, 0xa})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc005067020, 0x73?, 0xc005072fa0)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x20b4673, 0x4})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc005066000, 0x40?, 0xc005061e40)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0x1fa4b52, 0x3})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goStructEnumerate(0xc00505df20, 0x73?, 0xc005061980)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/type_go_struct.go:104 +0x1d7
github.com/robertkrimen/otto.(*_object).enumerate(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0xc0003f6f90, 0xa})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goMapEnumerate(0x24a0d20?, 0x90?, 0xc004797960)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/type_go_map.go:71 +0xb9
github.com/robertkrimen/otto.(*_object).enumerate(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/builtin_json.go:287 +0x9f2
github.com/robertkrimen/otto.builtinJSON_stringifyWalk.func2({0xc001a7ec36, 0xa})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/builtin_json.go:288 +0x78
github.com/robertkrimen/otto.goMapEnumerate(0x24a0d20?, 0x0?, 0xc004797920)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/type_go_map.go:71 +0xb9
github.com/robertkrimen/otto.(*_object).enumerate(...)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/type_function.go:200 +0x878
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeCallExpression(0xc0046f8000, 0xc00045a720, {0x0, 0x0, 0x0})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/cmpl_evaluate_expression.go:239 +0x3ff
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeExpression(0xc0046f8000, {0x2aa34c0?, 0xc00045a720})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/cmpl_evaluate_expression.go:188 +0x92c
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeExpression(0xc0046f8000, {0x2aa34c0?, 0xc00045a690})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/cmpl_evaluate_expression.go:43 +0x52b
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatement(0xc0046f8000, {0x2aa3580?, 0xc00039d200})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/cmpl_evaluate_statement.go:61 +0x3c9
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatementList(0xc0049db170?, {0xc00045a510?, 0x3, 0x1510725?})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/cmpl_evaluate_statement.go:119 +0x95
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeStatement(0xc0046f8000, {0x2aa3460?, 0xc002419f08})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/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/[email protected]/type_function.go:225 +0x27b
github.com/robertkrimen/otto.(*_runtime).cmpl_evaluate_nodeCallExpression(0xc0046f8000, 0xc00051d200, {0xc0049dbd88, 0x1, 0x1})
	/go/pkg/mod/github.com/robertkrimen/[email protected]/cmpl_evaluate_expression.go:239 +0x3ff
github.com/robertkrimen/otto.Otto.Call.func2()
	/go/pkg/mod/github.com/robertkrimen/[email protected]/otto.go:556 +0x36
github.com/robertkrimen/otto.catchPanic(0xc001a7ec00?)
	/go/pkg/mod/github.com/robertkrimen/[email protected]/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/[email protected]/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())
}

Copy link
Collaborator

@stevenh stevenh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mertyildiran fix looks good could you add at test to valid this please.

@stevenh
Copy link
Collaborator

stevenh commented May 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants