]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/TianoCompress: Avoid possible NULL pointer dereference
authorHao Wu <hao.a.wu@intel.com>
Tue, 11 Oct 2016 02:30:49 +0000 (10:30 +0800)
committerHao Wu <hao.a.wu@intel.com>
Tue, 8 Nov 2016 08:36:33 +0000 (16:36 +0800)
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/C/TianoCompress/TianoCompress.c

index 70f1b61a5ca31547b45b1b53097cd4b711e2380f..57253cca8f792de44407e2d6d19f1277dbea432e 100644 (file)
@@ -5,7 +5,7 @@ and Pointers to repeated strings.
 This sequence is further divided into Blocks and Huffman codings are applied to \r
 each Block.\r
   \r
-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2007 - 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
@@ -240,6 +240,10 @@ Returns:
   UINT32  Index;\r
 \r
   mText = malloc (WNDSIZ * 2 + MAXMATCH);\r
+  if (mText == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   for (Index = 0; Index < WNDSIZ * 2 + MAXMATCH; Index++) {\r
     mText[Index] = 0;\r
   }\r
@@ -250,6 +254,11 @@ Returns:
   mParent     = malloc (WNDSIZ * 2 * sizeof (*mParent));\r
   mPrev       = malloc (WNDSIZ * 2 * sizeof (*mPrev));\r
   mNext       = malloc ((MAX_HASH_VAL + 1) * sizeof (*mNext));\r
+  if (mLevel == NULL || mChildCount == NULL || mPosition == NULL ||\r
+    mParent == NULL || mPrev == NULL || mNext == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
 \r
   mBufSiz     = BLKSIZ;\r
   mBuf        = malloc (mBufSiz);\r
@@ -1911,20 +1920,18 @@ Returns:
     free(FileBuffer);\r
     return 1;\r
   }\r
-   \r
-  if (OutputFileName != NULL) {\r
-    OutputFile = fopen (LongFilePath (OutputFileName), "wb");\r
-    if (OutputFile == NULL) {\r
-      Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName);\r
+\r
+  if (OutputFileName == NULL) {\r
+    OutputFileName = DEFAULT_OUTPUT_FILE;\r
+  }\r
+  OutputFile = fopen (LongFilePath (OutputFileName), "wb");\r
+  if (OutputFile == NULL) {\r
+    Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName);\r
     if (InputFile != NULL) {\r
       fclose (InputFile);\r
-      }\r
-      goto ERROR;\r
-      }\r
-    } else {\r
-      OutputFileName = DEFAULT_OUTPUT_FILE;\r
-      OutputFile = fopen (LongFilePath (OutputFileName), "wb");\r
     }\r
+    goto ERROR;\r
+  }\r
     \r
   if (ENCODE) {\r
   //\r
@@ -1942,12 +1949,18 @@ Returns:
       goto ERROR;\r
     }\r
   }\r
+\r
   Status = TianoCompress ((UINT8 *)FileBuffer, InputLength, OutBuffer, &DstSize);\r
   if (Status != EFI_SUCCESS) {\r
     Error (NULL, 0, 0007, "Error compressing file", NULL);\r
     goto ERROR;\r
   }\r
 \r
+  if (OutBuffer == NULL) {\r
+    Error (NULL, 0, 4001, "Resource:", "Memory cannot be allocated!");\r
+    goto ERROR;\r
+  }\r
+\r
   fwrite(OutBuffer,(size_t)DstSize, 1, OutputFile);\r
   free(Scratch);\r
   free(FileBuffer);\r