Skip to content

Commit 742c438

Browse files
committed
Fix builtins checks for nil args
1 parent 258758e commit 742c438

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

builtin/builtin.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var Builtins = map[int]*Function{
3434
if len(args) != 1 {
3535
return anyType, fmt.Errorf("invalid number of arguments for len (expected 1, got %d)", len(args))
3636
}
37-
switch args[0].Kind() {
37+
switch kind(args[0]) {
3838
case reflect.Array, reflect.Map, reflect.Slice, reflect.String, reflect.Interface:
3939
return integerType, nil
4040
}
@@ -48,7 +48,7 @@ var Builtins = map[int]*Function{
4848
if len(args) != 1 {
4949
return anyType, fmt.Errorf("invalid number of arguments for abs (expected 1, got %d)", len(args))
5050
}
51-
switch args[0].Kind() {
51+
switch kind(args[0]) {
5252
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:
5353
return args[0], nil
5454
}
@@ -62,7 +62,7 @@ var Builtins = map[int]*Function{
6262
if len(args) != 1 {
6363
return anyType, fmt.Errorf("invalid number of arguments for int (expected 1, got %d)", len(args))
6464
}
65-
switch args[0].Kind() {
65+
switch kind(args[0]) {
6666
case reflect.Interface:
6767
return integerType, nil
6868
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:
@@ -80,7 +80,7 @@ var Builtins = map[int]*Function{
8080
if len(args) != 1 {
8181
return anyType, fmt.Errorf("invalid number of arguments for float (expected 1, got %d)", len(args))
8282
}
83-
switch args[0].Kind() {
83+
switch kind(args[0]) {
8484
case reflect.Interface:
8585
return floatType, nil
8686
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:
@@ -92,3 +92,10 @@ var Builtins = map[int]*Function{
9292
},
9393
},
9494
}
95+
96+
func kind(t reflect.Type) reflect.Kind {
97+
if t == nil {
98+
return reflect.Invalid
99+
}
100+
return t.Kind()
101+
}

checker/checker_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -875,3 +875,21 @@ func TestCheck_Function_without_types(t *testing.T) {
875875
require.NotNil(t, tree.Node.(*ast.CallNode).Func)
876876
require.Equal(t, "add", tree.Node.(*ast.CallNode).Func.Name)
877877
}
878+
879+
func TestCheck_dont_panic_on_nil_arguments_for_builtins(t *testing.T) {
880+
tests := []string{
881+
"len(nil)",
882+
"abs(nil)",
883+
"int(nil)",
884+
"float(nil)",
885+
}
886+
for _, test := range tests {
887+
t.Run(test, func(t *testing.T) {
888+
tree, err := parser.Parse(test)
889+
require.NoError(t, err)
890+
891+
_, err = checker.Check(tree, conf.New(nil))
892+
require.Error(t, err)
893+
})
894+
}
895+
}

0 commit comments

Comments
 (0)