mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fixing CA2014 warnings and removing the warning suppressions (#17982)
This commit is contained in:
@@ -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<Win32Native.TOKEN_USER>(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<char> userNameStr = stackalloc char[userNameLength];
|
||||
Span<char> 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<Win32Native.TOKEN_USER>(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<char> userNameStr = stackalloc char[userNameLength];
|
||||
Span<char> 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;
|
||||
}
|
||||
|
||||
@@ -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<char> outBuffer;
|
||||
|
||||
while (cursor < output.Length)
|
||||
while (cursor + MaxBufferSize < output.Length)
|
||||
{
|
||||
ReadOnlySpan<char> 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<char> 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<char> outBufferLine = stackalloc char[outBuffer.Length + endOfLineLength];
|
||||
outBuffer.CopyTo(outBufferLine);
|
||||
endOfLine.CopyTo(outBufferLine.Slice(outBufferLine.Length - endOfLineLength));
|
||||
WriteConsole(consoleHandle, outBufferLine);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteConsole(consoleHandle, outBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<char> 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<char> inputBuffer = stackalloc char[CharactersToRead + 1];
|
||||
#pragma warning restore CA2014
|
||||
string key = ConsoleControl.ReadConsole(handle, initialContentLength: 0, inputBuffer, charactersToRead: CharactersToRead, endOnTab: false, out _);
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user