Skip to content

MethodTypeInferrer.Fix has potential exposure to null compilation #80658

@jcouv

Description

@jcouv

After annotating MethodTypeInferrer._compilation, it became clear that one of the usage sites is not prepared to deal with null, which could lead to a null dereference.
In practice, I don't think this is reachable at the moment, because a null compilation is used when reducing extension methods and new extension members, and I don't think a function type could contribute to the inference in such a case. But we should confirm and strengthen this code.

            bool fromFunctionType = false;
            if (isFunctionType(best, out var functionType))
            {
                // Realize the type as TDelegate, or Expression<TDelegate> if the type parameter
                // is constrained to System.Linq.Expressions.Expression.
                var resultType = functionType.GetInternalDelegateType();
                if (hasExpressionTypeConstraint(typeParameter))
                {
                    var expressionOfTType = compilation.GetWellKnownType(WellKnownType.System_Linq_Expressions_Expression_T);
                    resultType = expressionOfTType.Construct(resultType);
                }
                best = TypeWithAnnotations.Create(resultType, best.NullableAnnotation);
                fromFunctionType = true;
            }

Found in #80659

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions