Skip to content

Commit

Permalink
fixed panic by variadic for checkPropertyFetchNullSafety
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Feb 13, 2025
1 parent aa69d8b commit e84f894
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
39 changes: 30 additions & 9 deletions src/linter/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,7 @@ func (b *blockWalker) checkNullSafetyCallArgsF(args []ir.Node, fn meta.FuncInfo)
b.checkListExprNullSafety(arg, fn, i, a, haveVariadic)

case *ir.PropertyFetchExpr:
b.checkPropertyFetchNullSafety(a)
b.checkPropertyFetchNullSafety(a, fn, i, haveVariadic)
}
}
}
Expand Down Expand Up @@ -1138,7 +1138,7 @@ func (b *blockWalker) checkListExprNullSafety(arg ir.Node, fn meta.FuncInfo, par
}
}

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

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

if types.IsTypeNullable(property.Typ) {
isPrpNullable := types.IsTypeNullable(property.Typ)
if haveVariadic {
// If the parameter is outside the declared parameters, we check the latter as a variable
if paramIndex >= len(fn.Params)-1 {
lastParam := fn.Params[len(fn.Params)-1] // last param (variadic ...args)
if types.IsTypeMixed(lastParam.Typ) {
return
}

paramAllowsNull := types.IsTypeNullable(lastParam.Typ)
if isPrpNullable && !paramAllowsNull {
b.report(expr, LevelError, "notNullSafety",
"potential null dereference when accessing property '%s'", prp.Value)
}
return

}
}

paramAllowsNull := types.IsTypeNullable(fn.Params[paramIndex].Typ)

if isPrpNullable && !paramAllowsNull {
b.report(expr, LevelError, "notNullSafety",
"potential null dereference when accessing property '%s'", prp.Value)
}

println(classInfo.Name)
}

println(classInfo.Name)
// TODO: check difficult chains like $a->b->c->d
/* if nestedProp, ok := expr.Variable.(*ir.PropertyFetchExpr); ok {
b.checkPropertyFetchNullSafety(nestedProp)
}*/
}

// TODO: check difficult chains like $a->b->c->d
/* if nestedProp, ok := expr.Variable.(*ir.PropertyFetchExpr); ok {
b.checkPropertyFetchNullSafety(nestedProp)
}*/
}

func (b *blockWalker) handleCallArgs(args []ir.Node, fn meta.FuncInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func IsAlias(s string) bool {
func IsTypeNullable(typ Map) bool {
isNullable := false
typ.Iterate(func(t string) {
if t == "null" {
if t == "null" || strings.Contains(t, "mixed") {
isNullable = true
}
})
Expand Down

0 comments on commit e84f894

Please sign in to comment.