Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 10 additions & 2 deletions vm/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ func Fetch(from, i any) any {
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() {
panic(fmt.Sprintf("cannot use %s as a map key: type is not comparable", key.Type()))
}
value = v.MapIndex(key)
}
if value.IsValid() {
return value.Interface()
Expand Down Expand Up @@ -233,7 +237,11 @@ func In(needle any, array any) bool {
if needle == nil {
value = v.MapIndex(reflect.Zero(v.Type().Key()))
} else {
value = v.MapIndex(reflect.ValueOf(needle))
key := reflect.ValueOf(needle)
if !key.Type().Comparable() {
panic(fmt.Sprintf("cannot use %s as a map key: type is not comparable", key.Type()))
}
value = v.MapIndex(key)
}
if value.IsValid() {
return true
Expand Down
17 changes: 17 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,23 @@ func TestVM_GroupAndSortOperations(t *testing.T) {
}
}

// TestVM_NonComparableMapKey checks that indexing a map or using the in
// operator with a non-comparable key returns a clean error instead of a raw
// "hash of unhashable type" runtime panic.
func TestVM_NonComparableMapKey(t *testing.T) {
env := map[string]any{"list": []map[string]any{{"a": 1}}}
for _, code := range []string{
`groupBy(list, 0)[reduce(list, B"")]`,
`B"" in groupBy(list, 0)`,
} {
program, err := expr.Compile(code, expr.Env(env))
require.NoError(t, err)
_, err = vm.Run(program, env)
require.Error(t, err)
require.Contains(t, err.Error(), "not comparable")
}
}

// TestVM_SortBy_NonStringOrder tests that sortBy with non-string order
// returns a proper error instead of panicking (regression test for OSS-Fuzz #477658245).
func TestVM_SortBy_NonStringOrder(t *testing.T) {
Expand Down