From 1a445776b1de1c7c34988a57038ee1eb6c2a7417 Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Thu, 7 Nov 2019 22:57:43 -0500 Subject: [PATCH] Move NativeLibrary.Free null check to managed (dotnet/coreclr#27744) Commit migrated from https://github.com/dotnet/coreclr/commit/4ef94ad5370a2c882e6a494ba64a4552572f8694 --- src/coreclr/src/vm/dllimport.cpp | 6 +----- .../src/System/Runtime/InteropServices/NativeLibrary.cs | 2 ++ 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/coreclr/src/vm/dllimport.cpp b/src/coreclr/src/vm/dllimport.cpp index f4ec198..04fc76c 100644 --- a/src/coreclr/src/vm/dllimport.cpp +++ b/src/coreclr/src/vm/dllimport.cpp @@ -6519,11 +6519,7 @@ NATIVE_LIBRARY_HANDLE NDirect::LoadLibraryModuleBySearch(NDirectMethodDesc * pMD void NDirect::FreeNativeLibrary(NATIVE_LIBRARY_HANDLE handle) { STANDARD_VM_CONTRACT; - - // FreeLibrary doesn't throw if the input is null. - // This avoids further null propagation/check while freeing resources (ex: in finally blocks) - if (handle == NULL) - return; + _ASSERTE(handle != NULL); #ifndef FEATURE_PAL BOOL retVal = FreeLibrary(handle); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs index 8f57407..9bd65fe 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs @@ -130,6 +130,8 @@ namespace System.Runtime.InteropServices /// The native library handle to be freed public static void Free(IntPtr handle) { + if (handle == IntPtr.Zero) + return; FreeLib(handle); } -- 2.7.4