jeudi 28 février 2019

Why does serializing Func with Newtonsoft result in stackoverflow exception? (Seems to be an issue in .Equals check on reflection object)

Problem

I'm currently working on creating an application. In this application I was working with serializing a Func. This somehow crashed my application without an exception.

Crashing without an exception made me curious on wtf is going on so I did some deep diving and after some digging finally found out that somewhere within Newtonsoft.Json a List.Contains is happening which is then executing an equals check on 2 properties.

Apparently in this equals check an infinite loop happens which causes a stackoverflow exception.

Reproduce the issue with Newtonsoft.Json

Expression<Func<string, int>> expr = (t) => t.Length;
Func<string, int> exprCompiled = expr.Compile();

var res = JsonConvert.SerializeObject(exprCompiled);

Reproducing the issue with just C#

Expression<Func<string, int>> expr = (t) => t.Length;
Func<string, int> exprCompiled = expr.Compile();

var aa = exprCompiled.Method.Module;
var bb = exprCompiled.Method.Module.Assembly;

if (aa.Equals(bb))
{
    Console.WriteLine("Now");
}

:)

Not sure if this is an issue with the .NET framework or something else, but it was a fun trip down the caves of C# none the less.





Aucun commentaire:

Enregistrer un commentaire