From 454f51dbd98e240685dfd6464e97d1d6acf5130a Mon Sep 17 00:00:00 2001 From: Aaron Sequeira Date: Mon, 8 Dec 2025 18:55:27 +0300 Subject: [PATCH] Fix static analysis issues: null checks, concurrency, and logic errors --- .../CimGetInstance.cs | 9 ++++--- .../Utils.cs | 2 +- .../GetEventCommand.cs | 2 +- .../commands/utility/New-Object.cs | 27 +++++++++++-------- .../ShowCommand/ShowCommandCommandInfo.cs | 2 +- .../host/msh/ConsoleHost.cs | 8 +++--- .../common/DisplayDatabase/typeDataQuery.cs | 2 +- .../engine/hostifaces/Command.cs | 18 ++++++------- .../utils/StringUtil.cs | 2 +- 9 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs index 371b06d9356..49f78607e83 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/CimGetInstance.cs @@ -165,10 +165,13 @@ protected void GetCimInstanceInternal(CimBaseCommand cmdlet) case CimBaseCommand.CimInstanceSessionSet: { CimInstance instance = GetCimInstanceParameter(cmdlet); - nameSpace = ConstValue.GetNamespace(instance.CimSystemProperties.Namespace); - foreach (CimSessionProxy proxy in proxys) + if (instance != null) { - proxy.GetInstanceAsync(nameSpace, instance); + nameSpace = ConstValue.GetNamespace(instance.CimSystemProperties.Namespace); + foreach (CimSessionProxy proxy in proxys) + { + proxy.GetInstanceAsync(nameSpace, instance); + } } } diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs index adcab254231..f5dfce30af5 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/Utils.cs @@ -143,7 +143,7 @@ internal static class DebugHelper /// /// Whether the log been initialized. /// - private static bool logInitialized = false; + private static volatile bool logInitialized = false; internal static bool GenerateVerboseMessage { get; set; } = true; diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs index c7a07bab6ec..524e86270c9 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs @@ -278,7 +278,7 @@ public SwitchParameter Oldest private const string queryOpenerTemplate = ""; - private const string suppressOpener = "*"; + private const string suppressOpener = "*"; private const string suppressCloser = ""; private const char propOpen = '['; private const char propClose = ']'; diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/New-Object.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/New-Object.cs index 0ea312f2963..2d9d65677a2 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/New-Object.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/New-Object.cs @@ -189,18 +189,23 @@ protected override void BeginProcessing() switch (Context.LanguageMode) { - case PSLanguageMode.ConstrainedLanguage: - if (!CoreTypes.Contains(type)) + case PSLanguageMode.ConstrainedLanguage: + if (System.Management.Automation.Security.SystemPolicy.GetSystemLockdownPolicy() != System.Management.Automation.Security.SystemEnforcementMode.Audit) + { + if ((ComObject != null) || (Strict.IsPresent)) { - if (SystemPolicy.GetSystemLockdownPolicy() != SystemEnforcementMode.Audit) - { - ThrowTerminatingError( - new ErrorRecord( - new PSNotSupportedException(NewObjectStrings.CannotCreateTypeConstrainedLanguage), - "CannotCreateTypeConstrainedLanguage", - ErrorCategory.PermissionDenied, - targetObject: null)); - } + ThrowTerminatingError( + new ErrorRecord( + new PSNotSupportedException(NewObjectStrings.CannotCreateTypeConstrainedLanguage), + "CannotCreateTypeConstrainedLanguage", + ErrorCategory.PermissionDenied, + targetObject: null)); + } + } + + break; + case PSLanguageMode.FullLanguage: + break; SystemPolicy.LogWDACAuditMessage( context: Context, diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs index e2ba41fb3fc..77ea015d215 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/ShowCommand/ShowCommandCommandInfo.cs @@ -78,7 +78,7 @@ public ShowCommandCommandInfo(PSObject other) { // Simple case - the objects are still live because they came from in-proc. Just cast them back this.CommandType = (CommandTypes)(other.Members["CommandType"].Value); - this.Module = other.Members["Module"].Value as ShowCommandModuleInfo; + this.Module = other.Members["Module"]?.Value as ShowCommandModuleInfo; } else { diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 5f59540ecf4..956d4092a4a 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -821,14 +821,14 @@ internal LocalRunspace LocalRunspace { get { - if (_isRunspacePushed) + if (RunspaceRef == null) { - return RunspaceRef.OldRunspace as LocalRunspace; + return null; } - if (RunspaceRef == null) + if (_isRunspacePushed) { - return null; + return RunspaceRef.OldRunspace as LocalRunspace; } return RunspaceRef.Runspace as LocalRunspace; diff --git a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs index de6df333951..4062b89ef6a 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/DisplayDatabase/typeDataQuery.cs @@ -412,7 +412,7 @@ private static ViewDefinition GetView(PSPropertyExpressionFactory expressionFact { ActiveTracer.WriteLine( "NOT MATCH {0} NAME: {1}", - ControlBase.GetControlShapeName(vd.mainControl), (vd != null ? vd.name : string.Empty)); + (vd != null ? ControlBase.GetControlShapeName(vd.mainControl) : "null"), (vd != null ? vd.name : string.Empty)); continue; } diff --git a/src/System.Management.Automation/engine/hostifaces/Command.cs b/src/System.Management.Automation/engine/hostifaces/Command.cs index 3de339ff112..9dfa4214004 100644 --- a/src/System.Management.Automation/engine/hostifaces/Command.cs +++ b/src/System.Management.Automation/engine/hostifaces/Command.cs @@ -782,47 +782,47 @@ public enum PipelineResultTypes /// /// Default streaming behavior. /// - None, + None = 0, /// /// Success output. /// - Output, + Output = 1, /// /// Error output. /// - Error, + Error = 2, /// /// Warning information stream. /// - Warning, + Warning = 4, /// /// Verbose information stream. /// - Verbose, + Verbose = 8, /// /// Debug information stream. /// - Debug, + Debug = 16, /// /// Information information stream. /// - Information, + Information = 32, /// /// All streams. /// - All, + All = Output | Error | Warning | Verbose | Debug | Information, /// /// Redirect to nothing. /// - Null + Null = 0 } /// diff --git a/src/System.Management.Automation/utils/StringUtil.cs b/src/System.Management.Automation/utils/StringUtil.cs index 8daf31866df..1d1bc21df16 100644 --- a/src/System.Management.Automation/utils/StringUtil.cs +++ b/src/System.Management.Automation/utils/StringUtil.cs @@ -243,7 +243,7 @@ internal static string VtSubstring(this string str, int startOffset, int length, } else { - int capacity = length + prependStr?.Length ?? 0 + appendStr?.Length ?? 0; + int capacity = length + (prependStr?.Length ?? 0) + (appendStr?.Length ?? 0); return new StringBuilder(prependStr, capacity) .Append(str, startOffset, length) .Append(appendStr)