[Wine-patches] [eterhack 07/24] ntoskrnl.exe: Fix Io{Allocate, Free}Mdl to work in DriverEntry.

Alexander Morozov =?iso-8859-1?q?amorozov_=CE=C1_etersoft=2Eru?=
Ср Мар 4 12:25:16 MSK 2009


---
 dlls/ntoskrnl.exe/ntoskrnl.c |   52 +++++++++++++++++++++--------------------
 1 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/dlls/ntoskrnl.exe/ntoskrnl.c b/dlls/ntoskrnl.exe/ntoskrnl.c
index d51fadb..bf32101 100644
--- a/dlls/ntoskrnl.exe/ntoskrnl.c
+++ b/dlls/ntoskrnl.exe/ntoskrnl.c
@@ -878,39 +878,39 @@ PMDL WINAPI IoAllocateMdl( PVOID VirtualAddress, ULONG Length, BOOLEAN Secondary
     PMDL mdl;
     PVOID ptr;
     SIZE_T bytes_read;
+    DWORD process_id = get_pid();
 
     TRACE( "%p, %u, %i, %i, %p\n", VirtualAddress, Length, SecondaryBuffer,
             ChargeQuota, Irp );
 
-    ptr = ExAllocatePool( NonPagedPool, Length );
-    if (NULL == ptr)
-        return NULL;
-
     mdl = ExAllocatePool( NonPagedPool, sizeof(*mdl) );
     if (NULL == mdl)
-    {
-        ExFreePool(ptr);
         return NULL;
-    }
 
     RtlZeroMemory( mdl, sizeof(*mdl) );
     mdl->ByteCount = Length;
-    mdl->MappedSystemVa = ptr;
     mdl->StartVa = VirtualAddress;
 
-    process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, get_pid() );
-    if (NULL == process)
+    if (process_id)
     {
-        ExFreePool( mdl );
-        ExFreePool( ptr );
-        return NULL;
+        ptr = ExAllocatePool( NonPagedPool, Length );
+        if (NULL == ptr)
+            goto fail;
+        process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, process_id );
+        if (NULL == process)
+            goto fail;
+        NtReadVirtualMemory( process, VirtualAddress, ptr, Length, &bytes_read );
+        CloseHandle( process );
+        mdl->MappedSystemVa = ptr;
     }
-
-    NtReadVirtualMemory( process, VirtualAddress, ptr, Length, &bytes_read );
-
-    CloseHandle( process );
+    else
+        mdl->MappedSystemVa = VirtualAddress;
 
     return mdl;
+fail:
+    if (ptr) ExFreePool( ptr );
+    if (mdl) ExFreePool( mdl );
+    return NULL;
 }
 
 
@@ -921,19 +921,21 @@ void WINAPI IoFreeMdl( MDL *mdl )
 {
     HANDLE process;
     SIZE_T bytes_written;
+    DWORD process_id = get_pid();
 
     TRACE( "%p\n", mdl );
 
-    process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, get_pid() );
-    if (NULL != process)
+    if (process_id)
     {
-        NtWriteVirtualMemory( process, mdl->StartVa, mdl->MappedSystemVa,
-                mdl->ByteCount, &bytes_written );
-
-        CloseHandle( process );
+        process = OpenProcess( PROCESS_ALL_ACCESS, FALSE, process_id );
+        if (NULL != process)
+        {
+            NtWriteVirtualMemory( process, mdl->StartVa, mdl->MappedSystemVa,
+                    mdl->ByteCount, &bytes_written );
+            CloseHandle( process );
+        }
+        ExFreePool( mdl->MappedSystemVa );
     }
-
-    ExFreePool( mdl->MappedSystemVa );
     ExFreePool( mdl );
 }
 
-- 
1.6.1.3.GIT



Подробная информация о списке рассылки Wine-patches