]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/Split/Split.c
BaseTools/Split: Fix potential memory and resource leak
[mirror_edk2.git] / BaseTools / Source / C / Split / Split.c
index 9b81eeb76291807f1ecbc0299a8e68f804db6b91..7ab66be9e873a2c3cadfea197c936b8a4e914585 100644 (file)
@@ -2,7 +2,7 @@
 \r
   Split a file into two pieces at the request offset.\r
 \r
-Copyright (c) 1999 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1999 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available\r
 under the terms and conditions of the BSD License which accompanies this\r
 distribution.  The full text of the license may be found at\r
@@ -26,7 +26,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "ParseInf.h"\r
 #include "CommonLib.h"\r
 #include "EfiUtilityMsgs.h"\r
-\r
 //\r
 // Utility Name\r
 //\r
@@ -35,8 +34,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 //\r
 // Utility version information\r
 //\r
-#define UTILITY_MAJOR_VERSION 0\r
-#define UTILITY_MINOR_VERSION 1\r
+#define UTILITY_MAJOR_VERSION 1\r
+#define UTILITY_MINOR_VERSION 0\r
 \r
 void\r
 Version (\r
@@ -58,8 +57,7 @@ Returns:
 \r
 --*/\r
 {\r
-  printf ("%s v%d.%d %s -Utility to break a file into two pieces at the request offset.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
-  printf ("Copyright (c) 1999-2010 Intel Corporation. All rights reserved.\n");\r
+  printf ("%s Version %d.%d Build %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
 }\r
 \r
 void\r
@@ -82,6 +80,9 @@ Returns:
 --*/\r
 {\r
   Version();\r
+  printf ("Copyright (c) 1999-2016 Intel Corporation. All rights reserved.\n");\r
+  printf ("\n  SplitFile creates two Binary files either in the same directory as the current working\n");\r
+  printf ("  directory or in the specified directory.\n");\r
   printf ("\nUsage: \n\\r
    Split\n\\r
      -f, --filename inputFile to split\n\\r
@@ -175,20 +176,25 @@ CreateDir (
 {\r
   CHAR8* temp = *FullFileName;\r
   CHAR8* start = temp;\r
+  CHAR8  tempchar;\r
   UINT64 index = 0;\r
 \r
   for (;index < strlen(temp); ++index) {\r
     if (temp[index] == '\\' || temp[index] == '/') {\r
-      temp[index] = 0;\r
-      if (chdir(start)) {\r
-        if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {\r
-          return EFI_ABORTED;\r
+      if (temp[index + 1] != '\0') {\r
+        tempchar = temp[index + 1];\r
+        temp[index + 1] = 0;\r
+        if (chdir(start)) {\r
+          if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {\r
+            return EFI_ABORTED;\r
+          }\r
+          chdir(start);\r
         }\r
-        chdir(start);\r
+        start = temp + index + 1;\r
+        temp[index] = '/';\r
+        temp[index + 1] = tempchar;\r
+      }\r
     }\r
-      start = temp + index + 1;\r
-      temp[index] = '/';\r
-  }\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -217,20 +223,20 @@ Returns:
 --*/\r
 {\r
   EFI_STATUS    Status = EFI_SUCCESS;\r
+  INTN          ReturnStatus = STATUS_SUCCESS;\r
   FILE          *In;\r
   CHAR8         *InputFileName = NULL;\r
   CHAR8         *OutputDir = NULL;\r
   CHAR8         *OutFileName1 = NULL;\r
   CHAR8         *OutFileName2 = NULL;\r
   UINT64        SplitValue = (UINT64) -1;\r
-  FILE          *Out1;\r
-  FILE          *Out2;\r
+  FILE          *Out1 = NULL;\r
+  FILE          *Out2 = NULL;\r
   CHAR8         *OutName1 = NULL;\r
   CHAR8         *OutName2 = NULL;\r
   CHAR8         *CurrentDir = NULL;\r
   UINT64        Index;\r
   CHAR8         CharC;\r
-  BOOLEAN       QuietFlag = TRUE;\r
   UINT64        DebugLevel = 0;\r
   UINT64        VerboseLevel = 0;\r
 \r
@@ -308,7 +314,6 @@ Returns:
     }\r
 \r
     if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {\r
-      QuietFlag = TRUE;\r
       argc --;\r
       argv ++;\r
       continue;\r
@@ -319,7 +324,7 @@ Returns:
       if (strlen(argv[0]) > 2) {\r
         Status = CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &VerboseLevel);\r
         if (EFI_ERROR (Status)) {\r
-          Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);\r
+          Error (NULL, 0, 0x1003, NULL, "%s is invaild parameter!", argv[0]);\r
           return STATUS_ERROR;\r
         }\r
       }\r
@@ -340,9 +345,9 @@ Returns:
       continue;\r
     }\r
     //\r
-    // Don't recognize the paramter.\r
+    // Don't recognize the parameter.\r
     //\r
-    Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);\r
+    Error (NULL, 0, 0x1003, NULL, "%s is invaild parameter!", argv[0]);\r
     return STATUS_ERROR;\r
   }\r
 \r
@@ -351,7 +356,7 @@ Returns:
     return STATUS_ERROR;\r
   }\r
 \r
-  In = fopen (InputFileName, "rb");\r
+  In = fopen (LongFilePath (InputFileName), "rb");\r
   if (In == NULL) {\r
     // ("Unable to open file \"%s\"\n", InputFileName);\r
     Error (InputFileName, 0, 1, "File open failure", NULL);\r
@@ -362,7 +367,8 @@ Returns:
     OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16);\r
     if (OutName1 == NULL) {\r
       Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");\r
-      return STATUS_ERROR;\r
+      ReturnStatus = STATUS_ERROR;\r
+      goto Finish;\r
     }\r
     strcpy (OutName1, InputFileName);\r
     strcat (OutName1, "1");\r
@@ -373,7 +379,8 @@ Returns:
     OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16);\r
     if (OutName2 == NULL) {\r
       Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");\r
-      return STATUS_ERROR;\r
+      ReturnStatus = STATUS_ERROR;\r
+      goto Finish;\r
     }\r
     strcpy (OutName2, InputFileName);\r
     strcat (OutName2, "2");\r
@@ -385,36 +392,41 @@ Returns:
     //OutputDirSpecified = TRUE;\r
     if (chdir(OutputDir) != 0) {\r
       Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");\r
-      return STATUS_ERROR;\r
+      ReturnStatus = STATUS_ERROR;\r
+      goto Finish;\r
     }\r
   }\r
 \r
   CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0);\r
   if (EFI_ERROR(CreateDir(&OutFileName1))) {\r
       Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);\r
-      return STATUS_ERROR;\r
+      ReturnStatus = STATUS_ERROR;\r
+      goto Finish;\r
   }\r
   chdir(CurrentDir);\r
 \r
   if (EFI_ERROR(CreateDir(&OutFileName2))) {\r
       Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);\r
-      return STATUS_ERROR;\r
+      ReturnStatus = STATUS_ERROR;\r
+      goto Finish;\r
   }\r
   chdir(CurrentDir);\r
   free(CurrentDir);\r
 \r
-  Out1 = fopen (OutFileName1, "wb");\r
+  Out1 = fopen (LongFilePath (OutFileName1), "wb");\r
   if (Out1 == NULL) {\r
     // ("Unable to open file \"%s\"\n", OutFileName1);\r
     Error (OutFileName1, 0, 1, "File open failure", NULL);\r
-    return STATUS_ERROR;\r
+    ReturnStatus = STATUS_ERROR;\r
+    goto Finish;\r
   }\r
 \r
-  Out2 = fopen (OutFileName2, "wb");\r
+  Out2 = fopen (LongFilePath (OutFileName2), "wb");\r
   if (Out2 == NULL) {\r
     // ("Unable to open file \"%s\"\n", OutFileName2);\r
     Error (OutFileName2, 0, 1, "File open failure", NULL);\r
-    return STATUS_ERROR;\r
+    ReturnStatus = STATUS_ERROR;\r
+    goto Finish;\r
   }\r
 \r
   for (Index = 0; Index < SplitValue; Index++) {\r
@@ -435,15 +447,22 @@ Returns:
     fputc (CharC, Out2);\r
   }\r
 \r
+Finish:\r
   if (OutName1 != NULL) {\r
     free(OutName1);\r
   }\r
   if (OutName2 != NULL) {\r
     free(OutName2);\r
   }\r
-  fclose (In);\r
-  fclose (Out1);\r
-  fclose (Out2);\r
+  if (In != NULL) {\r
+    fclose (In);\r
+  }\r
+  if (Out1 != NULL) {\r
+    fclose (Out1);\r
+  }\r
+  if (Out2 != NULL) {\r
+    fclose (Out2);\r
+  }\r
 \r
-  return STATUS_SUCCESS;\r
+  return ReturnStatus;\r
 }\r