From 2c18386d79dcada8fbe56b2857de5bd8f7bc7657 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 20 Jul 2020 05:23:14 +0100 Subject: [PATCH] Avoid usage of do statement to create an infinite loop (#13137) * Autofix RCS1063 --- .../commands/management/Service.cs | 4 ++-- .../commands/utility/ShowCommand/ShowCommand.cs | 3 +-- .../host/msh/ConsoleHostUserInterface.cs | 13 +++++-------- .../msh/ConsoleHostUserInterfacePromptForChoice.cs | 6 ++---- .../host/msh/PendingProgress.cs | 8 ++++---- .../engine/CommandPathSearch.cs | 8 ++++---- .../engine/GetCommandCommand.cs | 4 ++-- .../engine/MshCmdlet.cs | 12 ++++++------ .../engine/MshCommandRuntime.cs | 4 ++-- .../engine/hostifaces/InternalHostUserInterface.cs | 4 ++-- .../engine/hostifaces/PSDataCollection.cs | 4 ++-- .../engine/hostifaces/RunspacePoolInternal.cs | 4 ++-- .../engine/parser/Parser.cs | 4 ++-- .../engine/parser/tokenizer.cs | 4 ++-- .../engine/remoting/fanin/BaseTransportManager.cs | 4 ++-- .../engine/remoting/fanin/PriorityCollection.cs | 4 ++-- .../remoting/server/OutOfProcServerMediator.cs | 3 +-- .../namespaces/FileSystemContentStream.cs | 4 ++-- .../namespaces/RegistryProvider.cs | 4 ++-- .../utils/StringUtil.cs | 4 ++-- 20 files changed, 49 insertions(+), 56 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs index 9b5c1450c4..9e1d6c69c5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs @@ -857,7 +857,7 @@ namespace Microsoft.PowerShell.Commands string errorId, string errorMessage) { - do + while (true) { try { @@ -891,7 +891,7 @@ namespace Microsoft.PowerShell.Commands // will throw PipelineStoppedException if user hit CTRL-C WriteWarning(message); } - } while (true); + } } /// diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs index 701508ed4f..f4788fd89b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommand.cs @@ -418,7 +418,7 @@ namespace Microsoft.PowerShell.Commands /// private void WaitForWindowClosedOrHelpNeeded() { - do + while (true) { int which = WaitHandle.WaitAny(new WaitHandle[] { _showCommandProxy.WindowClosed, _showCommandProxy.HelpNeeded, _showCommandProxy.ImportModuleNeeded }); @@ -452,7 +452,6 @@ namespace Microsoft.PowerShell.Commands _showCommandProxy.ImportModuleDone(_importedModules, _commands); continue; } - while (true); } /// diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 483fdb4625..1154715e4f 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -335,7 +335,7 @@ namespace Microsoft.PowerShell Coordinates originalCursorPos = _rawui.CursorPosition; - do + while (true) { // // read one char at a time so that we don't @@ -426,7 +426,6 @@ namespace Microsoft.PowerShell } } } - while (true); } #if UNIX catch (InvalidOperationException) @@ -1149,7 +1148,7 @@ namespace Microsoft.PowerShell w.Flags = WordFlags.IsWhitespace; } - do + while (true) { w.Text = text.Substring(startIndex, i - startIndex); w.CellCount = RawUI.LengthInBufferCells(w.Text); @@ -1165,7 +1164,7 @@ namespace Microsoft.PowerShell --i; } - } while (true); + } Dbg.Assert(RawUI.LengthInBufferCells(w.Text) <= maxWidthInBufferCells, "word should not exceed max"); result.Add(w); @@ -1621,7 +1620,7 @@ namespace Microsoft.PowerShell } #endif - do + while (true) { #if UNIX keyInfo = Console.ReadKey(true); @@ -1848,7 +1847,6 @@ namespace Microsoft.PowerShell Console.CursorLeft = cursorCurrent + 1; #endif } - while (true); Dbg.Assert( (s == null && result == ReadLineResult.endedOnBreak) @@ -1949,7 +1947,7 @@ namespace Microsoft.PowerShell string completionInput = null; #endif - do + while (true) { if (TryInvokeUserDefinedReadLine(out input)) { @@ -2081,7 +2079,6 @@ namespace Microsoft.PowerShell } #endif } - while (true); // Since we did not transcribe any call to ReadLine, transcribe the results here. diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs index 70163c865e..0dfe3f6373 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfacePromptForChoice.cs @@ -91,7 +91,7 @@ namespace Microsoft.PowerShell defaultChoiceKeys.Add(defaultChoice, true); } - do + while (true) { WriteChoicePrompt(hotkeysAndPlainLabels, defaultChoiceKeys, false); @@ -140,7 +140,6 @@ namespace Microsoft.PowerShell // their input matched none of the choices, so prompt again } - while (true); return result; } @@ -231,7 +230,7 @@ namespace Microsoft.PowerShell // used to display ChoiceMessage like Choice[0],Choice[1] etc int choicesSelected = 0; - do + while (true) { // write the current prompt string choiceMsg = StringUtil.Format(ConsoleHostUserInterfaceStrings.ChoiceMessage, choicesSelected); @@ -286,7 +285,6 @@ namespace Microsoft.PowerShell } // prompt for multiple choices } - while (true); return result; } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs index ce4b6d95ac..2ca795447d 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/PendingProgress.cs @@ -337,7 +337,7 @@ namespace Microsoft.PowerShell ProgressNode result = null; ArrayList treeToSearch = _topLevelNodes; - do + while (true) { result = FindOldestLeafmostNodeHelper(treeToSearch, out listWhereFound, out indexWhereFound); if (result == null || result.Children == null || result.Children.Count == 0) @@ -348,7 +348,7 @@ namespace Microsoft.PowerShell // search the subtree for the oldest child treeToSearch = result.Children; - } while (true); + } return result; } @@ -838,7 +838,7 @@ namespace Microsoft.PowerShell int age = 0; - do + while (true) { ProgressNode node = FindOldestNodeOfGivenStyle(_topLevelNodes, age, priorStyle); if (node == null) @@ -854,7 +854,7 @@ namespace Microsoft.PowerShell { return true; } - } while (true); + } // If we get all the way to here, then we've compressed all the nodes and we still don't fit. diff --git a/src/System.Management.Automation/engine/CommandPathSearch.cs b/src/System.Management.Automation/engine/CommandPathSearch.cs index 96c6c8c195..1929000d7d 100644 --- a/src/System.Management.Automation/engine/CommandPathSearch.cs +++ b/src/System.Management.Automation/engine/CommandPathSearch.cs @@ -278,9 +278,9 @@ namespace System.Management.Automation GetNewDirectoryResults(_patternEnumerator.Current, _lookupPathsEnumerator.Current); } - do // while lookupPathsEnumerator is valid + while (true) // while lookupPathsEnumerator is valid { - do // while patternEnumerator is valid + while (true) // while patternEnumerator is valid { // Try moving to the next path in the current results @@ -309,7 +309,7 @@ namespace System.Management.Automation } // Since we have reset the results, loop again to find the next result. - } while (true); + } if (result) { @@ -336,7 +336,7 @@ namespace System.Management.Automation } GetNewDirectoryResults(_patternEnumerator.Current, _lookupPathsEnumerator.Current); - } while (true); + } return result; } diff --git a/src/System.Management.Automation/engine/GetCommandCommand.cs b/src/System.Management.Automation/engine/GetCommandCommand.cs index afc2b12393..b3ab36ae19 100644 --- a/src/System.Management.Automation/engine/GetCommandCommand.cs +++ b/src/System.Management.Automation/engine/GetCommandCommand.cs @@ -950,7 +950,7 @@ namespace Microsoft.PowerShell.Commands bool resultFound = false; isDuplicate = false; - do + while (true) { try { @@ -1057,7 +1057,7 @@ namespace Microsoft.PowerShell.Commands break; } } - } while (true); + } if (All) { diff --git a/src/System.Management.Automation/engine/MshCmdlet.cs b/src/System.Management.Automation/engine/MshCmdlet.cs index d6c8d941a0..113c3d39db 100644 --- a/src/System.Management.Automation/engine/MshCmdlet.cs +++ b/src/System.Management.Automation/engine/MshCmdlet.cs @@ -388,7 +388,7 @@ namespace System.Management.Automation SearchResolutionOptions.None, CommandTypes.Cmdlet, context); - do + while (true) { try { @@ -419,7 +419,7 @@ namespace System.Management.Automation } current = ((IEnumerator)searcher).Current as CmdletInfo; - } while (true); + } return current; } @@ -497,7 +497,7 @@ namespace System.Management.Automation SearchResolutionOptions.CommandNameIsPattern, CommandTypes.Cmdlet, _context); - do + while (true) { try { @@ -530,7 +530,7 @@ namespace System.Management.Automation current = ((IEnumerator)searcher).Current as CmdletInfo; if (current != null) cmdlets.Add(current); - } while (true); + } return cmdlets; } @@ -631,7 +631,7 @@ namespace System.Management.Automation searcher.CommandOrigin = commandOrigin.Value; } - do + while (true) { try { @@ -666,7 +666,7 @@ namespace System.Management.Automation { yield return commandInfo; } - } while (true); + } } /// diff --git a/src/System.Management.Automation/engine/MshCommandRuntime.cs b/src/System.Management.Automation/engine/MshCommandRuntime.cs index 4d07cd3d3a..b67d2d861a 100644 --- a/src/System.Management.Automation/engine/MshCommandRuntime.cs +++ b/src/System.Management.Automation/engine/MshCommandRuntime.cs @@ -3620,7 +3620,7 @@ namespace System.Management.Automation inquireCaption = CommandBaseStrings.InquireCaptionDefault; } - do + while (true) { // Transcribe the confirmation message CBhost.InternalUI.TranscribeResult(inquireCaption); @@ -3694,7 +3694,7 @@ namespace System.Management.Automation PSTraceSource.NewInvalidOperationException(); throw ManageException(e); } - } while (true); + } } /// diff --git a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs index 1e2478d7bd..1eef675260 100644 --- a/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs +++ b/src/System.Management.Automation/engine/hostifaces/InternalHostUserInterface.cs @@ -1034,7 +1034,7 @@ namespace System.Management.Automation.Internal.Host // read choices from the user Collection result = new Collection(); int choicesSelected = 0; - do + while (true) { string choiceMsg = StringUtil.Format(InternalHostUserInterfaceStrings.ChoiceMessage, choicesSelected); messageToBeDisplayed += choiceMsg; @@ -1071,7 +1071,7 @@ namespace System.Management.Automation.Internal.Host } // reset messageToBeDisplayed messageToBeDisplayed = string.Empty; - } while (true); + } return result; } diff --git a/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs b/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs index 51d74dcc26..13448528be 100644 --- a/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs +++ b/src/System.Management.Automation/engine/hostifaces/PSDataCollection.cs @@ -1940,7 +1940,7 @@ namespace System.Management.Automation { lock (_collToEnumerate.SyncObject) { - do + while (true) { if (_index < _collToEnumerate.Count) { @@ -1979,7 +1979,7 @@ namespace System.Management.Automation { return false; } - } while (true); + } } } diff --git a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs index 02f0fffaf3..37ab127658 100644 --- a/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs +++ b/src/System.Management.Automation/engine/hostifaces/RunspacePoolInternal.cs @@ -1510,7 +1510,7 @@ namespace System.Management.Automation.Runspaces.Internal try { - do + while (true) { lock (ultimateRequestQueue) { @@ -1591,7 +1591,7 @@ namespace System.Management.Automation.Runspaces.Internal ultimateRequestQueue.Enqueue(runspaceRequestQueue.Dequeue()); } } - } while (true); + } endOuterWhile:; } finally diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs index 25622b80ed..8914501152 100644 --- a/src/System.Management.Automation/engine/parser/Parser.cs +++ b/src/System.Management.Automation/engine/parser/Parser.cs @@ -5444,7 +5444,7 @@ namespace System.Management.Automation.Language List exceptionTypes = null; Token commaToken = null; - do + while (true) { var restorePoint = _tokenizer.GetRestorePoint(); SkipNewlines(); @@ -5493,7 +5493,7 @@ namespace System.Management.Automation.Language } SkipToken(); - } while (true); + } StatementBlockAst handler = StatementBlockRule(); if (handler == null) diff --git a/src/System.Management.Automation/engine/parser/tokenizer.cs b/src/System.Management.Automation/engine/parser/tokenizer.cs index 2b574071e1..244930b4b2 100644 --- a/src/System.Management.Automation/engine/parser/tokenizer.cs +++ b/src/System.Management.Automation/engine/parser/tokenizer.cs @@ -2618,7 +2618,7 @@ namespace System.Management.Automation.Language nameof(ParserStrings.UnexpectedCharactersAfterHereStringHeader), ParserStrings.UnexpectedCharactersAfterHereStringHeader); - do + while (true) { c = GetChar(); if (c == header[1] && (PeekChar() == '@')) @@ -2632,7 +2632,7 @@ namespace System.Management.Automation.Language UngetChar(); break; } - } while (true); + } return false; } diff --git a/src/System.Management.Automation/engine/remoting/fanin/BaseTransportManager.cs b/src/System.Management.Automation/engine/remoting/fanin/BaseTransportManager.cs index 0f7f46438c..e0dfba1cd0 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/BaseTransportManager.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/BaseTransportManager.cs @@ -805,7 +805,7 @@ namespace System.Management.Automation.Remoting.Client try { - do + while (true) { // if the transport manager is closed return. if (isClosed) @@ -843,7 +843,7 @@ namespace System.Management.Automation.Remoting.Client base.OnDataAvailableCallback(rcvdDataInfo.remoteObject); } } - } while (true); + } } catch (Exception exception) { diff --git a/src/System.Management.Automation/engine/remoting/fanin/PriorityCollection.cs b/src/System.Management.Automation/engine/remoting/fanin/PriorityCollection.cs index 6c2b64479f..8ddd0eb8cb 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/PriorityCollection.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/PriorityCollection.cs @@ -455,7 +455,7 @@ namespace System.Management.Automation.Remoting // this do loop will process one deserialized object. // using a loop allows to process multiple objects within // the same packet - do + while (true) { if (_pendingDataStream.Length <= FragmentedRemoteObject.HeaderLength) { @@ -651,7 +651,7 @@ namespace System.Management.Automation.Remoting break; } } - } while (true); + } } finally { diff --git a/src/System.Management.Automation/engine/remoting/server/OutOfProcServerMediator.cs b/src/System.Management.Automation/engine/remoting/server/OutOfProcServerMediator.cs index a89f1a9d28..67817c8c6e 100644 --- a/src/System.Management.Automation/engine/remoting/server/OutOfProcServerMediator.cs +++ b/src/System.Management.Automation/engine/remoting/server/OutOfProcServerMediator.cs @@ -343,7 +343,7 @@ namespace System.Management.Automation.Remoting.Server try { - do + while (true) { string data = originalStdIn.ReadLine(); lock (_syncObject) @@ -381,7 +381,6 @@ namespace System.Management.Automation.Remoting.Server ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessingThreadStart), data); #endif } - while (true); } catch (Exception e) { diff --git a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs index 5ee6515823..6a81206e22 100644 --- a/src/System.Management.Automation/namespaces/FileSystemContentStream.cs +++ b/src/System.Management.Automation/namespaces/FileSystemContentStream.cs @@ -1382,7 +1382,7 @@ namespace Microsoft.PowerShell.Commands } } - do + while (true) { while (_charCount > 0) { @@ -1403,7 +1403,7 @@ namespace Microsoft.PowerShell.Commands line.Remove(line.Length - charsToRemove, charsToRemove); return line.ToString(); } - } while (true); + } } /// diff --git a/src/System.Management.Automation/namespaces/RegistryProvider.cs b/src/System.Management.Automation/namespaces/RegistryProvider.cs index ff0f578077..cb2963f40c 100644 --- a/src/System.Management.Automation/namespaces/RegistryProvider.cs +++ b/src/System.Management.Automation/namespaces/RegistryProvider.cs @@ -1517,7 +1517,7 @@ namespace Microsoft.PowerShell.Commands bool result = false; - do + while (true) { // See if the paths are equal @@ -1550,7 +1550,7 @@ namespace Microsoft.PowerShell.Commands } destinationPath = newDestinationPath; - } while (true); + } if (result) { diff --git a/src/System.Management.Automation/utils/StringUtil.cs b/src/System.Management.Automation/utils/StringUtil.cs index 9b3cf3447c..9a95407df7 100644 --- a/src/System.Management.Automation/utils/StringUtil.cs +++ b/src/System.Management.Automation/utils/StringUtil.cs @@ -42,7 +42,7 @@ namespace System.Management.Automation.Internal string result; int i = Math.Min(toTruncate.Length, maxWidthInBufferCells); - do + while (true) { result = toTruncate.Substring(0, i); int cellCount = rawUI.LengthInBufferCells(result); @@ -59,7 +59,7 @@ namespace System.Management.Automation.Internal // be characters taking more 2 buffer cells --i; } - } while (true); + } return result; }