Skip to content

Commit 2ec7aea

Browse files
Fixing CA2014 warnings and removing the warning suppressions (PowerShell#17982)
1 parent 944ed54 commit 2ec7aea

File tree

3 files changed

+60
-77
lines changed

3 files changed

+60
-77
lines changed

src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -743,51 +743,46 @@ private static string RetrieveProcessUserName(Process process)
743743

744744
try
745745
{
746-
do
747-
{
748-
int error;
749-
if (!Win32Native.OpenProcessToken(process.Handle, TOKEN_QUERY, out processTokenHandler)) { break; }
746+
int error;
747+
if (!Win32Native.OpenProcessToken(process.Handle, TOKEN_QUERY, out processTokenHandler)) { return null; }
750748

751-
// Set the default length to be 256, so it will be sufficient for most cases.
752-
int tokenInfoLength = 256;
753-
tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength);
754-
if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength))
749+
// Set the default length to be 256, so it will be sufficient for most cases.
750+
int tokenInfoLength = 256;
751+
tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength);
752+
if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength))
753+
{
754+
error = Marshal.GetLastWin32Error();
755+
if (error == Win32Native.ERROR_INSUFFICIENT_BUFFER)
755756
{
756-
error = Marshal.GetLastWin32Error();
757-
if (error == Win32Native.ERROR_INSUFFICIENT_BUFFER)
758-
{
759-
Marshal.FreeHGlobal(tokenUserInfo);
760-
tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength);
757+
Marshal.FreeHGlobal(tokenUserInfo);
758+
tokenUserInfo = Marshal.AllocHGlobal(tokenInfoLength);
761759

762-
if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength)) { break; }
763-
}
764-
else
765-
{
766-
break;
767-
}
760+
if (!Win32Native.GetTokenInformation(processTokenHandler, Win32Native.TOKEN_INFORMATION_CLASS.TokenUser, tokenUserInfo, tokenInfoLength, out tokenInfoLength)) { return null; }
768761
}
769-
770-
var tokenUser = Marshal.PtrToStructure<Win32Native.TOKEN_USER>(tokenUserInfo);
771-
772-
// Max username is defined as UNLEN = 256 in lmcons.h
773-
// Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h
774-
// The buffer length must be +1, last position is for a null string terminator.
775-
int userNameLength = 257;
776-
int domainNameLength = 16;
777-
#pragma warning disable CA2014
778-
Span<char> userNameStr = stackalloc char[userNameLength];
779-
Span<char> domainNameStr = stackalloc char[domainNameLength];
780-
#pragma warning restore CA2014
781-
Win32Native.SID_NAME_USE accountType;
782-
783-
// userNameLength and domainNameLength will be set to actual lengths.
784-
if (!Win32Native.LookupAccountSid(null, tokenUser.User.Sid, userNameStr, ref userNameLength, domainNameStr, ref domainNameLength, out accountType))
762+
else
785763
{
786-
break;
764+
return null;
787765
}
766+
}
767+
768+
var tokenUser = Marshal.PtrToStructure<Win32Native.TOKEN_USER>(tokenUserInfo);
769+
770+
// Max username is defined as UNLEN = 256 in lmcons.h
771+
// Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h
772+
// The buffer length must be +1, last position is for a null string terminator.
773+
int userNameLength = 257;
774+
int domainNameLength = 16;
775+
Span<char> userNameStr = stackalloc char[userNameLength];
776+
Span<char> domainNameStr = stackalloc char[domainNameLength];
777+
Win32Native.SID_NAME_USE accountType;
788778

789-
userName = string.Concat(domainNameStr.Slice(0, domainNameLength), "\\", userNameStr.Slice(0, userNameLength));
790-
} while (false);
779+
// userNameLength and domainNameLength will be set to actual lengths.
780+
if (!Win32Native.LookupAccountSid(null, tokenUser.User.Sid, userNameStr, ref userNameLength, domainNameStr, ref domainNameLength, out accountType))
781+
{
782+
return null;
783+
}
784+
785+
userName = string.Concat(domainNameStr.Slice(0, domainNameLength), "\\", userNameStr.Slice(0, userNameLength));
791786
}
792787
catch (NotSupportedException)
793788
{
@@ -817,7 +812,6 @@ private static string RetrieveProcessUserName(Process process)
817812
Win32Native.CloseHandle(processTokenHandler);
818813
}
819814
}
820-
821815
#endif
822816
return userName;
823817
}

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleControl.cs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,39 +2565,29 @@ internal static void WriteConsole(ConsoleHandle consoleHandle, ReadOnlySpan<char
25652565
// We need to chop the output string if it is too long.
25662566
int cursor = 0; // This records the chopping position in output string
25672567
const int MaxBufferSize = 16383; // this is 64K/4 - 1 to account for possible width of each character.
2568+
ReadOnlySpan<char> outBuffer;
25682569

2569-
while (cursor < output.Length)
2570+
while (cursor + MaxBufferSize < output.Length)
25702571
{
2571-
ReadOnlySpan<char> outBuffer;
2572-
2573-
if (cursor + MaxBufferSize < output.Length)
2574-
{
2575-
outBuffer = output.Slice(cursor, MaxBufferSize);
2576-
cursor += MaxBufferSize;
2572+
outBuffer = output.Slice(cursor, MaxBufferSize);
2573+
cursor += MaxBufferSize;
2574+
WriteConsole(consoleHandle, outBuffer);
2575+
}
25772576

2578-
WriteConsole(consoleHandle, outBuffer);
2579-
}
2580-
else
2581-
{
2582-
outBuffer = output.Slice(cursor);
2583-
cursor = output.Length;
2577+
outBuffer = output.Slice(cursor);
25842578

2585-
if (newLine)
2586-
{
2587-
var endOfLine = Environment.NewLine.AsSpan();
2588-
var endOfLineLength = endOfLine.Length;
2589-
#pragma warning disable CA2014
2590-
Span<char> outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength];
2591-
#pragma warning restore CA2014
2592-
outBuffer.CopyTo(outBufferLine);
2593-
endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength));
2594-
WriteConsole(consoleHandle, outBufferLine);
2595-
}
2596-
else
2597-
{
2598-
WriteConsole(consoleHandle, outBuffer);
2599-
}
2600-
}
2579+
if (newLine)
2580+
{
2581+
var endOfLine = Environment.NewLine.AsSpan();
2582+
var endOfLineLength = endOfLine.Length;
2583+
Span<char> outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength];
2584+
outBuffer.CopyTo(outBufferLine);
2585+
endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength));
2586+
WriteConsole(consoleHandle, outBufferLine);
2587+
}
2588+
else
2589+
{
2590+
WriteConsole(consoleHandle, outBuffer);
26012591
}
26022592
}
26032593

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,20 +332,19 @@ private object ReadLineSafe(bool isSecureString, char? printToken)
332332

333333
Coordinates originalCursorPos = _rawui.CursorPosition;
334334

335+
//
336+
// read one char at a time so that we don't
337+
// end up having a immutable string holding the
338+
// secret in memory.
339+
//
340+
const int CharactersToRead = 1;
341+
Span<char> inputBuffer = stackalloc char[CharactersToRead + 1];
342+
335343
while (true)
336344
{
337-
//
338-
// read one char at a time so that we don't
339-
// end up having a immutable string holding the
340-
// secret in memory.
341-
//
342345
#if UNIX
343346
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
344347
#else
345-
const int CharactersToRead = 1;
346-
#pragma warning disable CA2014
347-
Span<char> inputBuffer = stackalloc char[CharactersToRead + 1];
348-
#pragma warning restore CA2014
349348
string key = ConsoleControl.ReadConsole(handle, initialContentLength: 0, inputBuffer, charactersToRead: CharactersToRead, endOnTab: false, out _);
350349
#endif
351350

0 commit comments

Comments
 (0)