[wasm] MonoProxy.cs: Avoid some errors by pre-emptively checking (#67243)
authorAnkit Jain <radical@gmail.com>
Mon, 28 Mar 2022 19:51:01 +0000 (15:51 -0400)
committerGitHub <noreply@github.com>
Mon, 28 Mar 2022 19:51:01 +0000 (15:51 -0400)
.. inspired by https://github.com/dotnet/runtime/issues/66149 .

src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs

index 52f3f65..4140202 100644 (file)
@@ -89,17 +89,22 @@ namespace Microsoft.WebAssembly.Diagnostics
                         if (type == "debug")
                         {
                             JToken a = args["args"];
-                            if (a?[0]?["value"]?.ToString() == MonoConstants.RUNTIME_IS_READY &&
-                                a?[1]?["value"]?.ToString() == "fe00e07a-5519-4dfe-b35a-f867dbaf2e28")
+                            if (a is null)
+                                break;
+
+                            int aCount = a.Count();
+                            if (aCount >= 2 &&
+                                a[0]?["value"]?.ToString() == MonoConstants.RUNTIME_IS_READY &&
+                                a[1]?["value"]?.ToString() == "fe00e07a-5519-4dfe-b35a-f867dbaf2e28")
                             {
-                                if (a.Count() > 2)
+                                if (aCount > 2)
                                 {
                                     try
                                     {
                                         // The optional 3rd argument is the stringified assembly
                                         // list so that we don't have to make more round trips
                                         ExecutionContext context = GetContext(sessionId);
-                                        string loaded = a?[2]?["value"]?.ToString();
+                                        string loaded = a[2]?["value"]?.ToString();
                                         if (loaded != null)
                                             context.LoadedFiles = JToken.Parse(loaded).ToObject<string[]>();
                                     }
@@ -110,7 +115,7 @@ namespace Microsoft.WebAssembly.Diagnostics
                                 }
                                 await RuntimeReady(sessionId, token);
                             }
-                            else if (a?[0]?["value"]?.ToString() == MonoConstants.EVENT_RAISED)
+                            else if (aCount > 1 && a[0]?["value"]?.ToString() == MonoConstants.EVENT_RAISED)
                             {
                                 if (a.Type != JTokenType.Array)
                                 {
@@ -118,7 +123,8 @@ namespace Microsoft.WebAssembly.Diagnostics
                                 }
                                 else
                                 {
-                                    if (JObjectTryParse(a?[2]?["value"]?.Value<string>(), out JObject raiseArgs) &&
+                                    if (aCount > 2 &&
+                                        JObjectTryParse(a?[2]?["value"]?.Value<string>(), out JObject raiseArgs) &&
                                         JObjectTryParse(a?[1]?["value"]?.Value<string>(), out JObject eventArgs))
                                     {
                                         await OnJSEventRaised(sessionId, eventArgs, token);