Skip to content

Commit e84f894

Browse files
committed
fixed panic by variadic for checkPropertyFetchNullSafety
1 parent aa69d8b commit e84f894

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/linter/block.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ func (b *blockWalker) checkNullSafetyCallArgsF(args []ir.Node, fn meta.FuncInfo)
10511051
b.checkListExprNullSafety(arg, fn, i, a, haveVariadic)
10521052

10531053
case *ir.PropertyFetchExpr:
1054-
b.checkPropertyFetchNullSafety(a)
1054+
b.checkPropertyFetchNullSafety(a, fn, i, haveVariadic)
10551055
}
10561056
}
10571057
}
@@ -1138,7 +1138,7 @@ func (b *blockWalker) checkListExprNullSafety(arg ir.Node, fn meta.FuncInfo, par
11381138
}
11391139
}
11401140

1141-
func (b *blockWalker) checkPropertyFetchNullSafety(expr *ir.PropertyFetchExpr) {
1141+
func (b *blockWalker) checkPropertyFetchNullSafety(expr *ir.PropertyFetchExpr, fn meta.FuncInfo, paramIndex int, haveVariadic bool) {
11421142
objVar, ok := expr.Variable.(*ir.SimpleVar)
11431143

11441144
if ok {
@@ -1150,19 +1150,40 @@ func (b *blockWalker) checkPropertyFetchNullSafety(expr *ir.PropertyFetchExpr) {
11501150
if okPrp {
11511151
property := classInfo.Properties[prp.Value]
11521152

1153-
if types.IsTypeNullable(property.Typ) {
1153+
isPrpNullable := types.IsTypeNullable(property.Typ)
1154+
if haveVariadic {
1155+
// If the parameter is outside the declared parameters, we check the latter as a variable
1156+
if paramIndex >= len(fn.Params)-1 {
1157+
lastParam := fn.Params[len(fn.Params)-1] // last param (variadic ...args)
1158+
if types.IsTypeMixed(lastParam.Typ) {
1159+
return
1160+
}
1161+
1162+
paramAllowsNull := types.IsTypeNullable(lastParam.Typ)
1163+
if isPrpNullable && !paramAllowsNull {
1164+
b.report(expr, LevelError, "notNullSafety",
1165+
"potential null dereference when accessing property '%s'", prp.Value)
1166+
}
1167+
return
1168+
1169+
}
1170+
}
1171+
1172+
paramAllowsNull := types.IsTypeNullable(fn.Params[paramIndex].Typ)
1173+
1174+
if isPrpNullable && !paramAllowsNull {
11541175
b.report(expr, LevelError, "notNullSafety",
11551176
"potential null dereference when accessing property '%s'", prp.Value)
11561177
}
1178+
1179+
println(classInfo.Name)
11571180
}
11581181

1159-
println(classInfo.Name)
1182+
// TODO: check difficult chains like $a->b->c->d
1183+
/* if nestedProp, ok := expr.Variable.(*ir.PropertyFetchExpr); ok {
1184+
b.checkPropertyFetchNullSafety(nestedProp)
1185+
}*/
11601186
}
1161-
1162-
// TODO: check difficult chains like $a->b->c->d
1163-
/* if nestedProp, ok := expr.Variable.(*ir.PropertyFetchExpr); ok {
1164-
b.checkPropertyFetchNullSafety(nestedProp)
1165-
}*/
11661187
}
11671188

11681189
func (b *blockWalker) handleCallArgs(args []ir.Node, fn meta.FuncInfo) {

src/types/predicates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func IsAlias(s string) bool {
4040
func IsTypeNullable(typ Map) bool {
4141
isNullable := false
4242
typ.Iterate(func(t string) {
43-
if t == "null" {
43+
if t == "null" || strings.Contains(t, "mixed") {
4444
isNullable = true
4545
}
4646
})

0 commit comments

Comments
 (0)