Move NativeLibrary.Free null check to managed (dotnet/coreclr#27744)
authorRyan Lucia <rylucia@microsoft.com>
Fri, 8 Nov 2019 03:57:43 +0000 (22:57 -0500)
committerAaron Robinson <arobins@microsoft.com>
Fri, 8 Nov 2019 03:57:43 +0000 (22:57 -0500)
Commit migrated from https://github.com/dotnet/coreclr/commit/4ef94ad5370a2c882e6a494ba64a4552572f8694

src/coreclr/src/vm/dllimport.cpp
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs

index f4ec198..04fc76c 100644 (file)
@@ -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);
index 8f57407..9bd65fe 100644 (file)
@@ -130,6 +130,8 @@ namespace System.Runtime.InteropServices
         /// <param name="handle">The native library handle to be freed</param>
         public static void Free(IntPtr handle)
         {
+            if (handle == IntPtr.Zero)
+                return;
             FreeLib(handle);
         }