Skip to content

Commit

Permalink
Fix panic in checker
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Aug 17, 2023
1 parent 931dbf3 commit 39c9df6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func (v *visitor) MemberNode(node *ast.MemberNode) (reflect.Type, info) {
func (v *visitor) SliceNode(node *ast.SliceNode) (reflect.Type, info) {
t, _ := v.visit(node.Node)

switch t.Kind() {
switch kind(t) {
case reflect.Interface:
// ok
case reflect.String, reflect.Array, reflect.Slice:
Expand Down Expand Up @@ -532,7 +532,7 @@ func (v *visitor) CallNode(node *ast.CallNode) (reflect.Type, info) {
fn.NumOut() == 1 &&
fn.Out(0).Kind() == reflect.Interface {
rest := fn.In(fn.NumIn() - 1) // function has only one param for functions and two for methods
if rest.Kind() == reflect.Slice && rest.Elem().Kind() == reflect.Interface {
if kind(rest) == reflect.Slice && rest.Elem().Kind() == reflect.Interface {
node.Fast = true
}
}
Expand Down Expand Up @@ -666,7 +666,7 @@ func (v *visitor) checkBuiltinGet(node *ast.BuiltinNode) (reflect.Type, info) {

t, _ := v.visit(val)

switch t.Kind() {
switch kind(t) {
case reflect.Interface:
return anyType, info{}
case reflect.Slice, reflect.Array:
Expand Down Expand Up @@ -791,7 +791,7 @@ func (v *visitor) checkArguments(name string, fn reflect.Type, method bool, argu
continue
}

if !t.AssignableTo(in) && t.Kind() != reflect.Interface {
if !t.AssignableTo(in) && kind(t) != reflect.Interface {
return anyType, &file.Error{
Location: arg.Location(),
Message: fmt.Sprintf("cannot use %v as argument (type %v) to call %v ", t, in, name),
Expand Down
7 changes: 7 additions & 0 deletions checker/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ func deref(t reflect.Type) reflect.Type {
return t
}

func kind(t reflect.Type) reflect.Kind {
if t == nil {
return reflect.Invalid
}
return t.Kind()
}

func isIntegerOrArithmeticOperation(node ast.Node) bool {
switch n := node.(type) {
case *ast.IntegerNode:
Expand Down
13 changes: 13 additions & 0 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/antonmedv/expr"
"github.com/antonmedv/expr/test/playground"
"github.com/antonmedv/expr/vm"
"github.com/antonmedv/expr/vm/runtime"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -278,3 +279,15 @@ func TestCompile(t *testing.T) {
assert.Equal(t, test.program.Disassemble(), program.Disassemble(), test.input)
}
}

func TestCompile_panic(t *testing.T) {
tests := []string{
`(TotalPosts.Profile[Authors > TotalPosts == get(nil, TotalLikes)] > Authors) ^ (TotalLikes / (Posts?.PublishDate[TotalPosts] < Posts))`,
}
for _, test := range tests {
t.Run(test, func(t *testing.T) {
_, err := expr.Compile(test, expr.Env(playground.Blog{}))
require.Error(t, err)
})
}
}

0 comments on commit 39c9df6

Please sign in to comment.