mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Fix builtins checks for nil args
This commit is contained in:
+11
-4
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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:
|
||||
@@ -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:
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user