Fix crash in SafePipeHandle.ReleaseHandle() on Windows (#3797)
authorNiklas Therning <niklas@therning.org>
Fri, 21 Oct 2016 01:52:38 +0000 (03:52 +0200)
committerMiguel de Icaza <miguel@gnome.org>
Fri, 21 Oct 2016 01:52:38 +0000 (20:52 -0500)
The ReleaseHandle() methods tries to deallocate the handle as if it was a
pointer to heap memory. But on Windows it's a HANDLE. The call to
Marshal.FreeHGlobal() causes a crash on Windows. It doesn't crash on other
platforms probably due to the emulated handles which are indeed heap allocated
memory objects. But the underlying file descriptor is never closed so there
should be a file descriptor leak on other platforms.

This patch fixes the issue by calling MonoIO.Close() on the handle rather than
Marshal.FreeHGlobal()

mcs/class/System.Core/Microsoft.Win32.SafeHandles/SafePipeHandle.cs

index e2e8aaeba0c3fb58714b37f5cf13f816bf618d68..0c5768009b5b299ad165eca954ad455a1c04867e 100644 (file)
@@ -46,12 +46,8 @@ namespace Microsoft.Win32.SafeHandles
 
                protected override bool ReleaseHandle ()
                {
-                       try {
-                               Marshal.FreeHGlobal (handle);
-                               return true;
-                       } catch (ArgumentException) {
-                               return false;
-                       }
+                       MonoIOError error;
+                       return MonoIO.Close (handle, out error);
                }
        }
 }