BaseTools/TianoCompress: Avoid possible NULL pointer dereference

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>
diff --git a/BaseTools/Source/C/TianoCompress/TianoCompress.c b/BaseTools/Source/C/TianoCompress/TianoCompress.c
index 70f1b61..57253cc 100644
--- a/BaseTools/Source/C/TianoCompress/TianoCompress.c
+++ b/BaseTools/Source/C/TianoCompress/TianoCompress.c
@@ -5,7 +5,7 @@
 This sequence is further divided into Blocks and Huffman codings are applied to 

 each Block.

   

-Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>

+Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>

 This program and the accompanying materials                          

 are licensed and made available under the terms and conditions of the BSD License         

 which accompanies this distribution.  The full text of the license may be found at        

@@ -240,6 +240,10 @@
   UINT32  Index;

 

   mText = malloc (WNDSIZ * 2 + MAXMATCH);

+  if (mText == NULL) {

+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");

+    return EFI_OUT_OF_RESOURCES;

+  }

   for (Index = 0; Index < WNDSIZ * 2 + MAXMATCH; Index++) {

     mText[Index] = 0;

   }

@@ -250,6 +254,11 @@
   mParent     = malloc (WNDSIZ * 2 * sizeof (*mParent));

   mPrev       = malloc (WNDSIZ * 2 * sizeof (*mPrev));

   mNext       = malloc ((MAX_HASH_VAL + 1) * sizeof (*mNext));

+  if (mLevel == NULL || mChildCount == NULL || mPosition == NULL ||

+    mParent == NULL || mPrev == NULL || mNext == NULL) {

+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");

+    return EFI_OUT_OF_RESOURCES;

+  }

 

   mBufSiz     = BLKSIZ;

   mBuf        = malloc (mBufSiz);

@@ -1911,20 +1920,18 @@
     free(FileBuffer);

     return 1;

   }

-   

-  if (OutputFileName != NULL) {

-    OutputFile = fopen (LongFilePath (OutputFileName), "wb");

-    if (OutputFile == NULL) {

-      Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName);

+

+  if (OutputFileName == NULL) {

+    OutputFileName = DEFAULT_OUTPUT_FILE;

+  }

+  OutputFile = fopen (LongFilePath (OutputFileName), "wb");

+  if (OutputFile == NULL) {

+    Error (NULL, 0, 0001, "Error opening output file for writing", OutputFileName);

     if (InputFile != NULL) {

       fclose (InputFile);

-      }

-      goto ERROR;

-      }

-    } else {

-      OutputFileName = DEFAULT_OUTPUT_FILE;

-      OutputFile = fopen (LongFilePath (OutputFileName), "wb");

     }

+    goto ERROR;

+  }

     

   if (ENCODE) {

   //

@@ -1942,12 +1949,18 @@
       goto ERROR;

     }

   }

+

   Status = TianoCompress ((UINT8 *)FileBuffer, InputLength, OutBuffer, &DstSize);

   if (Status != EFI_SUCCESS) {

     Error (NULL, 0, 0007, "Error compressing file", NULL);

     goto ERROR;

   }

 

+  if (OutBuffer == NULL) {

+    Error (NULL, 0, 4001, "Resource:", "Memory cannot be allocated!");

+    goto ERROR;

+  }

+

   fwrite(OutBuffer,(size_t)DstSize, 1, OutputFile);

   free(Scratch);

   free(FileBuffer);