From 055e3c33274b41551cfbfb1a104b63f8e774394f Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Sat, 2 Aug 2025 15:28:00 +0100 Subject: [PATCH 1/2] Fix CA2020: Prevent behavioral change https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca2020 --- .globalconfig | 4 ++++ .../engine/remoting/fanin/WSManNativeAPI.cs | 2 +- src/powershell/Program.cs | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.globalconfig b/.globalconfig index d51e5cfacfa..f1511f2b5d2 100644 --- a/.globalconfig +++ b/.globalconfig @@ -558,6 +558,10 @@ dotnet_diagnostic.CA2015.severity = warning # https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2016 dotnet_diagnostic.CA2016.severity = suggestion +# CA2020: Prevent behavioral change +# https://learn.microsoft.com/en-gb/dotnet/fundamentals/code-analysis/quality-rules/ca2020 +dotnet_diagnostic.CA2020.severity = warning + # CA2100: Review SQL queries for security vulnerabilities # https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2100 dotnet_diagnostic.CA2100.severity = none diff --git a/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs b/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs index 289f88c576f..c995e343db4 100644 --- a/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs +++ b/src/System.Management.Automation/engine/remoting/fanin/WSManNativeAPI.cs @@ -1121,7 +1121,7 @@ internal WSManOptionSet(WSManOption[] options) // Look at the structure of native WSManOptionSet.. Options is a pointer.. // In C-Style array individual elements are continuous..so I am building // continuous array elements here. - Marshal.StructureToPtr(options[index], (IntPtr)(_optionSet.options.ToInt64() + (sizeOfOption * index)), false); + Marshal.StructureToPtr(options[index], _optionSet.options + (sizeOfOption * index), false); } _data = MarshalledObject.Create(_optionSet); diff --git a/src/powershell/Program.cs b/src/powershell/Program.cs index 70346a1d1ce..1b50f40e2f8 100644 --- a/src/powershell/Program.cs +++ b/src/powershell/Program.cs @@ -116,7 +116,7 @@ private static void AttemptExecPwshLogin(string[] args) // Read the symlink to the startup executable IntPtr linkPathPtr = Marshal.AllocHGlobal(LINUX_PATH_MAX); IntPtr bufSize = ReadLink("/proc/self/exe", linkPathPtr, (UIntPtr)LINUX_PATH_MAX); - pwshPath = Marshal.PtrToStringAnsi(linkPathPtr, (int)bufSize); + pwshPath = Marshal.PtrToStringAnsi(linkPathPtr, checked((int)bufSize)); Marshal.FreeHGlobal(linkPathPtr); ArgumentNullException.ThrowIfNull(pwshPath); From 96977eb801f5e551ae84e278f6e900ce172af22f Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Tue, 12 Aug 2025 03:14:53 +0100 Subject: [PATCH 2/2] Update ComUtil.cs --- .../engine/COM/ComUtil.cs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/System.Management.Automation/engine/COM/ComUtil.cs b/src/System.Management.Automation/engine/COM/ComUtil.cs index e244e4cdf3f..231971f0da1 100644 --- a/src/System.Management.Automation/engine/COM/ComUtil.cs +++ b/src/System.Management.Automation/engine/COM/ComUtil.cs @@ -63,20 +63,7 @@ internal static string GetMethodSignatureFromFuncDesc(COM.ITypeInfo typeinfo, CO ElementDescription = new COM.ELEMDESC(); ElementDescriptionArrayByteOffset = i * ElementDescriptionSize; - - // Disable PRefast warning for converting to int32 and converting back into intptr. - // Code below takes into account 32 bit vs 64 bit conversions -#pragma warning disable 56515 - if (IntPtr.Size == 4) - { - ElementDescriptionPointer = (IntPtr)(ElementDescriptionArrayPtr.ToInt32() + ElementDescriptionArrayByteOffset); - } - else - { - ElementDescriptionPointer = (IntPtr)(ElementDescriptionArrayPtr.ToInt64() + ElementDescriptionArrayByteOffset); - } -#pragma warning restore 56515 - + ElementDescriptionPointer = ElementDescriptionArrayPtr + ElementDescriptionArrayByteOffset; ElementDescription = Marshal.PtrToStructure(ElementDescriptionPointer); string paramstring = GetStringFromTypeDesc(typeinfo, ElementDescription.tdesc);