From 397756a26f0608bfa47c3a5448ed9ea34864c0c1 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 10 Jan 2020 05:08:09 +0500 Subject: [PATCH] Skip null data in output data received handler to fix a `NullReferenceException` (#11448) --- .../fanin/OutOfProcTransportManager.cs | 39 ++++++------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs index baef84419a..35913a1f7b 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/OutOfProcTransportManager.cs @@ -650,7 +650,7 @@ namespace System.Management.Automation.Remoting.Client { // Object already disposed. } - + _commandMessageQueue.Dispose(); } } @@ -741,41 +741,26 @@ namespace System.Management.Automation.Remoting.Client } } - private const string GUIDTAG = "PSGuid='"; - private const int GUID_STR_LEN = 36; // GUID string: 32 digits plus 4 dashes - - private Guid GetMessageGuid(string data) - { - // Perform quick scan for data packet for a GUID, ignoring any errors. - var iTag = data.IndexOf(GUIDTAG, StringComparison.OrdinalIgnoreCase); - if (iTag > -1) - { - try - { - var psGuidString = data.Substring(iTag + GUIDTAG.Length, GUID_STR_LEN); - return new Guid(psGuidString); - } - catch - { - // Ignore any malformed packet errors here and return an empty Guid. - // Packet errors will be reported later during message processing. - } - } - - return Guid.Empty; - } - #endregion #region Event Handlers + private const string SESSIONDMESSAGETAG = "PSGuid='00000000-0000-0000-0000-000000000000'"; + protected void HandleOutputDataReceived(string data) { + if (string.IsNullOrEmpty(data)) + { + // A null/empty data string indicates a problem in the transport, + // e.g., named pipe emitting a null packet because it closed or some reason. + // In this case we simply ignore the packet. + return; + } + try { // Route protocol message based on whether it is a session or command message. - // Session messages have empty Guid values. - if (Guid.Equals(GetMessageGuid(data), Guid.Empty)) + if (data.IndexOf(SESSIONDMESSAGETAG, StringComparison.OrdinalIgnoreCase) > -1) { // Session message _sessionMessageQueue.Add(data);