From f82e0dca52a655a4df77940bf40f1149f0eedbba Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Thu, 28 May 2020 19:13:29 +0100 Subject: [PATCH 1/4] Use coalesce expression RCS1128 --- .../GetCimAssociatedInstanceCommand.cs | 6 +--- .../GetCimClassCommand.cs | 6 +--- .../GetCimInstanceCommand.cs | 6 +--- .../InvokeCimMethodCommand.cs | 6 +--- .../NewCimInstanceCommand.cs | 6 +--- .../RemoveCimInstanceCommand.cs | 6 +--- .../SetCimInstanceCommand.cs | 6 +--- .../FilterRuleTemplateSelector.cs | 6 +--- .../commandHelpers/ShowCommandHelper.cs | 6 +--- .../commands/utility/AddType.cs | 6 +--- src/Microsoft.WSMan.Management/CredSSP.cs | 8 ++--- .../WSManConnections.cs | 6 +--- src/Microsoft.WSMan.Management/WsManHelper.cs | 6 +--- .../engine/PSConfiguration.cs | 30 +++++++++---------- .../engine/SessionStateDriveAPIs.cs | 10 +++---- .../engine/lang/parserutils.cs | 10 +++---- .../help/HelpProvider.cs | 10 +++---- .../security/wldpNativeMethods.cs | 10 +++---- 18 files changed, 49 insertions(+), 101 deletions(-) diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimAssociatedInstanceCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimAssociatedInstanceCommand.cs index 680693aab4b..32bf5a2f92d 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimAssociatedInstanceCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimAssociatedInstanceCommand.cs @@ -248,11 +248,7 @@ protected override void BeginProcessing() protected override void ProcessRecord() { base.CheckParameterSet(); - CimGetAssociatedInstance operation = this.GetOperationAgent(); - if (operation == null) - { - operation = this.CreateOperationAgent(); - } + CimGetAssociatedInstance operation = this.GetOperationAgent() ?? this.CreateOperationAgent(); operation.GetCimAssociatedInstance(this); operation.ProcessActions(this.CmdletOperation); diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimClassCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimClassCommand.cs index a1a33d1eddd..2d804dffa8a 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimClassCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimClassCommand.cs @@ -220,11 +220,7 @@ protected override void BeginProcessing() protected override void ProcessRecord() { base.CheckParameterSet(); - CimGetCimClass cimGetCimClass = this.GetOperationAgent(); - if (cimGetCimClass == null) - { - cimGetCimClass = CreateOperationAgent(); - } + CimGetCimClass cimGetCimClass = this.GetOperationAgent() ?? CreateOperationAgent(); cimGetCimClass.GetCimClass(this); cimGetCimClass.ProcessActions(this.CmdletOperation); diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimInstanceCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimInstanceCommand.cs index 4eb43d9b65c..af56bd5eedd 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimInstanceCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/GetCimInstanceCommand.cs @@ -460,11 +460,7 @@ protected override void ProcessRecord() { base.CheckParameterSet(); this.CheckArgument(); - CimGetInstance cimGetInstance = this.GetOperationAgent(); - if (cimGetInstance == null) - { - cimGetInstance = CreateOperationAgent(); - } + CimGetInstance cimGetInstance = this.GetOperationAgent() ?? CreateOperationAgent(); cimGetInstance.GetCimInstance(this); cimGetInstance.ProcessActions(this.CmdletOperation); diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/InvokeCimMethodCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/InvokeCimMethodCommand.cs index 914c4d00334..67172d6385e 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/InvokeCimMethodCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/InvokeCimMethodCommand.cs @@ -373,11 +373,7 @@ public UInt32 OperationTimeoutSec /// protected override void BeginProcessing() { - CimInvokeCimMethod cimInvokeMethod = this.GetOperationAgent(); - if (cimInvokeMethod == null) - { - cimInvokeMethod = CreateOperationAgent(); - } + CimInvokeCimMethod cimInvokeMethod = this.GetOperationAgent() ?? CreateOperationAgent(); this.CmdletOperation = new CmdletOperationInvokeCimMethod(this, cimInvokeMethod); this.AtBeginProcess = false; diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/NewCimInstanceCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/NewCimInstanceCommand.cs index c15a8da548d..1cee429864d 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/NewCimInstanceCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/NewCimInstanceCommand.cs @@ -355,11 +355,7 @@ protected override void ProcessRecord() } } - CimNewCimInstance cimNewCimInstance = this.GetOperationAgent(); - if (cimNewCimInstance == null) - { - cimNewCimInstance = CreateOperationAgent(); - } + CimNewCimInstance cimNewCimInstance = this.GetOperationAgent() ?? CreateOperationAgent(); cimNewCimInstance.NewCimInstance(this); cimNewCimInstance.ProcessActions(this.CmdletOperation); diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/RemoveCimInstanceCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/RemoveCimInstanceCommand.cs index 7ab67b83cc2..c83a307f15c 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/RemoveCimInstanceCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/RemoveCimInstanceCommand.cs @@ -244,11 +244,7 @@ public string QueryDialect /// protected override void BeginProcessing() { - CimRemoveCimInstance cimRemoveInstance = this.GetOperationAgent(); - if (cimRemoveInstance == null) - { - cimRemoveInstance = CreateOperationAgent(); - } + CimRemoveCimInstance cimRemoveInstance = this.GetOperationAgent() ?? CreateOperationAgent(); this.CmdletOperation = new CmdletOperationRemoveCimInstance(this, cimRemoveInstance); this.AtBeginProcess = false; diff --git a/src/Microsoft.Management.Infrastructure.CimCmdlets/SetCimInstanceCommand.cs b/src/Microsoft.Management.Infrastructure.CimCmdlets/SetCimInstanceCommand.cs index 5b1e6bc154d..27bd2996b5f 100644 --- a/src/Microsoft.Management.Infrastructure.CimCmdlets/SetCimInstanceCommand.cs +++ b/src/Microsoft.Management.Infrastructure.CimCmdlets/SetCimInstanceCommand.cs @@ -301,11 +301,7 @@ public SwitchParameter PassThru /// protected override void BeginProcessing() { - CimSetCimInstance cimSetCimInstance = this.GetOperationAgent(); - if (cimSetCimInstance == null) - { - cimSetCimInstance = CreateOperationAgent(); - } + CimSetCimInstance cimSetCimInstance = this.GetOperationAgent() ?? CreateOperationAgent(); this.CmdletOperation = new CmdletOperationSetCimInstance(this, cimSetCimInstance); this.AtBeginProcess = false; diff --git a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRuleTemplateSelector.cs b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRuleTemplateSelector.cs index e7e44ce8507..fc40c3c5768 100644 --- a/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRuleTemplateSelector.cs +++ b/src/Microsoft.Management.UI.Internal/ManagementList/FilterProviders/FilterRuleTemplateSelector.cs @@ -45,11 +45,7 @@ public override DataTemplate SelectTemplate(object item, System.Windows.Dependen return base.SelectTemplate(item, container); } - Type type = item as Type; - if (type == null) - { - type = item.GetType(); - } + Type type = item as Type ?? item.GetType(); DataTemplate template; diff --git a/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs b/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs index bc15f1e76bd..2f66687d9e2 100644 --- a/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs +++ b/src/Microsoft.Management.UI.Internal/commandHelpers/ShowCommandHelper.cs @@ -597,11 +597,7 @@ internal static List GetCommandList(object[] commandObje /// An array of objects out of . internal static object[] ObjectArrayFromObjectCollection(object commandObjects) { - object[] objectArray = commandObjects as object[]; - if (objectArray == null) - { - objectArray = ((System.Collections.ArrayList)commandObjects).ToArray(); - } + object[] objectArray = commandObjects as object[] ?? ((System.Collections.ArrayList)commandObjects).ToArray(); return objectArray; } diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs index df96aba17a4..b165efca27c 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/AddType.cs @@ -650,11 +650,7 @@ private void LoadAssemblies(IEnumerable assemblies) { // CoreCLR doesn't allow re-load TPA assemblies with different API (i.e. we load them by name and now want to load by path). // LoadAssemblyHelper helps us avoid re-loading them, if they already loaded. - Assembly assembly = LoadAssemblyHelper(assemblyName); - if (assembly == null) - { - assembly = Assembly.LoadFrom(ResolveAssemblyName(assemblyName, false)); - } + Assembly assembly = LoadAssemblyHelper(assemblyName) ?? Assembly.LoadFrom(ResolveAssemblyName(assemblyName, false)); if (PassThru) { diff --git a/src/Microsoft.WSMan.Management/CredSSP.cs b/src/Microsoft.WSMan.Management/CredSSP.cs index 259bf4910ff..c51c663a4d4 100644 --- a/src/Microsoft.WSMan.Management/CredSSP.cs +++ b/src/Microsoft.WSMan.Management/CredSSP.cs @@ -665,17 +665,13 @@ private void UpdateGPORegistrySettings(string applicationname, string[] delegate { string Registry_Path_Credentials_Delegation = Registry_Path + @"\CredentialsDelegation"; // open the registry key.If key is not present,create a new one - Credential_Delegation_Key = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation, true); - if (Credential_Delegation_Key == null) - Credential_Delegation_Key = rootKey.CreateSubKey(Registry_Path_Credentials_Delegation, RegistryKeyPermissionCheck.ReadWriteSubTree); + Credential_Delegation_Key = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation, true) ?? rootKey.CreateSubKey(Registry_Path_Credentials_Delegation, RegistryKeyPermissionCheck.ReadWriteSubTree); Credential_Delegation_Key.SetValue(helper.Key_Allow_Fresh_Credentials, 1, RegistryValueKind.DWord); Credential_Delegation_Key.SetValue(helper.Key_Concatenate_Defaults_AllowFresh, 1, RegistryValueKind.DWord); // add the delegate value - Allow_Fresh_Credential_Key = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation + @"\" + helper.Key_Allow_Fresh_Credentials, true); - if (Allow_Fresh_Credential_Key == null) - Allow_Fresh_Credential_Key = rootKey.CreateSubKey(Registry_Path_Credentials_Delegation + @"\" + helper.Key_Allow_Fresh_Credentials, RegistryKeyPermissionCheck.ReadWriteSubTree); + Allow_Fresh_Credential_Key = rootKey.OpenSubKey(Registry_Path_Credentials_Delegation + @"\" + helper.Key_Allow_Fresh_Credentials, true) ?? rootKey.CreateSubKey(Registry_Path_Credentials_Delegation + @"\" + helper.Key_Allow_Fresh_Credentials, RegistryKeyPermissionCheck.ReadWriteSubTree); if (Allow_Fresh_Credential_Key != null) { diff --git a/src/Microsoft.WSMan.Management/WSManConnections.cs b/src/Microsoft.WSMan.Management/WSManConnections.cs index 103115fff98..4aeb283b81f 100644 --- a/src/Microsoft.WSMan.Management/WSManConnections.cs +++ b/src/Microsoft.WSMan.Management/WSManConnections.cs @@ -269,11 +269,7 @@ protected override void BeginProcessing() } } - string crtComputerName = computername; - if (crtComputerName == null) - { - crtComputerName = "localhost"; - } + string crtComputerName = computername ?? "localhost"; if (this.SessionState.Path.CurrentProviderLocation(WSManStringLiterals.rootpath).Path.StartsWith(this.SessionState.Drive.Current.Name + ":" + WSManStringLiterals.DefaultPathSeparator + crtComputerName, StringComparison.OrdinalIgnoreCase)) { diff --git a/src/Microsoft.WSMan.Management/WsManHelper.cs b/src/Microsoft.WSMan.Management/WsManHelper.cs index a1ce8af4214..7b0a8f4bf13 100644 --- a/src/Microsoft.WSMan.Management/WsManHelper.cs +++ b/src/Microsoft.WSMan.Management/WsManHelper.cs @@ -960,11 +960,7 @@ internal void CreateWsManConnection(string ParameterSetName, Uri connectionuri, IWSManSession m_session = CreateSessionObject(m_wsmanObject, authentication, sessionoption, credential, connectionStr, certificateThumbprint, usessl); m_session.Identify(0); - string key = computername; - if (key == null) - { - key = "localhost"; - } + string key = computername ?? "localhost"; AddtoDictionary(key, m_session); } diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index 6c585cc0a05..adc14c561d3 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -219,36 +219,36 @@ internal void SetExperimentalFeatures(ConfigScope scope, string featureName, boo internal bool IsImplicitWinCompatEnabled() { - bool? settingValue = ReadValueFromFile(ConfigScope.CurrentUser, DisableImplicitWinCompatKey); - if (!settingValue.HasValue) - { + bool? settingValue = ReadValueFromFile(ConfigScope.CurrentUser, DisableImplicitWinCompatKey) ?? ReadValueFromFile(ConfigScope.AllUsers, DisableImplicitWinCompatKey, defaultValue: false); + + // if the setting is not mentioned in configuration files, then the default DisableImplicitWinCompat value is False - settingValue = ReadValueFromFile(ConfigScope.AllUsers, DisableImplicitWinCompatKey, defaultValue: false); - } + + return !settingValue.Value; } internal string[] GetWindowsPowerShellCompatibilityModuleDenyList() { - string[] settingValue = ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityModuleDenyListKey); - if (settingValue == null) - { + string[] settingValue = ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityModuleDenyListKey) ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityModuleDenyListKey); + + // if the setting is not mentioned in configuration files, then the default WindowsPowerShellCompatibilityModuleDenyList value is null - settingValue = ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityModuleDenyListKey); - } + + return settingValue; } internal string[] GetWindowsPowerShellCompatibilityNoClobberModuleList() { - string[] settingValue = ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityNoClobberModuleListKey); - if (settingValue == null) - { + string[] settingValue = ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityNoClobberModuleListKey) ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityNoClobberModuleListKey); + + // if the setting is not mentioned in configuration files, then the default WindowsPowerShellCompatibilityNoClobberModuleList value is null - settingValue = ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityNoClobberModuleListKey); - } + + return settingValue; } diff --git a/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs b/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs index 91228e4dd1b..ba78c88089b 100644 --- a/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs +++ b/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs @@ -474,12 +474,12 @@ private PSDriveInfo GetDrive(string name, bool automount) if (result == null && automount) { // first try to automount as a file system drive - result = AutomountFileSystemDrive(name); + result = AutomountFileSystemDrive(name) ?? AutomountBuiltInDrive(name); // if it didn't work, then try automounting as a BuiltIn drive (e.g. "Cert"/"Certificate"/"WSMan") - if (result == null) - { - result = AutomountBuiltInDrive(name); // internally this calls GetDrive(name, false) - } + + + // internally this calls GetDrive(name, false) + } if (result == null) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index d2203f8b90e..97e0f771527 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -1177,13 +1177,13 @@ internal static object MatchOperator(ExecutionContext context, IScriptExtent err // if passed an explicit regex, just use it // otherwise compile the expression. - Regex r = PSObject.Base(rval) as Regex; - if (r == null) - { + Regex r = PSObject.Base(rval) as Regex ?? NewRegex(PSObject.ToStringParser(context, rval), reOptions); + + // In this situation, creation of Regex should not fail. We are not // processing ArgumentException in this case. - r = NewRegex(PSObject.ToStringParser(context, rval), reOptions); - } + + IEnumerator list = LanguagePrimitives.GetEnumerator(lval); if (list == null) diff --git a/src/System.Management.Automation/help/HelpProvider.cs b/src/System.Management.Automation/help/HelpProvider.cs index 66a7ab1ab95..88d9dc3f929 100644 --- a/src/System.Management.Automation/help/HelpProvider.cs +++ b/src/System.Management.Automation/help/HelpProvider.cs @@ -225,13 +225,13 @@ internal string GetDefaultShellSearchPath() { string shellID = this.HelpSystem.ExecutionContext.ShellID; // Beginning in PowerShell 6.0.0.12, the $pshome is no longer registry specified, we search the application base instead. - string returnValue = Utils.GetApplicationBase(shellID); + string returnValue = Utils.GetApplicationBase(shellID) ?? Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName); - if (returnValue == null) - { + + // use executing assemblies location in case registry entry not found - returnValue = Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName); - } + + return returnValue; } diff --git a/src/System.Management.Automation/security/wldpNativeMethods.cs b/src/System.Management.Automation/security/wldpNativeMethods.cs index 974392424eb..1a18014696b 100644 --- a/src/System.Management.Automation/security/wldpNativeMethods.cs +++ b/src/System.Management.Automation/security/wldpNativeMethods.cs @@ -82,7 +82,7 @@ public static SystemEnforcementMode GetLockdownPolicy(string path, SafeHandle ha // At this point, LockdownPolicy = Audit or Allowed. // If there was a WLDP policy, but WLDP didn't block it, // then it was explicitly allowed. Therefore, return the result for the file. - SystemEnforcementMode systemWldpPolicy = s_cachedWldpSystemPolicy.GetValueOrDefault(SystemEnforcementMode.None); + SystemEnforcementMode systemWldpPolicy = s_cachedWldpSystemPolicy ?? SystemEnforcementMode.None; if ((systemWldpPolicy == SystemEnforcementMode.Enforce) || (systemWldpPolicy == SystemEnforcementMode.Audit)) { @@ -98,7 +98,7 @@ public static SystemEnforcementMode GetLockdownPolicy(string path, SafeHandle ha // If there was a system-wide AppLocker policy, but AppLocker didn't block it, // then return AppLocker's status. - if (s_cachedSaferSystemPolicy.GetValueOrDefault(SaferPolicy.Allowed) == + if ((s_cachedSaferSystemPolicy ?? SaferPolicy.Allowed) == SaferPolicy.Disallowed) { return lockdownPolicy; @@ -115,7 +115,7 @@ private static SystemEnforcementMode GetWldpPolicy(string path, SafeHandle handl // If the WLDP assembly is missing (such as windows 7 or down OS), return default/None to skip WLDP validation if (s_hadMissingWldpAssembly) { - return s_cachedWldpSystemPolicy.GetValueOrDefault(SystemEnforcementMode.None); + return s_cachedWldpSystemPolicy ?? SystemEnforcementMode.None; } // If path is NULL, see if we have the cached system-wide lockdown policy. @@ -168,7 +168,7 @@ private static SystemEnforcementMode GetWldpPolicy(string path, SafeHandle handl catch (DllNotFoundException) { s_hadMissingWldpAssembly = true; - return s_cachedWldpSystemPolicy.GetValueOrDefault(SystemEnforcementMode.None); + return s_cachedWldpSystemPolicy ?? SystemEnforcementMode.None; } } @@ -384,7 +384,7 @@ private static SystemEnforcementMode GetDebugLockdownPolicy(string path) // No explicit debug allowance for the file, so return the system policy if there // is one. - return s_systemLockdownPolicy.GetValueOrDefault(SystemEnforcementMode.None); + return s_systemLockdownPolicy ?? SystemEnforcementMode.None; } // Support fall-back debug hook for system-wide policy on non-WOA platforms From d2b1e818260f5f3c5a4da93e2eb0123ed76e9769 Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Thu, 28 May 2020 19:39:44 +0100 Subject: [PATCH 2/4] cleanup --- .../engine/PSConfiguration.cs | 35 ++++++------------- .../engine/SessionStateDriveAPIs.cs | 11 +++--- .../engine/lang/parserutils.cs | 11 +++--- .../help/HelpProvider.cs | 12 ++----- 4 files changed, 23 insertions(+), 46 deletions(-) diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index adc14c561d3..49625084244 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -219,38 +219,25 @@ internal void SetExperimentalFeatures(ConfigScope scope, string featureName, boo internal bool IsImplicitWinCompatEnabled() { - bool? settingValue = ReadValueFromFile(ConfigScope.CurrentUser, DisableImplicitWinCompatKey) ?? ReadValueFromFile(ConfigScope.AllUsers, DisableImplicitWinCompatKey, defaultValue: false); - - - // if the setting is not mentioned in configuration files, then the default DisableImplicitWinCompat value is False - - - - return !settingValue.Value; + bool settingValue = ReadValueFromFile(ConfigScope.CurrentUser, DisableImplicitWinCompatKey) + ?? ReadValueFromFile(ConfigScope.AllUsers, DisableImplicitWinCompatKey) + ?? false; + + return !settingValue; } internal string[] GetWindowsPowerShellCompatibilityModuleDenyList() { - string[] settingValue = ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityModuleDenyListKey) ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityModuleDenyListKey); - - - // if the setting is not mentioned in configuration files, then the default WindowsPowerShellCompatibilityModuleDenyList value is null - - - - return settingValue; + return ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityModuleDenyListKey) + ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityModuleDenyListKey) + ?? null; } internal string[] GetWindowsPowerShellCompatibilityNoClobberModuleList() { - string[] settingValue = ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityNoClobberModuleListKey) ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityNoClobberModuleListKey); - - - // if the setting is not mentioned in configuration files, then the default WindowsPowerShellCompatibilityNoClobberModuleList value is null - - - - return settingValue; + return ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityNoClobberModuleListKey) + ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityNoClobberModuleListKey) + ?? null; } /// diff --git a/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs b/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs index ba78c88089b..7c2eeb447c8 100644 --- a/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs +++ b/src/System.Management.Automation/engine/SessionStateDriveAPIs.cs @@ -473,13 +473,9 @@ private PSDriveInfo GetDrive(string name, bool automount) if (result == null && automount) { - // first try to automount as a file system drive + // Attempt to automount as a file system drive + // or as a BuiltIn drive (e.g. "Cert"/"Certificate"/"WSMan") result = AutomountFileSystemDrive(name) ?? AutomountBuiltInDrive(name); - // if it didn't work, then try automounting as a BuiltIn drive (e.g. "Cert"/"Certificate"/"WSMan") - - - // internally this calls GetDrive(name, false) - } if (result == null) @@ -744,6 +740,9 @@ private PSDriveInfo AutomountFileSystemDrive(System.IO.DriveInfo systemDriveInfo /// /// Auto-mounts a built-in drive. /// + /// + /// Calls GetDrive(name, false) internally. + /// /// The name of the drive to load. /// internal PSDriveInfo AutomountBuiltInDrive(string name) diff --git a/src/System.Management.Automation/engine/lang/parserutils.cs b/src/System.Management.Automation/engine/lang/parserutils.cs index 97e0f771527..353c6241495 100644 --- a/src/System.Management.Automation/engine/lang/parserutils.cs +++ b/src/System.Management.Automation/engine/lang/parserutils.cs @@ -1177,13 +1177,10 @@ internal static object MatchOperator(ExecutionContext context, IScriptExtent err // if passed an explicit regex, just use it // otherwise compile the expression. - Regex r = PSObject.Base(rval) as Regex ?? NewRegex(PSObject.ToStringParser(context, rval), reOptions); - - - // In this situation, creation of Regex should not fail. We are not - // processing ArgumentException in this case. - - + // In this situation, creation of Regex should not fail. We are not + // processing ArgumentException in this case. + Regex r = PSObject.Base(rval) as Regex + ?? NewRegex(PSObject.ToStringParser(context, rval), reOptions); IEnumerator list = LanguagePrimitives.GetEnumerator(lval); if (list == null) diff --git a/src/System.Management.Automation/help/HelpProvider.cs b/src/System.Management.Automation/help/HelpProvider.cs index 88d9dc3f929..76ce3168432 100644 --- a/src/System.Management.Automation/help/HelpProvider.cs +++ b/src/System.Management.Automation/help/HelpProvider.cs @@ -225,15 +225,9 @@ internal string GetDefaultShellSearchPath() { string shellID = this.HelpSystem.ExecutionContext.ShellID; // Beginning in PowerShell 6.0.0.12, the $pshome is no longer registry specified, we search the application base instead. - string returnValue = Utils.GetApplicationBase(shellID) ?? Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName); - - - - // use executing assemblies location in case registry entry not found - - - - return returnValue; + // We use executing assemblies location in case registry entry not found + return Utils.GetApplicationBase(shellID) + ?? Path.GetDirectoryName(PsUtils.GetMainModule(System.Diagnostics.Process.GetCurrentProcess()).FileName); } /// From 370b008b7a2161012f61b1d3fdadd6d1799fbe2e Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 29 May 2020 16:18:37 +0100 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Ilya --- src/System.Management.Automation/engine/PSConfiguration.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/PSConfiguration.cs b/src/System.Management.Automation/engine/PSConfiguration.cs index 49625084244..aacf5c50e32 100644 --- a/src/System.Management.Automation/engine/PSConfiguration.cs +++ b/src/System.Management.Automation/engine/PSConfiguration.cs @@ -229,15 +229,13 @@ internal bool IsImplicitWinCompatEnabled() internal string[] GetWindowsPowerShellCompatibilityModuleDenyList() { return ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityModuleDenyListKey) - ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityModuleDenyListKey) - ?? null; + ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityModuleDenyListKey); } internal string[] GetWindowsPowerShellCompatibilityNoClobberModuleList() { return ReadValueFromFile(ConfigScope.CurrentUser, WindowsPowerShellCompatibilityNoClobberModuleListKey) - ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityNoClobberModuleListKey) - ?? null; + ?? ReadValueFromFile(ConfigScope.AllUsers, WindowsPowerShellCompatibilityNoClobberModuleListKey); } /// From 6fd9426a3c44bdec444570206a13e9aa11cae9dd Mon Sep 17 00:00:00 2001 From: xtqqczze Date: Fri, 29 May 2020 18:37:19 +0100 Subject: [PATCH 4/4] Revert changes to wldpNativeMethods.cs --- .../security/wldpNativeMethods.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/security/wldpNativeMethods.cs b/src/System.Management.Automation/security/wldpNativeMethods.cs index 1a18014696b..974392424eb 100644 --- a/src/System.Management.Automation/security/wldpNativeMethods.cs +++ b/src/System.Management.Automation/security/wldpNativeMethods.cs @@ -82,7 +82,7 @@ public static SystemEnforcementMode GetLockdownPolicy(string path, SafeHandle ha // At this point, LockdownPolicy = Audit or Allowed. // If there was a WLDP policy, but WLDP didn't block it, // then it was explicitly allowed. Therefore, return the result for the file. - SystemEnforcementMode systemWldpPolicy = s_cachedWldpSystemPolicy ?? SystemEnforcementMode.None; + SystemEnforcementMode systemWldpPolicy = s_cachedWldpSystemPolicy.GetValueOrDefault(SystemEnforcementMode.None); if ((systemWldpPolicy == SystemEnforcementMode.Enforce) || (systemWldpPolicy == SystemEnforcementMode.Audit)) { @@ -98,7 +98,7 @@ public static SystemEnforcementMode GetLockdownPolicy(string path, SafeHandle ha // If there was a system-wide AppLocker policy, but AppLocker didn't block it, // then return AppLocker's status. - if ((s_cachedSaferSystemPolicy ?? SaferPolicy.Allowed) == + if (s_cachedSaferSystemPolicy.GetValueOrDefault(SaferPolicy.Allowed) == SaferPolicy.Disallowed) { return lockdownPolicy; @@ -115,7 +115,7 @@ private static SystemEnforcementMode GetWldpPolicy(string path, SafeHandle handl // If the WLDP assembly is missing (such as windows 7 or down OS), return default/None to skip WLDP validation if (s_hadMissingWldpAssembly) { - return s_cachedWldpSystemPolicy ?? SystemEnforcementMode.None; + return s_cachedWldpSystemPolicy.GetValueOrDefault(SystemEnforcementMode.None); } // If path is NULL, see if we have the cached system-wide lockdown policy. @@ -168,7 +168,7 @@ private static SystemEnforcementMode GetWldpPolicy(string path, SafeHandle handl catch (DllNotFoundException) { s_hadMissingWldpAssembly = true; - return s_cachedWldpSystemPolicy ?? SystemEnforcementMode.None; + return s_cachedWldpSystemPolicy.GetValueOrDefault(SystemEnforcementMode.None); } } @@ -384,7 +384,7 @@ private static SystemEnforcementMode GetDebugLockdownPolicy(string path) // No explicit debug allowance for the file, so return the system policy if there // is one. - return s_systemLockdownPolicy ?? SystemEnforcementMode.None; + return s_systemLockdownPolicy.GetValueOrDefault(SystemEnforcementMode.None); } // Support fall-back debug hook for system-wide policy on non-WOA platforms