Skip to content

Commit

Permalink
Fix builtins checks for nil args
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Feb 22, 2023
1 parent 258758e commit 742c438
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
15 changes: 11 additions & 4 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var Builtins = map[int]*Function{
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for len (expected 1, got %d)", len(args))
}
switch args[0].Kind() {
switch kind(args[0]) {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String, reflect.Interface:
return integerType, nil
}
Expand All @@ -48,7 +48,7 @@ var Builtins = map[int]*Function{
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for abs (expected 1, got %d)", len(args))
}
switch args[0].Kind() {
switch kind(args[0]) {
case reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Interface:
return args[0], nil
}
Expand All @@ -62,7 +62,7 @@ var Builtins = map[int]*Function{
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for int (expected 1, got %d)", len(args))
}
switch args[0].Kind() {
switch kind(args[0]) {
case reflect.Interface:
return integerType, nil
case reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
Expand All @@ -80,7 +80,7 @@ var Builtins = map[int]*Function{
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for float (expected 1, got %d)", len(args))
}
switch args[0].Kind() {
switch kind(args[0]) {
case reflect.Interface:
return floatType, nil
case reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
Expand All @@ -92,3 +92,10 @@ var Builtins = map[int]*Function{
},
},
}

func kind(t reflect.Type) reflect.Kind {
if t == nil {
return reflect.Invalid
}
return t.Kind()
}
18 changes: 18 additions & 0 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,21 @@ func TestCheck_Function_without_types(t *testing.T) {
require.NotNil(t, tree.Node.(*ast.CallNode).Func)
require.Equal(t, "add", tree.Node.(*ast.CallNode).Func.Name)
}

func TestCheck_dont_panic_on_nil_arguments_for_builtins(t *testing.T) {
tests := []string{
"len(nil)",
"abs(nil)",
"int(nil)",
"float(nil)",
}
for _, test := range tests {
t.Run(test, func(t *testing.T) {
tree, err := parser.Parse(test)
require.NoError(t, err)

_, err = checker.Check(tree, conf.New(nil))
require.Error(t, err)
})
}
}

0 comments on commit 742c438

Please sign in to comment.