Alexandre Julliard : ntdll: Simplify the sanity checks in RtlFreeHeap and RtlReAllocateHeap.

Alexandre Julliard julliard at wine.codeweavers.com
Fri Jul 21 13:49:18 CDT 2006


Module: wine
Branch: refs/heads/master
Commit: d49884c3e957c2a4218316232a6239c388541df1
URL:    http://source.winehq.org/git/?p=wine.git;a=commit;h=d49884c3e957c2a4218316232a6239c388541df1

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Fri Jul 21 13:22:13 2006 +0200

ntdll: Simplify the sanity checks in RtlFreeHeap and RtlReAllocateHeap.

---

 dlls/ntdll/heap.c |   48 ++++++++++++++++++++++++------------------------
 1 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/dlls/ntdll/heap.c b/dlls/ntdll/heap.c
index a2e76dd..d02e61a 100644
--- a/dlls/ntdll/heap.c
+++ b/dlls/ntdll/heap.c
@@ -969,12 +969,6 @@ static BOOL HEAP_IsRealArena( HEAP *heap
     SUBHEAP *subheap;
     BOOL ret = TRUE;
 
-    if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
-    {
-        ERR("Invalid heap %p!\n", heapPtr );
-        return FALSE;
-    }
-
     flags &= HEAP_NO_SERIALIZE;
     flags |= heapPtr->flags;
     /* calling HeapLock may result in infinite recursion, so do the critsect directly */
@@ -1233,24 +1227,28 @@ BOOLEAN WINAPI RtlFreeHeap( HANDLE heap,
     flags &= HEAP_NO_SERIALIZE;
     flags |= heapPtr->flags;
     if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
-    if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
-    {
-        if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
-        RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
-        TRACE("(%p,%08lx,%p): returning FALSE\n", heap, flags, ptr );
-        return FALSE;
-    }
 
-    /* Turn the block into a free block */
+    /* Some sanity checks */
 
     pInUse  = (ARENA_INUSE *)ptr - 1;
-    subheap = HEAP_FindSubHeap( heapPtr, pInUse );
+    if (!(subheap = HEAP_FindSubHeap( heapPtr, pInUse ))) goto error;
+    if ((char *)pInUse < (char *)subheap + subheap->headerSize) goto error;
+    if (!HEAP_ValidateInUseArena( subheap, pInUse, QUIET )) goto error;
+
+    /* Turn the block into a free block */
+
     HEAP_MakeInUseBlockFree( subheap, pInUse );
 
     if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
 
     TRACE("(%p,%08lx,%p): returning TRUE\n", heap, flags, ptr );
     return TRUE;
+
+error:
+    if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
+    RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
+    TRACE("(%p,%08lx,%p): returning FALSE\n", heap, flags, ptr );
+    return FALSE;
 }
 
 
@@ -1292,18 +1290,14 @@ PVOID WINAPI RtlReAllocateHeap( HANDLE h
     if (rounded_size < HEAP_MIN_DATA_SIZE) rounded_size = HEAP_MIN_DATA_SIZE;
 
     if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
-    if (!HEAP_IsRealArena( heapPtr, HEAP_NO_SERIALIZE, ptr, QUIET ))
-    {
-        if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
-        RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
-        TRACE("(%p,%08lx,%p,%08lx): returning NULL\n", heap, flags, ptr, size );
-        return NULL;
-    }
+
+    pArena = (ARENA_INUSE *)ptr - 1;
+    if (!(subheap = HEAP_FindSubHeap( heapPtr, pArena ))) goto error;
+    if ((char *)pArena < (char *)subheap + subheap->headerSize) goto error;
+    if (!HEAP_ValidateInUseArena( subheap, pArena, QUIET )) goto error;
 
     /* Check if we need to grow the block */
 
-    pArena = (ARENA_INUSE *)ptr - 1;
-    subheap = HEAP_FindSubHeap( heapPtr, pArena );
     oldSize = (pArena->size & ARENA_SIZE_MASK);
     if (rounded_size > oldSize)
     {
@@ -1380,6 +1374,12 @@ PVOID WINAPI RtlReAllocateHeap( HANDLE h
 
     TRACE("(%p,%08lx,%p,%08lx): returning %p\n", heap, flags, ptr, size, pArena + 1 );
     return (LPVOID)(pArena + 1);
+
+error:
+    if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
+    RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
+    TRACE("(%p,%08lx,%p,%08lx): returning NULL\n", heap, flags, ptr, size );
+    return NULL;
 }
 
 




More information about the wine-cvs mailing list