From 5ee910acad47207d7e5a9054a237f8a7d9544990 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 30 Apr 2018 10:32:45 -0700 Subject: [PATCH 1/3] fix crash when terminal is reset --- .../host/msh/ConsoleHostUserInterface.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 7c57d9532b4..69f4bf829e1 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -1791,10 +1791,16 @@ private string ReadLineFromConsole(bool endOnTab, string initialContent, bool ca } // Modify string - if (!insertMode) // then overwrite mode + if (!insertMode && s != "") // then overwrite mode { s = s.Remove(index, 1); } + + if (index > s.Length) + { + index = s.Length; + } + s = s.Insert(index, keyInfo.KeyChar.ToString()); index++; From 96c7060a5b2eb7f9409671c229436bc9ab76698f Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 30 Apr 2018 16:48:09 -0700 Subject: [PATCH 2/3] address Dongbo's feedback --- .../host/msh/ConsoleHostUserInterface.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 69f4bf829e1..ac746f90582 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -1790,15 +1790,16 @@ private string ReadLineFromConsole(bool endOnTab, string initialContent, bool ca continue; } - // Modify string - if (!insertMode && s != "") // then overwrite mode + // Handle case where terminal gets reset and the index is outside of the buffer + if (index > s.Length) { - s = s.Remove(index, 1); + index = s.Length; } - if (index > s.Length) + // Modify string + if (!insertMode) // then overwrite mode { - index = s.Length; + s = s.Remove(index, 1); } s = s.Insert(index, keyInfo.KeyChar.ToString()); From 0b493eade7c2ae72bc592f7c7890f570847f8db0 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 30 Apr 2018 22:17:52 -0700 Subject: [PATCH 3/3] fix check before removing characters --- .../host/msh/ConsoleHostUserInterface.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index ac746f90582..d15d239cb30 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -1797,7 +1797,7 @@ private string ReadLineFromConsole(bool endOnTab, string initialContent, bool ca } // Modify string - if (!insertMode) // then overwrite mode + if (!insertMode && index < s.Length) // then overwrite mode { s = s.Remove(index, 1); }