diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 0d0dec35..89cebab3 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -271,6 +271,7 @@ func TestBuiltin_errors(t *testing.T) { {`date("error")`, `invalid date`}, {`get()`, `invalid number of arguments (expected 2, got 0)`}, {`get(1, 2)`, `type int does not support indexing`}, + {`get(groupBy([1, 2, 3], # % 2), [1])`, `cannot use []interface {} as a map key: type is not comparable`}, {`bitnot("1")`, "cannot use string as argument (type int) to call bitnot (1:8)"}, {`bitand("1", 1)`, "cannot use string as argument (type int) to call bitand (1:8)"}, {`"10" | bitor(1)`, "cannot use string as argument (type int) to call bitor (1:1)"}, diff --git a/builtin/lib.go b/builtin/lib.go index 61748da0..c5ad5485 100644 --- a/builtin/lib.go +++ b/builtin/lib.go @@ -588,7 +588,11 @@ func get(params ...any) (out any, err error) { if i == nil { value = v.MapIndex(reflect.Zero(v.Type().Key())) } else { - value = v.MapIndex(reflect.ValueOf(i)) + key := reflect.ValueOf(i) + if !key.Type().Comparable() { + return nil, fmt.Errorf("cannot use %s as a map key: type is not comparable", key.Type()) + } + value = v.MapIndex(key) } if value.IsValid() { return value.Interface(), nil diff --git a/test/fuzz/fuzz_test.go b/test/fuzz/fuzz_test.go index e12c4d8e..79f9cedd 100644 --- a/test/fuzz/fuzz_test.go +++ b/test/fuzz/fuzz_test.go @@ -66,6 +66,7 @@ func FuzzExpr(f *testing.F) { regexp.MustCompile(`invalid order .*, expected asc or desc`), regexp.MustCompile(`unknown order, use asc or desc`), regexp.MustCompile(`cannot use .* as a key for groupBy: type is not comparable`), + regexp.MustCompile(`cannot use .* as a map key: type is not comparable`), } env := NewEnv()