]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/VolInfo/VolInfo.c
BaseTools/VolInfo: Provide string width in '%s' specifier in format string
[mirror_edk2.git] / BaseTools / Source / C / VolInfo / VolInfo.c
index 5c4a5036c225c1f306a466a580af06c0b85d3e72..5285acdb97f9ebe4cbfd1ed06fc13a5da983225e 100644 (file)
@@ -258,6 +258,14 @@ Returns:
       continue;\r
     }\r
     if ((stricmp (argv[0], "--hash") == 0)) {\r
+      if (EnableHash == TRUE) {\r
+        //\r
+        // --hash already given in the option, ignore this one\r
+        //\r
+        argc --;\r
+        argv ++;\r
+        continue;\r
+      }\r
       EnableHash = TRUE;\r
       OpenSslCommand = "openssl";\r
       OpenSslEnv = getenv("OPENSSL_PATH");\r
@@ -265,6 +273,10 @@ Returns:
         OpenSslPath = OpenSslCommand;\r
       } else {\r
         OpenSslPath = malloc(strlen(OpenSslEnv)+strlen(OpenSslCommand)+1);\r
+        if (OpenSslPath == NULL) {\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+          return GetUtilityStatus ();\r
+        }\r
         CombinePath(OpenSslEnv, OpenSslCommand, OpenSslPath);\r
       }\r
       if (OpenSslPath == NULL){\r
@@ -1382,13 +1394,6 @@ Returns:
     }\r
   }\r
 \r
-  //\r
-  // No available section header is found.\r
-  //\r
-  if (Index == ImgHdr->Pe32.FileHeader.NumberOfSections) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
   //\r
   // BaseAddress is set to section header.\r
   //\r
@@ -1630,9 +1635,11 @@ Returns:
     SectionHeaderLen = GetSectionHeaderLength((EFI_COMMON_SECTION_HEADER *)Ptr);\r
 \r
     SectionName = SectionNameToStr (Type);\r
-    printf ("------------------------------------------------------------\n");\r
-    printf ("  Type:  %s\n  Size:  0x%08X\n", SectionName, (unsigned) SectionLength);\r
-    free (SectionName);\r
+    if (SectionName != NULL) {\r
+      printf ("------------------------------------------------------------\n");\r
+      printf ("  Type:  %s\n  Size:  0x%08X\n", SectionName, (unsigned) SectionLength);\r
+      free (SectionName);\r
+    }\r
 \r
     switch (Type) {\r
     case EFI_SECTION_RAW:\r
@@ -1660,6 +1667,10 @@ Returns:
           strlen (ToolOutputFileName) +\r
           1\r
           );\r
+        if (SystemCommand == NULL) {\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+          return EFI_OUT_OF_RESOURCES;\r
+        }\r
         sprintf (\r
           SystemCommand,\r
           SystemCommandFormatString,\r
@@ -1685,12 +1696,18 @@ Returns:
             nFileLen = ftell(fp);\r
             fseek(fp,0,SEEK_SET);\r
             StrLine = malloc(nFileLen);\r
+            if (StrLine == NULL) {\r
+              fclose(fp);\r
+              free (SystemCommand);\r
+              Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+              return EFI_OUT_OF_RESOURCES;\r
+            }\r
             fgets(StrLine, nFileLen, fp);\r
             NewStr = strrchr (StrLine, '=');\r
             printf ("  SHA1: %s\n", NewStr + 1);\r
             free (StrLine);\r
+            fclose(fp);\r
           }\r
-          fclose(fp);\r
         }\r
         remove(ToolInputFileName);\r
         remove(ToolOutputFileName);\r
@@ -1699,7 +1716,7 @@ Returns:
       break;\r
 \r
     case EFI_SECTION_USER_INTERFACE:\r
-      printf ("  String: %ls\n", (CHAR16 *) &((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString);\r
+      printf ("  String: %ls\n", (wchar_t *) &((EFI_USER_INTERFACE_SECTION *) Ptr)->FileNameString);\r
       break;\r
 \r
     case EFI_SECTION_FIRMWARE_VOLUME_IMAGE:\r
@@ -1775,8 +1792,14 @@ Returns:
         }\r
 \r
         ScratchBuffer       = malloc (ScratchSize);\r
+        if (ScratchBuffer == NULL) {\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+          return EFI_OUT_OF_RESOURCES;\r
+        }\r
         UncompressedBuffer  = malloc (UncompressedLength);\r
-        if ((ScratchBuffer == NULL) || (UncompressedBuffer == NULL)) {\r
+        if (UncompressedBuffer == NULL) {\r
+          free (ScratchBuffer);\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
           return EFI_OUT_OF_RESOURCES;\r
         }\r
         Status = DecompressFunction (\r
@@ -1852,6 +1875,19 @@ Returns:
         close(fd2);\r
        #endif\r
 \r
+        if ((ToolInputFile == NULL) || (ToolOutputFile == NULL)) {\r
+          if (ToolInputFile != NULL) {\r
+            free (ToolInputFile);\r
+          }\r
+          if (ToolOutputFile != NULL) {\r
+            free (ToolOutputFile);\r
+          }\r
+          free (ExtractionTool);\r
+\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+          return EFI_OUT_OF_RESOURCES;\r
+        }\r
+\r
         //\r
         // Construction 'system' command string\r
         //\r
@@ -1863,6 +1899,14 @@ Returns:
           strlen (ToolOutputFile) +\r
           1\r
           );\r
+        if (SystemCommand == NULL) {\r
+          free (ToolInputFile);\r
+          free (ToolOutputFile);\r
+          free (ExtractionTool);\r
+\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+          return EFI_OUT_OF_RESOURCES;\r
+        }\r
         sprintf (\r
           SystemCommand,\r
           SystemCommandFormatString,\r
@@ -1891,6 +1935,7 @@ Returns:
             );\r
         remove (ToolOutputFile);\r
         free (ToolOutputFile);\r
+        free (SystemCommand);\r
         if (EFI_ERROR (Status)) {\r
           Error (NULL, 0, 0004, "unable to read decoded GUIDED section", NULL);\r
           return EFI_SECTION_ERROR;\r
@@ -2133,6 +2178,8 @@ Returns:
 {\r
   FILE              *Fptr;\r
   CHAR8             Line[MAX_LINE_LEN];\r
+  CHAR8             *FormatString;\r
+  INTN              FormatLength;\r
   GUID_TO_BASENAME  *GPtr;\r
 \r
   if ((Fptr = fopen (LongFilePath (FileName), "r")) == NULL) {\r
@@ -2140,17 +2187,44 @@ Returns:
     return EFI_DEVICE_ERROR;\r
   }\r
 \r
+  //\r
+  // Generate the format string for fscanf\r
+  //\r
+  FormatLength = snprintf (\r
+                   NULL,\r
+                   0,\r
+                   "%%%us %%%us",\r
+                   (unsigned) sizeof (GPtr->Guid) - 1,\r
+                   (unsigned) sizeof (GPtr->BaseName) - 1\r
+                   ) + 1;\r
+\r
+  FormatString = (CHAR8 *) malloc (FormatLength);\r
+  if (FormatString == NULL) {\r
+    fclose (Fptr);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  snprintf (\r
+    FormatString,\r
+    FormatLength,\r
+    "%%%us %%%us",\r
+    (unsigned) sizeof (GPtr->Guid) - 1,\r
+    (unsigned) sizeof (GPtr->BaseName) - 1\r
+    );\r
+\r
   while (fgets (Line, sizeof (Line), Fptr) != NULL) {\r
     //\r
     // Allocate space for another guid/basename element\r
     //\r
     GPtr = malloc (sizeof (GUID_TO_BASENAME));\r
     if (GPtr == NULL) {\r
+      free (FormatString);\r
+      fclose (Fptr);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
 \r
     memset ((char *) GPtr, 0, sizeof (GUID_TO_BASENAME));\r
-    if (sscanf (Line, "%s %s", GPtr->Guid, GPtr->BaseName) == 2) {\r
+    if (sscanf (Line, FormatString, GPtr->Guid, GPtr->BaseName) == 2) {\r
       GPtr->Next        = mGuidBaseNameList;\r
       mGuidBaseNameList = GPtr;\r
     } else {\r
@@ -2161,6 +2235,7 @@ Returns:
     }\r
   }\r
 \r
+  free (FormatString);\r
   fclose (Fptr);\r
   return EFI_SUCCESS;\r
 }\r