dotnet / dotnet/roslyn

`MethodTypeInferrer.Fix` has potential exposure to `null` compilation

Open
#80,658 1 comment 0 reactions 1 assignee Claimed by @jcouv View on GitHub
Area-Compilers Concept-Design Debt
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

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 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 https://github.com/dotnet/roslyn/pull/80659

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.