From 2ec7aea2ec18d8729dfd9d57c321b05dc97a6737 Mon Sep 17 00:00:00 2001 From: creative-cloud <1001alphatest@gmail.com> Date: Tue, 13 Sep 2022 09:46:52 -0700 Subject: [PATCH] Fixing `CA2014` warnings and removing the warning suppressions (#17982) --- .../commands/management/Process.cs | 72 +++++++++---------- .../host/msh/ConsoleControl.cs | 48 +++++-------- .../host/msh/ConsoleHostUserInterface.cs | 17 +++-- 3 files changed, 60 insertions(+), 77 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index 7cbd1eea1f..fdf3917965 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -743,51 +743,46 @@ namespace Microsoft.PowerShell.Commands try { - do + int error; + if (!Win32Native.OpenProcessToken(process.Handle, TOKEN_QUERY, out processTokenHandler)) { return null; } + + // Set the default length to be 256, so it will be sufficient for most cases. + int tokenInfoLength = 256; + tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength); + if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength)) { - int error; - if (!Win32Native.OpenProcessToken(process.Handle, TOKEN_QUERY, out processTokenHandler)) { break; } - - // Set the default length to be 256, so it will be sufficient for most cases. - int tokenInfoLength = 256; - tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength); - if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength)) + error = Marshal.GetLastWin32Error(); + if (error == Win32Native.ERROR_INSUFFICIENT_BUFFER) { - error = Marshal.GetLastWin32Error(); - if (error == Win32Native.ERROR_INSUFFICIENT_BUFFER) - { - Marshal.FreeHGlobal(tokenUserInfo); - tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength); + Marshal.FreeHGlobal(tokenUserInfo); + tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength); - if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength)) { break; } - } - else - { - break; - } + if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength)) { return null; } } - - var tokenUser = Marshal.PtrToStructure(tokenUserInfo); - - // Max username is defined as UNLEN = 256 in lmcons.h - // Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h - // The buffer length must be +1, last position is for a null string terminator. - int userNameLength = 257; - int domainNameLength = 16; -#pragma warning disable CA2014 - Span userNameStr = stackalloc char[userNameLength]; - Span domainNameStr = stackalloc char[domainNameLength]; -#pragma warning restore CA2014 - Win32Native.SID_NAME_USE accountType; - - // userNameLength and domainNameLength will be set to actual lengths. - if (!Win32Native.LookupAccountSid(null, tokenUser.User.Sid, userNameStr, ref userNameLength, domainNameStr, ref domainNameLength, out accountType)) + else { - break; + return null; } + } - userName = string.Concat(domainNameStr.Slice(0, domainNameLength), "\\", userNameStr.Slice(0, userNameLength)); - } while (false); + var tokenUser = Marshal.PtrToStructure(tokenUserInfo); + + // Max username is defined as UNLEN = 256 in lmcons.h + // Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h + // The buffer length must be +1, last position is for a null string terminator. + int userNameLength = 257; + int domainNameLength = 16; + Span userNameStr = stackalloc char[userNameLength]; + Span domainNameStr = stackalloc char[domainNameLength]; + Win32Native.SID_NAME_USE accountType; + + // userNameLength and domainNameLength will be set to actual lengths. + if (!Win32Native.LookupAccountSid(null, tokenUser.User.Sid, userNameStr, ref userNameLength, domainNameStr, ref domainNameLength, out accountType)) + { + return null; + } + + userName = string.Concat(domainNameStr.Slice(0, domainNameLength), "\\", userNameStr.Slice(0, userNameLength)); } catch (NotSupportedException) { @@ -817,7 +812,6 @@ namespace Microsoft.PowerShell.Commands Win32Native.CloseHandle(processTokenHandler); } } - #endif return userName; } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs index d5b81cf4dd..4ebeb7f92c 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs @@ -2565,39 +2565,29 @@ namespace Microsoft.PowerShell // We need to chop the output string if it is too long. int cursor = 0; // This records the chopping position in output string const int MaxBufferSize = 16383; // this is 64K/4 - 1 to account for possible width of each character. + ReadOnlySpan outBuffer; - while (cursor < output.Length) + while (cursor + MaxBufferSize < output.Length) { - ReadOnlySpan outBuffer; + outBuffer = output.Slice(cursor, MaxBufferSize); + cursor += MaxBufferSize; + WriteConsole(consoleHandle, outBuffer); + } - if (cursor + MaxBufferSize < output.Length) - { - outBuffer = output.Slice(cursor, MaxBufferSize); - cursor += MaxBufferSize; + outBuffer = output.Slice(cursor); - WriteConsole(consoleHandle, outBuffer); - } - else - { - outBuffer = output.Slice(cursor); - cursor = output.Length; - - if (newLine) - { - var endOfLine = Environment.NewLine.AsSpan(); - var endOfLineLength = endOfLine.Length; -#pragma warning disable CA2014 - Span outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength]; -#pragma warning restore CA2014 - outBuffer.CopyTo(outBufferLine); - endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength)); - WriteConsole(consoleHandle, outBufferLine); - } - else - { - WriteConsole(consoleHandle, outBuffer); - } - } + if (newLine) + { + var endOfLine = Environment.NewLine.AsSpan(); + var endOfLineLength = endOfLine.Length; + Span outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength]; + outBuffer.CopyTo(outBufferLine); + endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength)); + WriteConsole(consoleHandle, outBufferLine); + } + else + { + WriteConsole(consoleHandle, outBuffer); } } diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs index 16c8382722..d755e04298 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs @@ -332,20 +332,19 @@ namespace Microsoft.PowerShell Coordinates originalCursorPos = _rawui.CursorPosition; + // + // read one char at a time so that we don't + // end up having a immutable string holding the + // secret in memory. + // + const int CharactersToRead = 1; + Span inputBuffer = stackalloc char[CharactersToRead + 1]; + while (true) { - // - // read one char at a time so that we don't - // end up having a immutable string holding the - // secret in memory. - // #if UNIX ConsoleKeyInfo keyInfo = Console.ReadKey(true); #else - const int CharactersToRead = 1; -#pragma warning disable CA2014 - Span inputBuffer = stackalloc char[CharactersToRead + 1]; -#pragma warning restore CA2014 string key = ConsoleControl.ReadConsole(handle, initialContentLength: 0, inputBuffer, charactersToRead: CharactersToRead, endOnTab: false, out _); #endif