From c4e144b13e1209a6a723160d38be9f11ab45b8da Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Fri, 31 Jan 2020 05:13:12 +0000 Subject: [PATCH] Replace use of throw GetExceptionForHR with ThrowExceptionForHR (#11640) `System.Runtime.InteropServices.Marshall.GetExceptionForHR` may not return an exception. Instead of `throw GetExceptionForHR` we can use `ThrowExceptionForHR`. Fix warnings `CS8597:Thrown value may be null.` --- .../security/CertificateProvider.cs | 2 +- .../engine/parser/FusionAssemblyIdentity.cs | 19 +++++-------------- .../security/SecuritySupport.cs | 3 +-- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs b/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs index 014eb6db92..fa23e31a47 100644 --- a/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs +++ b/src/Microsoft.PowerShell.Security/security/CertificateProvider.cs @@ -3088,7 +3088,7 @@ namespace Microsoft.PowerShell.Commands } else { - throw Marshal.GetExceptionForHR(Security.NativeMethods.NTE_NOT_SUPPORTED); + Marshal.ThrowExceptionForHR(Security.NativeMethods.NTE_NOT_SUPPORTED); } } diff --git a/src/System.Management.Automation/engine/parser/FusionAssemblyIdentity.cs b/src/System.Management.Automation/engine/parser/FusionAssemblyIdentity.cs index 8eebb804dd..6970e17b7c 100644 --- a/src/System.Management.Automation/engine/parser/FusionAssemblyIdentity.cs +++ b/src/System.Management.Automation/engine/parser/FusionAssemblyIdentity.cs @@ -133,17 +133,14 @@ namespace Microsoft.CodeAnalysis if (hr != ERROR_INSUFFICIENT_BUFFER) { - throw Marshal.GetExceptionForHR(hr); + Marshal.ThrowExceptionForHR(hr); } byte[] data = new byte[(int)characterCountIncludingTerminator * 2]; fixed (byte* p = data) { hr = nameObject.GetDisplayName(p, ref characterCountIncludingTerminator, displayFlags); - if (hr != 0) - { - throw Marshal.GetExceptionForHR(hr); - } + Marshal.ThrowExceptionForHR(hr); return Marshal.PtrToStringUni((IntPtr)p, (int)characterCountIncludingTerminator - 1); } @@ -162,17 +159,14 @@ namespace Microsoft.CodeAnalysis if (hr != ERROR_INSUFFICIENT_BUFFER) { - throw Marshal.GetExceptionForHR(hr); + Marshal.ThrowExceptionForHR(hr); } byte[] data = new byte[(int)size]; fixed (byte* p = data) { hr = nameObject.GetProperty(propertyId, p, ref size); - if (hr != 0) - { - throw Marshal.GetExceptionForHR(hr); - } + Marshal.ThrowExceptionForHR(hr); } return data; @@ -210,10 +204,7 @@ namespace Microsoft.CodeAnalysis uint result; uint size = sizeof(uint); int hr = nameObject.GetProperty(propertyId, &result, ref size); - if (hr != 0) - { - throw Marshal.GetExceptionForHR(hr); - } + Marshal.ThrowExceptionForHR(hr); if (size == 0) { diff --git a/src/System.Management.Automation/security/SecuritySupport.cs b/src/System.Management.Automation/security/SecuritySupport.cs index 41c92c0191..ee1058be35 100644 --- a/src/System.Management.Automation/security/SecuritySupport.cs +++ b/src/System.Management.Automation/security/SecuritySupport.cs @@ -900,8 +900,7 @@ namespace System.Management.Automation.Internal if (newfilter.Contains("=") || newfilter.Contains("&")) { - throw Marshal.GetExceptionForHR( - Security.NativeMethods.E_INVALID_DATA); + Marshal.ThrowExceptionForHR(Security.NativeMethods.E_INVALID_DATA); } newfilter = name + "=" + newfilter;