From bb2f8a0b4b62a9050b1f0f574a5aa9485b6f2832 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 14 Mar 2024 09:46:59 -0700 Subject: [PATCH] Fix a null reference exception when 'Runspace.DefaultRunspace' is null --- .../security/wldpNativeMethods.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/security/wldpNativeMethods.cs b/src/System.Management.Automation/security/wldpNativeMethods.cs index a95a78dbdbe..a59f37c0a8f 100644 --- a/src/System.Management.Automation/security/wldpNativeMethods.cs +++ b/src/System.Management.Automation/security/wldpNativeMethods.cs @@ -8,6 +8,7 @@ using System.Diagnostics.CodeAnalysis; using System.Management.Automation.Internal; +using System.Management.Automation.Runspaces; using System.Management.Automation.Tracing; using System.Runtime.InteropServices; @@ -91,9 +92,8 @@ internal static void LogWDACAuditMessage( string messageToWrite = message; // Augment the log message with current script information from the script debugger, if available. - context ??= System.Management.Automation.Runspaces.LocalPipeline.GetExecutionContextFromTLS(); + context ??= LocalPipeline.GetExecutionContextFromTLS(); bool debuggerAvailable = context is not null && - context._debugger is not null && context._debugger is ScriptDebugger; if (debuggerAvailable) @@ -108,10 +108,9 @@ context._debugger is not null && PSEtwLog.LogWDACAuditEvent(title, messageToWrite, fqid); // We drop into the debugger only if requested and we are running in the interactive host session runspace (Id == 1). - if (debuggerAvailable && - dropIntoDebugger is true && + if (debuggerAvailable && dropIntoDebugger && context._debugger.DebugMode.HasFlag(DebugModes.LocalScript) && - System.Management.Automation.Runspaces.Runspace.DefaultRunspace.Id == 1 && + Runspace.DefaultRunspace?.Id == 1 && context.DebugPreferenceVariable.HasFlag(ActionPreference.Break) && context.InternalHost?.UI is not null) {