[PATCH 1/4] kernel32: Fix indentation of Global* functions.

Rémi Bernon rbernon at codeweavers.com
Tue Mar 29 08:24:36 CDT 2022


Signed-off-by: Rémi Bernon <rbernon at codeweavers.com>
---

My intent is to cleanup these functions to then re-implement them using
a static handle table, shared between kernelbase and kernel32 through
KernelBaseGetGlobalData(), as native does it (although we don't need the
global data structure to be necessarily compatible).

The idea, is ultimately to have these functions a bit more robust, less
dependent on exception handlers and heap pointers validation, and less
dependent on the heap implementation itself. Native doesn't seem to be
actually catching exceptions and it's possible to get crashes with bad
pointer values.

 dlls/kernel32/heap.c | 163 +++++++++++++++++++++----------------------
 1 file changed, 81 insertions(+), 82 deletions(-)

diff --git a/dlls/kernel32/heap.c b/dlls/kernel32/heap.c
index 8e767d03211..7e366eb991e 100644
--- a/dlls/kernel32/heap.c
+++ b/dlls/kernel32/heap.c
@@ -231,11 +231,10 @@ BOOL WINAPI GlobalUnlock(HGLOBAL hmem)
  *      Handle: Success
  *      NULL: Failure
  */
-HGLOBAL WINAPI GlobalHandle(
-                 LPCVOID pmem /* [in] Pointer to global memory block */
-) {
+HGLOBAL WINAPI GlobalHandle( LPCVOID pmem )
+{
     HGLOBAL handle;
-    PGLOBAL32_INTERN  maybe_intern;
+    PGLOBAL32_INTERN maybe_intern;
     LPCVOID test;
 
     if (!pmem)
@@ -244,7 +243,7 @@ HGLOBAL WINAPI GlobalHandle(
         return 0;
     }
 
-    RtlLockHeap(GetProcessHeap());
+    RtlLockHeap( GetProcessHeap() );
     __TRY
     {
         handle = 0;
@@ -252,22 +251,25 @@ HGLOBAL WINAPI GlobalHandle(
         /* note that if pmem is a pointer to a block allocated by        */
         /* GlobalAlloc with GMEM_MOVEABLE then magic test in HeapValidate  */
         /* will fail.                                                      */
-        if (ISPOINTER(pmem)) {
-            if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, pmem )) {
-                handle = (HGLOBAL)pmem;  /* valid fixed block */
+        if (ISPOINTER( pmem ))
+        {
+            if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, pmem ))
+            {
+                handle = (HGLOBAL)pmem; /* valid fixed block */
                 break;
             }
-            handle = POINTER_TO_HANDLE(pmem);
-        } else
-            handle = (HGLOBAL)pmem;
+            handle = POINTER_TO_HANDLE( pmem );
+        }
+        else handle = (HGLOBAL)pmem;
 
         /* Now test handle either passed in or retrieved from pointer */
         maybe_intern = HANDLE_TO_INTERN( handle );
-        if (maybe_intern->Magic == MAGIC_GLOBAL_USED) {
+        if (maybe_intern->Magic == MAGIC_GLOBAL_USED)
+        {
             test = maybe_intern->Pointer;
             if (HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, (const char *)test - HGLOBAL_STORAGE ) && /* obj(-handle) valid arena? */
-                HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, maybe_intern ))  /* intern valid arena? */
-                break;  /* valid moveable block */
+                HeapValidate( GetProcessHeap(), HEAP_NO_SERIALIZE, maybe_intern )) /* intern valid arena? */
+                break; /* valid moveable block */
         }
         handle = 0;
         SetLastError( ERROR_INVALID_HANDLE );
@@ -278,12 +280,11 @@ HGLOBAL WINAPI GlobalHandle(
         handle = 0;
     }
     __ENDTRY
-    RtlUnlockHeap(GetProcessHeap());
+    RtlUnlockHeap( GetProcessHeap() );
 
     return handle;
 }
 
-
 /***********************************************************************
  *           GlobalReAlloc   (KERNEL32.@)
  *
@@ -317,50 +318,49 @@ HGLOBAL WINAPI GlobalReAlloc( HGLOBAL hmem, SIZE_T size, UINT flags )
  */
 SIZE_T WINAPI GlobalSize(HGLOBAL hmem)
 {
-   SIZE_T               retval;
-   PGLOBAL32_INTERN     pintern;
+    SIZE_T retval;
+    PGLOBAL32_INTERN pintern;
 
-   if (!((ULONG_PTR)hmem >> 16))
-   {
-       SetLastError(ERROR_INVALID_HANDLE);
-       return 0;
-   }
+    if (!((ULONG_PTR)hmem >> 16))
+    {
+        SetLastError( ERROR_INVALID_HANDLE );
+        return 0;
+    }
 
-   if(ISPOINTER(hmem))
-   {
-      retval=HeapSize(GetProcessHeap(), 0, hmem);
+    if (ISPOINTER( hmem ))
+    {
+        retval = HeapSize( GetProcessHeap(), 0, hmem );
+        if (retval == ~(SIZE_T)0) /* It might be a GMEM_MOVEABLE data pointer */
+        {
+            retval = HeapSize( GetProcessHeap(), 0, (char *)hmem - HGLOBAL_STORAGE );
+            if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
+        }
+    }
+    else
+    {
+        RtlLockHeap( GetProcessHeap() );
+        pintern = HANDLE_TO_INTERN( hmem );
 
-      if (retval == ~(SIZE_T)0) /* It might be a GMEM_MOVEABLE data pointer */
-      {
-          retval = HeapSize(GetProcessHeap(), 0, (char*)hmem - HGLOBAL_STORAGE);
-          if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
-      }
-   }
-   else
-   {
-      RtlLockHeap(GetProcessHeap());
-      pintern=HANDLE_TO_INTERN(hmem);
-
-      if(pintern->Magic==MAGIC_GLOBAL_USED)
-      {
-         if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
-             retval = 0;
-         else
-         {
-             retval = HeapSize(GetProcessHeap(), 0, (char *)pintern->Pointer - HGLOBAL_STORAGE );
-             if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
-         }
-      }
-      else
-      {
-         WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
-         SetLastError(ERROR_INVALID_HANDLE);
-         retval=0;
-      }
-      RtlUnlockHeap(GetProcessHeap());
-   }
-   if (retval == ~(SIZE_T)0) retval = 0;
-   return retval;
+        if (pintern->Magic == MAGIC_GLOBAL_USED)
+        {
+            if (!pintern->Pointer) /* handle case of GlobalAlloc( ??,0) */
+                retval = 0;
+            else
+            {
+                retval = HeapSize( GetProcessHeap(), 0, (char *)pintern->Pointer - HGLOBAL_STORAGE );
+                if (retval != ~(SIZE_T)0) retval -= HGLOBAL_STORAGE;
+            }
+        }
+        else
+        {
+            WARN( "invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic );
+            SetLastError( ERROR_INVALID_HANDLE );
+            retval = 0;
+        }
+        RtlUnlockHeap( GetProcessHeap() );
+    }
+    if (retval == ~(SIZE_T)0) retval = 0;
+    return retval;
 }
 
 
@@ -415,32 +415,31 @@ VOID WINAPI GlobalUnfix(HGLOBAL hmem)
  */
 UINT WINAPI GlobalFlags(HGLOBAL hmem)
 {
-   DWORD                retval;
-   PGLOBAL32_INTERN     pintern;
+    DWORD retval;
+    PGLOBAL32_INTERN pintern;
 
-   if(ISPOINTER(hmem))
-   {
-      retval=0;
-   }
-   else
-   {
-      RtlLockHeap(GetProcessHeap());
-      pintern=HANDLE_TO_INTERN(hmem);
-      if(pintern->Magic==MAGIC_GLOBAL_USED)
-      {
-         retval=pintern->LockCount + (pintern->Flags<<8);
-         if(pintern->Pointer==0)
-            retval|= GMEM_DISCARDED;
-      }
-      else
-      {
-         WARN("invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic);
-         SetLastError(ERROR_INVALID_HANDLE);
-         retval = GMEM_INVALID_HANDLE;
-      }
-      RtlUnlockHeap(GetProcessHeap());
-   }
-   return retval;
+    if (ISPOINTER( hmem ))
+    {
+        retval = 0;
+    }
+    else
+    {
+        RtlLockHeap( GetProcessHeap() );
+        pintern = HANDLE_TO_INTERN( hmem );
+        if (pintern->Magic == MAGIC_GLOBAL_USED)
+        {
+            retval = pintern->LockCount + (pintern->Flags << 8);
+            if (pintern->Pointer == 0) retval |= GMEM_DISCARDED;
+        }
+        else
+        {
+            WARN( "invalid handle %p (Magic: 0x%04x)\n", hmem, pintern->Magic );
+            SetLastError( ERROR_INVALID_HANDLE );
+            retval = GMEM_INVALID_HANDLE;
+        }
+        RtlUnlockHeap( GetProcessHeap() );
+    }
+    return retval;
 }
 
 
-- 
2.35.1




More information about the wine-devel mailing list