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

INTERNAL ERROR: (CRASH) interface conversion: interface {} is int, not float64 #752

Closed
suzuki-shunsuke opened this issue Jun 3, 2024 · 1 comment · Fixed by #753
Closed

Comments

@suzuki-shunsuke
Copy link
Contributor

The following native function returns an array []any.
If the array includes an int value, go-jsonnet causes a panic.

How to reproduce

https://go.dev/play/p/ewpsw0iI21j

package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/google/go-jsonnet"
	"github.com/google/go-jsonnet/ast"
)

func main() {
	if err := core(); err != nil {
		log.Fatal(err)
	}
}

func count() *jsonnet.NativeFunction {
	return &jsonnet.NativeFunction{
		Name:   "strings.count",
		Params: ast.Identifiers{"s", "substr"},
		Func: func(s []interface{}) (interface{}, error) {
			return []any{strings.Count(s[0].(string), s[1].(string)), nil}, nil
		},
	}
}

func core() error {
	vm := jsonnet.MakeVM()
	vm.NativeFunction(count())
	code := `std.native("strings.count")("hello", "l")`
	result, err := vm.EvaluateAnonymousSnippet("test.jsonnet", code)
	if err != nil {
		return err
	}
	fmt.Println(result)
	return nil
}
$ go run main.go
2009/11/10 23:00:00 INTERNAL ERROR: (CRASH) interface conversion: interface {} is int, not float64
goroutine 1 [running]:
runtime/debug.Stack()
	/usr/local/go-faketime/src/runtime/debug/stack.go:24 +0x5e
github.com/google/go-jsonnet.(*VM).evaluateSnippet.func1()
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/vm.go:214 +0x3d
panic({0x57b260?, 0xc0000a3ad0?})
	/usr/local/go-faketime/src/runtime/panic.go:770 +0x132
github.com/google/go-jsonnet.jsonToValue(0xc0000c75e0, {0x5709a0?, 0x6dc7d0?})
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/interpreter.go:953 +0x417
github.com/google/go-jsonnet.jsonToValue(0xc0000c75e0, {0x56f420?, 0xc0000b4cc0?})
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/interpreter.go:942 +0x772
github.com/google/go-jsonnet.(*NativeFunction).evalCall(0xc0000a33b0, {{0xc000097330, 0x2, 0x2}, {0x942c20, 0x0, 0x0}, 0x0}, 0xc0000c75e0)
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/thunks.go:290 +0x5b1
github.com/google/go-jsonnet.(*valueFunction).call(0xc000097320, 0xc0000c75e0, {{0xc000097330, 0x2, 0x2}, {0x942c20, 0x0, 0x0}, 0x0})
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/value.go:349 +0x127
github.com/google/go-jsonnet.(*interpreter).evaluateTailCall(0x5fc2c0?, 0xc000100100?, {{0xc000097330, 0x2, 0x2}, {0x942c20, 0x0, 0x0}, 0x0}, 0x1)
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/interpreter.go:1001 +0x9d
github.com/google/go-jsonnet.(*interpreter).evaluate(0xc0000c75e0, {0x5fc2c0, 0xc000100100}, 0x1)
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/interpreter.go:593 +0x26a5
github.com/google/go-jsonnet.(*interpreter).EvalInCleanEnv(0xc0000c75e0, 0x0?, {0x5fc2c0, 0xc000100100}, 0x0?)
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/interpreter.go:983 +0x6b
github.com/google/go-jsonnet.evaluateAux(0xc0000c75e0, {0x5fc2c0, 0xc000100100}, 0xc0000a3290)
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/interpreter.go:1290 +0x147
github.com/google/go-jsonnet.evaluate({0x5fc2c0, 0xc000100100}, 0x5fb1f8?, 0xc0000a3290, 0xc0000cfdb8?, 0x40dcdb?, 0x5fb1f8?, {0x5faab8?, 0xc0000a8010?}, 0x0)
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/interpreter.go:1325 +0x7c
github.com/google/go-jsonnet.(*VM).evaluateSnippet(0xc000177ea8, {0x59e4bd?, 0x30?}, {0x0?, 0x8e4388?}, {0x5acc16?, 0x40eae5?}, 0x0)
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/vm.go:223 +0x1d2
github.com/google/go-jsonnet.(*VM).EvaluateAnonymousSnippet(0xc000177ea8, {0x59e4bd?, 0x59e9a9?}, {0x5acc16?, 0x550011?})
	/tmp/gopath1748081828/pkg/mod/github.com/google/go-jsonnet@v0.20.0/vm.go:371 +0x33
main.core()
	/tmp/sandbox3542272284/prog.go:32 +0x305
main.main()
	/tmp/sandbox3542272284/prog.go:13 +0x13

Please report a bug here: https://github.com/google/go-jsonnet/issues
@suzuki-shunsuke
Copy link
Contributor Author

suzuki-shunsuke commented Jun 3, 2024

I created a pull request to resolve this issue.

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 a pull request may close this issue.

1 participant