Browse Source

TransformExpressionTrees: do not crash on a lambda that did not convert

The builder returned by ConvertLambda hands back null when a nested
conversion declines, and the result was cast and dereferenced before
anything checked it, so a tree the transform cannot handle took down the
whole method with a NullReferenceException instead of being left alone.

EF Core's StringCharConverter.ToChar is such a tree: the conditional
spills the Expression.Call arguments into stack slots, which
MatchGetMethodFromHandle does not see through.

Assisted-by: Claude:claude-opus-5:Claude Code
pull/4091/head
Siegfried Pammer 1 week ago
parent
commit
bfa3f18980
  1. 7
      ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs

7
ICSharpCode.Decompiler/IL/Transforms/TransformExpressionTrees.cs

@ -171,8 +171,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms @@ -171,8 +171,11 @@ namespace ICSharpCode.Decompiler.IL.Transforms
if (lambda != null)
{
context.Step("Convert Expression Tree", instruction);
var newLambda = (ILFunction)lambda();
if (newLambda == null)
// A builder returns null where a nested conversion declined; the cast has to
// tolerate that, and anything that is not an ILFunction is no lambda to put
// in the call's place either. The tree then stays as the Expression calls
// that built it.
if (lambda() is not ILFunction newLambda)
return false;
SetExpressionTreeFlag(newLambda, (CallInstruction)instruction);
instruction.ReplaceWith(newLambda);

Loading…
Cancel
Save