[ros-diffs] [navaraf] 20117: Check for failed allocations and fix some resource leaks.

navaraf at svn.reactos.com navaraf at svn.reactos.com
Mon Dec 12 21:46:35 CET 2005


Check for failed allocations and fix some resource leaks.
Modified: trunk/reactos/lib/user32/windows/bitmap.c
  _____  

Modified: trunk/reactos/lib/user32/windows/bitmap.c
--- trunk/reactos/lib/user32/windows/bitmap.c	2005-12-12 20:41:27 UTC
(rev 20116)
+++ trunk/reactos/lib/user32/windows/bitmap.c	2005-12-12 20:46:26 UTC
(rev 20117)
@@ -176,12 +176,18 @@

 
    IconDIR = MapViewOfFile(hSection, FILE_MAP_READ, 0, 0, 0);
    CloseHandle(hSection);
-   if (IconDIR == NULL || 0 != IconDIR->idReserved
-       || (IMAGE_ICON != IconDIR->idType && IMAGE_CURSOR !=
IconDIR->idType))
+   if (IconDIR == NULL)
    {
       return NULL;
    }
 
+   if (0 != IconDIR->idReserved ||
+       (IMAGE_ICON != IconDIR->idType && IMAGE_CURSOR !=
IconDIR->idType))
+   {
+      UnmapViewOfFile(IconDIR);
+      return NULL;
+   }
+
    /*
     * Get a handle to the screen dc, the icon we create is going to be
     * compatable with it.
@@ -213,11 +219,17 @@
    if (!dirEntry)
    {
       UnmapViewOfFile(IconDIR);
-      return(NULL);
+      return NULL;
    }
 
    SafeIconImage = RtlAllocateHeap(GetProcessHeap(), 0,
dirEntry->dwBytesInRes);
+   if (SafeIconImage == NULL)
+   {
+      UnmapViewOfFile(IconDIR);
+      return NULL;
+   }
    memcpy(SafeIconImage, ((PBYTE)IconDIR) + dirEntry->dwImageOffset,
dirEntry->dwBytesInRes);
+   UnmapViewOfFile(IconDIR);
 
    /* at this point we have a copy of the icon image to play with */
 
@@ -351,9 +363,9 @@
 			 0,
 			 NULL);
       if (hFile == NULL)
-	  {
-	    return(NULL);
-	  }
+      {
+          return NULL;
+      }
 
       hSection = CreateFileMappingW(hFile,
 				   NULL,
@@ -362,43 +374,42 @@
 				   0,
 				   NULL);
 
+      CloseHandle(hFile);
       if (hSection == NULL)
-	  {
-	    CloseHandle(hFile);
-	    return(NULL);
-	  }
+      {
+          return NULL;
+      }
+
       IconDIR = MapViewOfFile(hSection,
 				 FILE_MAP_READ,
 				 0,
 				 0,
 				 0);
+      CloseHandle(hSection);
+      if (IconDIR == NULL)
+      {
+          return NULL;
+      }
+      
+      if (0 != IconDIR->idReserved ||
+          (IMAGE_ICON != IconDIR->idType && IMAGE_CURSOR !=
IconDIR->idType))
+      {
+          UnmapViewOfFile(IconDIR);
+          return NULL;
+      }
 
-      if (IconDIR == NULL || 0 != IconDIR->idReserved
-          || (IMAGE_ICON != IconDIR->idType && IMAGE_CURSOR !=
IconDIR->idType))
-	  {
-	    CloseHandle(hFile);
-	    CloseHandle(hSection);
-	    return(NULL);
-	  }
-
       //pick the best size.
       dirEntry = (CURSORICONDIRENTRY *)  CURSORICON_FindBestIcon(
IconDIR, width, height, 1);
-
-
       if (!dirEntry)
-	  {
-	       CloseHandle(hFile);
-	       CloseHandle(hSection);
-	       UnmapViewOfFile(IconDIR);
-	       return(NULL);
-	  }
+      {
+          UnmapViewOfFile(IconDIR);
+          return NULL;
+      }
 
       SafeIconImage = RtlAllocateHeap(GetProcessHeap(), 0,
dirEntry->dwBytesInRes);
 
       memcpy(SafeIconImage, ((PBYTE)IconDIR) + dirEntry->dwImageOffset,
dirEntry->dwBytesInRes);
-
-      CloseHandle(hFile);
-      CloseHandle(hSection);
+      UnmapViewOfFile(IconDIR);
   }
 
   //at this point we have a copy of the icon image to play with
@@ -430,10 +441,9 @@
   if (hScreenDc == NULL)
   {
       if (fuLoad & LR_LOADFROMFILE)
-	  {
-	  	RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage);
-        UnmapViewOfFile(IconDIR);
-	  }
+      {
+         RtlFreeHeap(GetProcessHeap(), 0, SafeIconImage);
+      }
       return(NULL);
   }
 
@@ -684,6 +694,11 @@
 				if ((res = CreateBitmapIndirect(&bm)))
 				{
                     char *buf = HeapAlloc(GetProcessHeap(), 0,
bm.bmWidthBytes * bm.bmHeight);
+					if (buf == NULL)
+					{
+						DeleteObject(res);
+						return NULL;
+					}
 					GetBitmapBits(hnd,
bm.bmWidthBytes * bm.bmHeight, buf);
 					SetBitmapBits(res,
bm.bmWidthBytes * bm.bmHeight, buf);
 					HeapFree(GetProcessHeap(), 0,
buf);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.reactos.org/pipermail/ros-diffs/attachments/20051212/819895cc/attachment.html


More information about the Ros-diffs mailing list