]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools/VfrCompile: Add checks for user/file inputs
authorHao Wu <hao.a.wu@intel.com>
Thu, 3 Nov 2016 06:47:48 +0000 (14:47 +0800)
committerHao Wu <hao.a.wu@intel.com>
Tue, 8 Nov 2016 08:37:15 +0000 (16:37 +0800)
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Dandan Bi <dandan.bi@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/VfrCompile/VfrCompiler.cpp
BaseTools/Source/C/VfrCompile/VfrCompiler.h

index 59f4bf3a8e557733eea84ebdaeb466b2a249d8bd..16453439ec0b148bec6368b0dd408361037c7431 100644 (file)
@@ -68,15 +68,15 @@ CVfrCompiler::OptionInitialization (
   Status = EFI_SUCCESS;\r
   SetUtilityName ((CHAR8*) PROGRAM_NAME);\r
 \r
-  mOptions.VfrFileName[0]                = '\0';\r
-  mOptions.RecordListFile[0]             = '\0';\r
+  mOptions.VfrFileName                   = NULL;\r
+  mOptions.RecordListFile                = NULL;\r
   mOptions.CreateRecordListFile          = FALSE;\r
   mOptions.CreateIfrPkgFile              = FALSE;\r
-  mOptions.PkgOutputFileName[0]          = '\0';\r
-  mOptions.COutputFileName[0]            = '\0';\r
-  mOptions.OutputDirectory[0]            = '\0';\r
-  mOptions.PreprocessorOutputFileName[0] = '\0';\r
-  mOptions.VfrBaseFileName[0]            = '\0';\r
+  mOptions.PkgOutputFileName             = NULL;\r
+  mOptions.COutputFileName               = NULL;\r
+  mOptions.OutputDirectory               = NULL;\r
+  mOptions.PreprocessorOutputFileName    = NULL;\r
+  mOptions.VfrBaseFileName               = NULL;\r
   mOptions.IncludePaths                  = NULL;\r
   mOptions.SkipCPreprocessor             = TRUE;\r
   mOptions.CPreprocessorOptions          = NULL;\r
@@ -119,6 +119,16 @@ CVfrCompiler::OptionInitialization (
         DebugError (NULL, 0, 1001, "Missing option", "-o missing output directory name");\r
         goto Fail;\r
       }\r
+      if (strlen (Argv[Index]) > MAX_PATH - 1) {\r
+        DebugError (NULL, 0, 1003, "Invalid option value", "Output directory name %s is too long", Argv[Index]);\r
+        goto Fail;\r
+      }\r
+\r
+      mOptions.OutputDirectory = (CHAR8 *) malloc (strlen (Argv[Index]) + strlen ("\\") + 1);\r
+      if (mOptions.OutputDirectory == NULL) {\r
+        DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);\r
+        goto Fail;\r
+      }\r
       strcpy (mOptions.OutputDirectory, Argv[Index]);\r
       \r
       CHAR8 lastChar = mOptions.OutputDirectory[strlen(mOptions.OutputDirectory) - 1];\r
@@ -176,7 +186,25 @@ CVfrCompiler::OptionInitialization (
     DebugError (NULL, 0, 1001, "Missing option", "VFR file name is not specified.");\r
     goto Fail;\r
   } else {\r
+    if (strlen (Argv[Index]) > MAX_PATH) {\r
+      DebugError (NULL, 0, 1003, "Invalid option value", "VFR file name %s is too long.", Argv[Index]);\r
+      goto Fail;\r
+    }\r
+    mOptions.VfrFileName = (CHAR8 *) malloc (strlen (Argv[Index]) + 1);\r
+    if (mOptions.VfrFileName == NULL) {\r
+      DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);\r
+      goto Fail;\r
+    }\r
     strcpy (mOptions.VfrFileName, Argv[Index]);\r
+\r
+    if (mOptions.OutputDirectory == NULL) {\r
+      mOptions.OutputDirectory = (CHAR8 *) malloc (1);\r
+      if (mOptions.OutputDirectory == NULL) {\r
+        DebugError (NULL, 0, 4001, "Resource: memory can't be allocated", NULL);\r
+        goto Fail;\r
+      }\r
+      mOptions.OutputDirectory[0] = '\0';\r
+    }\r
   }\r
 \r
   if (SetBaseFileName() != 0) {\r
@@ -199,15 +227,37 @@ CVfrCompiler::OptionInitialization (
 Fail:\r
   SET_RUN_STATUS (STATUS_DEAD);\r
 \r
-  mOptions.VfrFileName[0]                = '\0';\r
-  mOptions.RecordListFile[0]             = '\0';\r
   mOptions.CreateRecordListFile          = FALSE;\r
   mOptions.CreateIfrPkgFile              = FALSE;\r
-  mOptions.PkgOutputFileName[0]          = '\0';\r
-  mOptions.COutputFileName[0]            = '\0';\r
-  mOptions.OutputDirectory[0]            = '\0';\r
-  mOptions.PreprocessorOutputFileName[0] = '\0';\r
-  mOptions.VfrBaseFileName[0]            = '\0';\r
+\r
+  if (mOptions.VfrFileName != NULL) {\r
+    free (mOptions.VfrFileName);\r
+    mOptions.VfrFileName                 = NULL;\r
+  }\r
+  if (mOptions.VfrBaseFileName != NULL) {\r
+    free (mOptions.VfrBaseFileName);\r
+    mOptions.VfrBaseFileName             = NULL;\r
+  }\r
+  if (mOptions.OutputDirectory != NULL) {\r
+    free (mOptions.OutputDirectory);\r
+    mOptions.OutputDirectory             = NULL;\r
+  }\r
+  if (mOptions.PkgOutputFileName != NULL) {\r
+    free (mOptions.PkgOutputFileName);\r
+    mOptions.PkgOutputFileName           = NULL;\r
+  }\r
+  if (mOptions.COutputFileName != NULL) {\r
+    free (mOptions.COutputFileName);\r
+    mOptions.COutputFileName             = NULL;\r
+  }\r
+  if (mOptions.PreprocessorOutputFileName != NULL) {\r
+    free (mOptions.PreprocessorOutputFileName);\r
+    mOptions.PreprocessorOutputFileName  = NULL;\r
+  }\r
+  if (mOptions.RecordListFile != NULL) {\r
+    free (mOptions.RecordListFile);\r
+    mOptions.RecordListFile              = NULL;\r
+  }\r
   if (mOptions.IncludePaths != NULL) {\r
     delete mOptions.IncludePaths;\r
     mOptions.IncludePaths                = NULL;\r
@@ -283,7 +333,7 @@ CVfrCompiler::SetBaseFileName (
 {\r
   CHAR8         *pFileName, *pPath, *pExt;\r
 \r
-  if (mOptions.VfrFileName[0] == '\0') {\r
+  if (mOptions.VfrFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -304,8 +354,20 @@ CVfrCompiler::SetBaseFileName (
     return -1;\r
   }\r
 \r
-  strncpy (mOptions.VfrBaseFileName, pFileName, pExt - pFileName);\r
-  mOptions.VfrBaseFileName[pExt - pFileName] = '\0';\r
+  *pExt = '\0';\r
+  if (strlen (pFileName) > MAX_PATH - 1) {\r
+    *pExt = '.';\r
+    return -1;\r
+  }\r
+\r
+  mOptions.VfrBaseFileName = (CHAR8 *) malloc (strlen (pFileName) + 1);\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    *pExt = '.';\r
+    return -1;\r
+  }\r
+\r
+  strcpy (mOptions.VfrBaseFileName, pFileName);\r
+  *pExt = '.';\r
 \r
   return 0;\r
 }\r
@@ -315,7 +377,22 @@ CVfrCompiler::SetPkgOutputFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (VFR_PACKAGE_FILENAME_EXTENSION) +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.PkgOutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.PkgOutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -331,7 +408,22 @@ CVfrCompiler::SetCOutputFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (".c") +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.COutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.COutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -347,7 +439,22 @@ CVfrCompiler::SetPreprocessorOutputFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (VFR_PREPROCESS_FILENAME_EXTENSION) +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.PreprocessorOutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.PreprocessorOutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -363,7 +470,22 @@ CVfrCompiler::SetRecordListFileName (
   VOID\r
   )\r
 {\r
-  if (mOptions.VfrBaseFileName[0] == '\0') {\r
+  INTN Length;\r
+\r
+  if (mOptions.VfrBaseFileName == NULL) {\r
+    return -1;\r
+  }\r
+\r
+  Length = strlen (mOptions.OutputDirectory) +\r
+           strlen (mOptions.VfrBaseFileName) +\r
+           strlen (VFR_RECORDLIST_FILENAME_EXTENSION) +\r
+           1;\r
+  if (Length > MAX_PATH) {\r
+    return -1;\r
+  }\r
+\r
+  mOptions.RecordListFile = (CHAR8 *) malloc (Length);\r
+  if (mOptions.RecordListFile == NULL) {\r
     return -1;\r
   }\r
 \r
@@ -397,6 +519,41 @@ CVfrCompiler::~CVfrCompiler (
   VOID\r
   )\r
 {\r
+  if (mOptions.VfrFileName != NULL) {\r
+    free (mOptions.VfrFileName);\r
+    mOptions.VfrFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.VfrBaseFileName != NULL) {\r
+    free (mOptions.VfrBaseFileName);\r
+    mOptions.VfrBaseFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.OutputDirectory != NULL) {\r
+    free (mOptions.OutputDirectory);\r
+    mOptions.OutputDirectory = NULL;\r
+  }\r
+\r
+  if (mOptions.PkgOutputFileName != NULL) {\r
+    free (mOptions.PkgOutputFileName);\r
+    mOptions.PkgOutputFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.COutputFileName != NULL) {\r
+    free (mOptions.COutputFileName);\r
+    mOptions.COutputFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.PreprocessorOutputFileName != NULL) {\r
+    free (mOptions.PreprocessorOutputFileName);\r
+    mOptions.PreprocessorOutputFileName = NULL;\r
+  }\r
+\r
+  if (mOptions.RecordListFile != NULL) {\r
+    free (mOptions.RecordListFile);\r
+    mOptions.RecordListFile = NULL;\r
+  }\r
+\r
   if (mOptions.IncludePaths != NULL) {\r
     delete mOptions.IncludePaths;\r
     mOptions.IncludePaths = NULL;\r
index 7dd9dd0ecd5a586613c6e6fbf0b6bff4cef33985..b8f39dfa6ca7b500972af60c4f98c063239cb3bc 100644 (file)
@@ -41,15 +41,15 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #define VFR_RECORDLIST_FILENAME_EXTENSION   ".lst"\r
 \r
 typedef struct {\r
-  CHAR8   VfrFileName[MAX_PATH];\r
-  CHAR8   RecordListFile[MAX_PATH];\r
-  CHAR8   PkgOutputFileName[MAX_PATH];\r
-  CHAR8   COutputFileName[MAX_PATH];\r
+  CHAR8   *VfrFileName;\r
+  CHAR8   *RecordListFile;\r
+  CHAR8   *PkgOutputFileName;\r
+  CHAR8   *COutputFileName;\r
   bool    CreateRecordListFile;\r
   bool    CreateIfrPkgFile;\r
-  CHAR8   OutputDirectory[MAX_PATH];\r
-  CHAR8   PreprocessorOutputFileName[MAX_PATH];\r
-  CHAR8   VfrBaseFileName[MAX_PATH];  // name of input VFR file with no path or extension\r
+  CHAR8   *OutputDirectory;\r
+  CHAR8   *PreprocessorOutputFileName;\r
+  CHAR8   *VfrBaseFileName;  // name of input VFR file with no path or extension\r
   CHAR8   *IncludePaths;\r
   bool    SkipCPreprocessor;\r
   CHAR8   *CPreprocessorOptions;\r