Skip to content

Commit f383e06

Browse files
committed
Fix panic in checker
1 parent 87e38b1 commit f383e06

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

checker/checker.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,22 @@ func (v *visitor) checkBuiltinGet(node *ast.BuiltinNode) (reflect.Type, info) {
670670
case reflect.Interface:
671671
return anyType, info{}
672672
case reflect.Slice, reflect.Array:
673+
p, _ := v.visit(prop)
674+
if p == nil {
675+
return v.error(prop, "cannot use nil as slice index")
676+
}
677+
if !isInteger(p) && !isAny(p) {
678+
return v.error(prop, "non-integer slice index %v", p)
679+
}
673680
return t.Elem(), info{}
674681
case reflect.Map:
682+
p, _ := v.visit(prop)
683+
if p == nil {
684+
return v.error(prop, "cannot use nil as map index")
685+
}
686+
if !p.AssignableTo(t.Key()) && !isAny(p) {
687+
return v.error(prop, "cannot use %v to get an element from %v", p, t)
688+
}
675689
return t.Elem(), info{}
676690
}
677691
return v.error(val, "type %v does not support indexing", t)

compiler/compiler_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ func TestCompile_panic(t *testing.T) {
284284
tests := []string{
285285
`(TotalPosts.Profile[Authors > TotalPosts == get(nil, TotalLikes)] > Authors) ^ (TotalLikes / (Posts?.PublishDate[TotalPosts] < Posts))`,
286286
`one(Posts, nil)`,
287+
`trim(TotalViews, Posts) <= get(Authors, nil)`,
287288
}
288289
for _, test := range tests {
289290
t.Run(test, func(t *testing.T) {

0 commit comments

Comments
 (0)