]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/VfrCompile/VfrCompiler.cpp
BaseTools: resolve initialization order errors in VfrFormPkg.h
[mirror_edk2.git] / BaseTools / Source / C / VfrCompile / VfrCompiler.cpp
index 59f4bf3a8e557733eea84ebdaeb466b2a249d8bd..ff2a837dfc8fa8de1c41026bddea7d65c8fe99dd 100644 (file)
@@ -2,7 +2,7 @@
   \r
   VfrCompiler main class and main function.\r
 \r
   \r
   VfrCompiler main class and main function.\r
 \r
-Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2017, 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
 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
@@ -68,15 +68,15 @@ CVfrCompiler::OptionInitialization (
   Status = EFI_SUCCESS;\r
   SetUtilityName ((CHAR8*) PROGRAM_NAME);\r
 \r
   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.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
   mOptions.IncludePaths                  = NULL;\r
   mOptions.SkipCPreprocessor             = TRUE;\r
   mOptions.CPreprocessorOptions          = NULL;\r
@@ -119,6 +119,12 @@ CVfrCompiler::OptionInitialization (
         DebugError (NULL, 0, 1001, "Missing option", "-o missing output directory name");\r
         goto Fail;\r
       }\r
         DebugError (NULL, 0, 1001, "Missing option", "-o missing output directory name");\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
       strcpy (mOptions.OutputDirectory, Argv[Index]);\r
       \r
       CHAR8 lastChar = mOptions.OutputDirectory[strlen(mOptions.OutputDirectory) - 1];\r
@@ -129,7 +135,7 @@ CVfrCompiler::OptionInitialization (
           strcat (mOptions.OutputDirectory, "\\");\r
         }\r
       }\r
           strcat (mOptions.OutputDirectory, "\\");\r
         }\r
       }\r
-      DebugMsg (NULL, 0, 9, (CHAR8 *) "Output Directory", mOptions.OutputDirectory);\r
+      DebugMsg (NULL, 0, 9, (CHAR8 *) "Output Directory", (CHAR8 *) "%s", mOptions.OutputDirectory);\r
     } else if (stricmp(Argv[Index], "-b") == 0 || stricmp(Argv[Index], "--create-ifr-package") == 0 || stricmp(Argv[Index], "-ibin") == 0) {\r
       mOptions.CreateIfrPkgFile = TRUE;\r
     } else if (stricmp(Argv[Index], "-n") == 0 || stricmp(Argv[Index], "--no-pre-processing") == 0 || stricmp(Argv[Index], "-nopp") == 0) {\r
     } else if (stricmp(Argv[Index], "-b") == 0 || stricmp(Argv[Index], "--create-ifr-package") == 0 || stricmp(Argv[Index], "-ibin") == 0) {\r
       mOptions.CreateIfrPkgFile = TRUE;\r
     } else if (stricmp(Argv[Index], "-n") == 0 || stricmp(Argv[Index], "--no-pre-processing") == 0 || stricmp(Argv[Index], "-nopp") == 0) {\r
@@ -151,7 +157,7 @@ CVfrCompiler::OptionInitialization (
         goto Fail;\r
       }\r
       gCVfrStringDB.SetStringFileName(Argv[Index]);\r
         goto Fail;\r
       }\r
       gCVfrStringDB.SetStringFileName(Argv[Index]);\r
-      DebugMsg (NULL, 0, 9, (CHAR8 *) "Input string file path", Argv[Index]);\r
+      DebugMsg (NULL, 0, 9, (CHAR8 *) "Input string file path", (CHAR8 *) "%s", Argv[Index]);\r
     } else if ((stricmp (Argv[Index], "-g") == 0) || (stricmp (Argv[Index], "--guid") == 0)) {\r
       Index++;\r
       Status = StringToGuid (Argv[Index], &mOptions.OverrideClassGuid);\r
     } else if ((stricmp (Argv[Index], "-g") == 0) || (stricmp (Argv[Index], "--guid") == 0)) {\r
       Index++;\r
       Status = StringToGuid (Argv[Index], &mOptions.OverrideClassGuid);\r
@@ -176,7 +182,21 @@ CVfrCompiler::OptionInitialization (
     DebugError (NULL, 0, 1001, "Missing option", "VFR file name is not specified.");\r
     goto Fail;\r
   } else {\r
     DebugError (NULL, 0, 1001, "Missing option", "VFR file name is not specified.");\r
     goto Fail;\r
   } else {\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
     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
   }\r
 \r
   if (SetBaseFileName() != 0) {\r
@@ -199,15 +219,37 @@ CVfrCompiler::OptionInitialization (
 Fail:\r
   SET_RUN_STATUS (STATUS_DEAD);\r
 \r
 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.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
   if (mOptions.IncludePaths != NULL) {\r
     delete mOptions.IncludePaths;\r
     mOptions.IncludePaths                = NULL;\r
@@ -283,7 +325,7 @@ CVfrCompiler::SetBaseFileName (
 {\r
   CHAR8         *pFileName, *pPath, *pExt;\r
 \r
 {\r
   CHAR8         *pFileName, *pPath, *pExt;\r
 \r
-  if (mOptions.VfrFileName[0] == '\0') {\r
+  if (mOptions.VfrFileName == NULL) {\r
     return -1;\r
   }\r
 \r
     return -1;\r
   }\r
 \r
@@ -304,8 +346,16 @@ CVfrCompiler::SetBaseFileName (
     return -1;\r
   }\r
 \r
     return -1;\r
   }\r
 \r
-  strncpy (mOptions.VfrBaseFileName, pFileName, pExt - pFileName);\r
-  mOptions.VfrBaseFileName[pExt - pFileName] = '\0';\r
+  *pExt = '\0';\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
 \r
   return 0;\r
 }\r
@@ -315,7 +365,19 @@ CVfrCompiler::SetPkgOutputFileName (
   VOID\r
   )\r
 {\r
   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
+\r
+  mOptions.PkgOutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.PkgOutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
     return -1;\r
   }\r
 \r
@@ -331,7 +393,19 @@ CVfrCompiler::SetCOutputFileName (
   VOID\r
   )\r
 {\r
   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
+\r
+  mOptions.COutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.COutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
     return -1;\r
   }\r
 \r
@@ -347,7 +421,19 @@ CVfrCompiler::SetPreprocessorOutputFileName (
   VOID\r
   )\r
 {\r
   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
+\r
+  mOptions.PreprocessorOutputFileName = (CHAR8 *) malloc (Length);\r
+  if (mOptions.PreprocessorOutputFileName == NULL) {\r
     return -1;\r
   }\r
 \r
     return -1;\r
   }\r
 \r
@@ -363,7 +449,19 @@ CVfrCompiler::SetRecordListFileName (
   VOID\r
   )\r
 {\r
   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
+\r
+  mOptions.RecordListFile = (CHAR8 *) malloc (Length);\r
+  if (mOptions.RecordListFile == NULL) {\r
     return -1;\r
   }\r
 \r
     return -1;\r
   }\r
 \r
@@ -397,6 +495,41 @@ CVfrCompiler::~CVfrCompiler (
   VOID\r
   )\r
 {\r
   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
   if (mOptions.IncludePaths != NULL) {\r
     delete mOptions.IncludePaths;\r
     mOptions.IncludePaths = NULL;\r
@@ -485,7 +618,7 @@ CVfrCompiler::PreProcess (
   }\r
 \r
   if ((pVfrFile = fopen (LongFilePath (mOptions.VfrFileName), "r")) == NULL) {\r
   }\r
 \r
   if ((pVfrFile = fopen (LongFilePath (mOptions.VfrFileName), "r")) == NULL) {\r
-    DebugError (NULL, 0, 0001, "Error opening the input VFR file", mOptions.VfrFileName);\r
+    DebugError (NULL, 0, 0001, "Error opening the input VFR file", "%s", mOptions.VfrFileName);\r
     goto Fail;\r
   }\r
   fclose (pVfrFile);\r
     goto Fail;\r
   }\r
   fclose (pVfrFile);\r
@@ -520,7 +653,7 @@ CVfrCompiler::PreProcess (
     goto Fail;\r
   }\r
 \r
     goto Fail;\r
   }\r
 \r
-  delete PreProcessCmd;\r
+  delete[] PreProcessCmd;\r
 \r
 Out:\r
   SET_RUN_STATUS (STATUS_PREPROCESSED);\r
 \r
 Out:\r
   SET_RUN_STATUS (STATUS_PREPROCESSED);\r
@@ -530,7 +663,7 @@ Fail:
   if (!IS_RUN_STATUS(STATUS_DEAD)) {\r
     SET_RUN_STATUS (STATUS_FAILED);\r
   }\r
   if (!IS_RUN_STATUS(STATUS_DEAD)) {\r
     SET_RUN_STATUS (STATUS_FAILED);\r
   }\r
-  delete PreProcessCmd;\r
+  delete[] PreProcessCmd;\r
 }\r
 \r
 extern UINT8 VfrParserStart (IN FILE *, IN INPUT_INFO_TO_SYNTAX *);\r
 }\r
 \r
 extern UINT8 VfrParserStart (IN FILE *, IN INPUT_INFO_TO_SYNTAX *);\r
@@ -554,7 +687,7 @@ CVfrCompiler::Compile (
   gCVfrErrorHandle.SetWarningAsError(mOptions.WarningAsError);\r
 \r
   if ((pInFile = fopen (LongFilePath (InFileName), "r")) == NULL) {\r
   gCVfrErrorHandle.SetWarningAsError(mOptions.WarningAsError);\r
 \r
   if ((pInFile = fopen (LongFilePath (InFileName), "r")) == NULL) {\r
-    DebugError (NULL, 0, 0001, "Error opening the input file", InFileName);\r
+    DebugError (NULL, 0, 0001, "Error opening the input file", "%s", InFileName);\r
     goto Fail;\r
   }\r
 \r
     goto Fail;\r
   }\r
 \r
@@ -570,6 +703,7 @@ CVfrCompiler::Compile (
   }\r
 \r
   fclose (pInFile);\r
   }\r
 \r
   fclose (pInFile);\r
+  pInFile = NULL;\r
 \r
   if (gCFormPkg.HavePendingUnassigned () == TRUE) {\r
     gCFormPkg.PendingAssignPrintAll ();\r
 \r
   if (gCFormPkg.HavePendingUnassigned () == TRUE) {\r
     gCFormPkg.PendingAssignPrintAll ();\r
@@ -684,7 +818,7 @@ CVfrCompiler::GenBinary (
 \r
   if (mOptions.CreateIfrPkgFile == TRUE) {\r
     if ((pFile = fopen (LongFilePath (mOptions.PkgOutputFileName), "wb")) == NULL) {\r
 \r
   if (mOptions.CreateIfrPkgFile == TRUE) {\r
     if ((pFile = fopen (LongFilePath (mOptions.PkgOutputFileName), "wb")) == NULL) {\r
-      DebugError (NULL, 0, 0001, "Error opening file", mOptions.PkgOutputFileName);\r
+      DebugError (NULL, 0, 0001, "Error opening file", "%s", mOptions.PkgOutputFileName);\r
       goto Fail;\r
     }\r
     if (gCFormPkg.BuildPkg (pFile, &gRBuffer) != VFR_RETURN_SUCCESS) {\r
       goto Fail;\r
     }\r
     if (gCFormPkg.BuildPkg (pFile, &gRBuffer) != VFR_RETURN_SUCCESS) {\r
@@ -727,7 +861,7 @@ CVfrCompiler::GenCFile (
   \r
   if (!mOptions.CreateIfrPkgFile || mOptions.CompatibleMode) {\r
     if ((pFile = fopen (LongFilePath (mOptions.COutputFileName), "w")) == NULL) {\r
   \r
   if (!mOptions.CreateIfrPkgFile || mOptions.CompatibleMode) {\r
     if ((pFile = fopen (LongFilePath (mOptions.COutputFileName), "w")) == NULL) {\r
-      DebugError (NULL, 0, 0001, "Error opening output C file", mOptions.COutputFileName);\r
+      DebugError (NULL, 0, 0001, "Error opening output C file", "%s", mOptions.COutputFileName);\r
       goto Fail;\r
     }\r
 \r
       goto Fail;\r
     }\r
 \r
@@ -768,18 +902,18 @@ CVfrCompiler::GenRecordListFile (
 \r
   InFileName = (mOptions.SkipCPreprocessor == TRUE) ? mOptions.VfrFileName : mOptions.PreprocessorOutputFileName;\r
 \r
 \r
   InFileName = (mOptions.SkipCPreprocessor == TRUE) ? mOptions.VfrFileName : mOptions.PreprocessorOutputFileName;\r
 \r
-  if (mOptions.CreateRecordListFile == TRUE) {\r
+  if (mOptions.CreateRecordListFile == TRUE && InFileName != NULL && mOptions.RecordListFile != NULL) {\r
     if ((InFileName[0] == '\0') || (mOptions.RecordListFile[0] == '\0')) {\r
       return;\r
     }\r
 \r
     if ((pInFile = fopen (LongFilePath (InFileName), "r")) == NULL) {\r
     if ((InFileName[0] == '\0') || (mOptions.RecordListFile[0] == '\0')) {\r
       return;\r
     }\r
 \r
     if ((pInFile = fopen (LongFilePath (InFileName), "r")) == NULL) {\r
-      DebugError (NULL, 0, 0001, "Error opening the input VFR preprocessor output file", InFileName);\r
+      DebugError (NULL, 0, 0001, "Error opening the input VFR preprocessor output file", "%s", InFileName);\r
       return;\r
     }\r
 \r
     if ((pOutFile = fopen (LongFilePath (mOptions.RecordListFile), "w")) == NULL) {\r
       return;\r
     }\r
 \r
     if ((pOutFile = fopen (LongFilePath (mOptions.RecordListFile), "w")) == NULL) {\r
-      DebugError (NULL, 0, 0001, "Error opening the record list file", mOptions.RecordListFile);\r
+      DebugError (NULL, 0, 0001, "Error opening the record list file", "%s", mOptions.RecordListFile);\r
       goto Err1;\r
     }\r
 \r
       goto Err1;\r
     }\r
 \r