]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Common/Decompress.c
BaseTools/C/Common: Fix potential memory leak
[mirror_edk2.git] / BaseTools / Source / C / Common / Decompress.c
index a12fc595a7da5e17328def298debee32c46ed803..4b83e88210e14b89ff4ec704f5b6385dcad7c54b 100644 (file)
@@ -1,6 +1,8 @@
 /** @file\r
+Decompressor. Algorithm Ported from OPSD code (Decomp.asm) for Efi and Tiano \r
+compress algorithm.\r
 \r
-Copyright (c) 2004 - 2008, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -9,19 +11,11 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-Module Name:\r
-\r
-  Decompress.c\r
-\r
-Abstract:\r
-\r
-  Decompressor. Algorithm Ported from OPSD code (Decomp.asm)\r
-  for Efi and Tiano compress algorithm.\r
-\r
 --*/\r
 \r
 #include <stdlib.h>\r
 #include <string.h>\r
+#include <assert.h>\r
 #include "Decompress.h"\r
 \r
 //\r
@@ -247,7 +241,7 @@ Returns:
   for (Char = 0; Char < NumOfChar; Char++) {\r
 \r
     Len = BitLen[Char];\r
-    if (Len == 0) {\r
+    if (Len == 0 || Len >= 17) {\r
       continue;\r
     }\r
 \r
@@ -380,6 +374,8 @@ Returns:
   UINT16  Index;\r
   UINT32  Mask;\r
 \r
+  assert (nn <= NPT);\r
+\r
   Number = (UINT16) GetBits (Sd, nbit);\r
 \r
   if (Number == 0) {\r
@@ -682,7 +678,7 @@ Arguments:
 \r
 Returns:\r
 \r
-  EFI_SUCCESS           - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
+  EFI_SUCCESS           - The size of destination buffer and the size of scratch buffer are successfully retrieved.\r
   EFI_INVALID_PARAMETER - The source data is corrupted\r
 \r
 --*/\r
@@ -817,7 +813,7 @@ Arguments:
 \r
 Returns:\r
 \r
-  EFI_SUCCESS           - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
+  EFI_SUCCESS           - The size of destination buffer and the size of scratch buffer are successfully retrieved.\r
   EFI_INVALID_PARAMETER - The source data is corrupted\r
 \r
 --*/\r
@@ -847,7 +843,7 @@ Arguments:
 \r
 Returns:\r
 \r
-  EFI_SUCCESS           - The size of destination buffer and the size of scratch buffer are successull retrieved.\r
+  EFI_SUCCESS           - The size of destination buffer and the size of scratch buffer are successfully retrieved.\r
   EFI_INVALID_PARAMETER - The source data is corrupted\r
 \r
 --*/\r
@@ -938,7 +934,9 @@ Extract (
   UINT32        ScratchSize;\r
   EFI_STATUS    Status;\r
 \r
-  Status = EFI_SUCCESS;\r
+  Scratch = NULL;\r
+  Status  = EFI_SUCCESS;\r
+\r
   switch (Algorithm) {\r
   case 0:\r
     *Destination = (VOID *)malloc(SrcSize);\r
@@ -952,30 +950,44 @@ Extract (
     Status = EfiGetInfo(Source, SrcSize, DstSize, &ScratchSize);\r
     if (Status == EFI_SUCCESS) {\r
       Scratch = (VOID *)malloc(ScratchSize);\r
+      if (Scratch == NULL) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+\r
       *Destination = (VOID *)malloc(*DstSize);\r
-      if (Scratch != NULL && *Destination != NULL) {\r
-        Status = EfiDecompress(Source, SrcSize, *Destination, *DstSize, Scratch, ScratchSize);\r
-      } else {\r
-        Status = EFI_OUT_OF_RESOURCES;\r
+      if (*Destination == NULL) {\r
+        free (Scratch);\r
+        return EFI_OUT_OF_RESOURCES;\r
       }\r
+\r
+      Status = EfiDecompress(Source, SrcSize, *Destination, *DstSize, Scratch, ScratchSize);\r
     }\r
     break;\r
   case 2:\r
     Status = TianoGetInfo(Source, SrcSize, DstSize, &ScratchSize);\r
     if (Status == EFI_SUCCESS) {\r
       Scratch = (VOID *)malloc(ScratchSize);\r
+      if (Scratch == NULL) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+\r
       *Destination = (VOID *)malloc(*DstSize);\r
-      if (Scratch != NULL && *Destination != NULL) {\r
-        Status = TianoDecompress(Source, SrcSize, *Destination, *DstSize, Scratch, ScratchSize);\r
-      } else {\r
-        Status = EFI_OUT_OF_RESOURCES;\r
+      if (*Destination == NULL) {\r
+        free (Scratch);\r
+        return EFI_OUT_OF_RESOURCES;\r
       }\r
+\r
+      Status = TianoDecompress(Source, SrcSize, *Destination, *DstSize, Scratch, ScratchSize);\r
     }\r
     break;\r
   default:\r
     Status = EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  if (Scratch != NULL) {\r
+    free (Scratch);\r
+  }\r
+\r
   return Status;\r
 }\r
 \r