From 819b654e92513aee40966785fdfa242c8c7d49a4 Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Mon, 28 Mar 2022 15:51:01 -0400 Subject: [PATCH] [wasm] MonoProxy.cs: Avoid some errors by pre-emptively checking (#67243) .. inspired by https://github.com/dotnet/runtime/issues/66149 . --- .../debugger/BrowserDebugProxy/MonoProxy.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs index 52f3f65cafb19..4140202ade1ea 100644 --- a/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs +++ b/src/mono/wasm/debugger/BrowserDebugProxy/MonoProxy.cs @@ -89,17 +89,22 @@ protected override async Task AcceptEvent(SessionId sessionId, string meth 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(); } @@ -110,7 +115,7 @@ protected override async Task AcceptEvent(SessionId sessionId, string meth } 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 @@ protected override async Task AcceptEvent(SessionId sessionId, string meth } else { - if (JObjectTryParse(a?[2]?["value"]?.Value(), out JObject raiseArgs) && + if (aCount > 2 && + JObjectTryParse(a?[2]?["value"]?.Value(), out JObject raiseArgs) && JObjectTryParse(a?[1]?["value"]?.Value(), out JObject eventArgs)) { await OnJSEventRaised(sessionId, eventArgs, token);