rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
225 stars 29 forks source link

Infinite loop on incorrect structure deserialization #217

Closed heremit closed 5 years ago

heremit commented 5 years ago

NetJSON up to 1.2.10.2 hangs if object is expected but integer given. Expected behavior: exception or current key ignoring. Code for testing:

public static class TestJsonBug
{
    public static void TestBug()
    {
        //works OK
        var goodJson = "{  \"Id\": 31,  \"SubStruct\":  {    \"Id\": 13,    \"SomeDate\": \"2018-10-19T12:23:55.1081550\"  },  \"SomeString\": \"My test string\"}";
        var myStruct = NetJSON.NetJSON.Deserialize<MySuperStruct>(goodJson);
        Console.WriteLine($"Object is null?: {myStruct == null}, Date: {myStruct?.SubStruct.SomeDate}");

        //hangs
        var badJson = "{  \"Id\": 31,  \"SubStruct\":  1,  \"SomeString\": \"My test string\"}";
        myStruct = NetJSON.NetJSON.Deserialize<MySuperStruct>(badJson);
        Console.WriteLine($"Object is null?: {myStruct == null}, Date: {myStruct?.SubStruct.SomeDate}");
    }
}

public class MySuperStruct {
    public long Id { get; set; }
    public MySubStruct SubStruct { get; set; }
    public string SomeString { get; set; }
}

public class MySubStruct
{
    public long Id { get; set; }
    public DateTime SomeDate { get; set; }
}

In example "SubStruct" should be an object, but integer given. Of course it's an error in data but infinite loop should not occur anyway.

rpgmaker commented 5 years ago

Thanks for the contribution. I will look into it. I currently don't verify type as the focus was on performance.

rpgmaker commented 5 years ago

Note, I am only going to support this for collections and class/struct. Type mismatch for placing integer vs string will not be handled since this greatly impact overall performance