]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenBootSector/GenBootSector.c
BaseTools/GenBootSector: Fix file handles not being closed
[mirror_edk2.git] / BaseTools / Source / C / GenBootSector / GenBootSector.c
index 213e1b9722ffc5379d36082ff1ac12b7bfeeef22..3908c589afa529ef95fd1b1b4bec2b5e9d51c8a1 100644 (file)
@@ -201,6 +201,7 @@ Return:
     //\r
     // Only care about the disk.\r
     //\r
+    CloseHandle(VolumeHandle);\r
     return FALSE;\r
   } else{\r
     DriveInfo->DiskNumber = StorageDeviceNumber.DeviceNumber;\r
@@ -437,8 +438,8 @@ ProcessBsOrMbr (
   BYTE              DiskPartitionBackup[0x200] = {0};\r
   DWORD             BytesReturn;\r
   INT               DrvNumOffset;\r
-  HANDLE            InputHandle;\r
-  HANDLE            OutputHandle;\r
+  HANDLE            InputHandle = INVALID_HANDLE_VALUE;\r
+  HANDLE            OutputHandle = INVALID_HANDLE_VALUE;\r
   ERROR_STATUS      Status;\r
   DWORD             InputDbrOffset;\r
   DWORD             OutputDbrOffset;\r
@@ -448,7 +449,7 @@ ProcessBsOrMbr (
   //\r
   Status =  GetFileHandle(InputInfo, ProcessMbr, &InputHandle, &InputDbrOffset);\r
   if (Status != ErrorSuccess) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   //\r
@@ -456,14 +457,15 @@ ProcessBsOrMbr (
   //\r
   Status = GetFileHandle(OutputInfo, ProcessMbr, &OutputHandle, &OutputDbrOffset);\r
   if (Status != ErrorSuccess) {\r
-    return Status;\r
+    goto Done;\r
   }\r
 \r
   //\r
   // Read boot sector from source disk/file\r
   // \r
   if (!ReadFile (InputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) {\r
-    return ErrorFileReadWrite;\r
+    Status = ErrorFileReadWrite;\r
+    goto Done;\r
   }\r
 \r
   if (InputInfo->Type == PathUsb) {\r
@@ -473,7 +475,8 @@ ProcessBsOrMbr (
       //\r
       DrvNumOffset = GetDrvNumOffset (DiskPartition);\r
       if (DrvNumOffset == -1) {\r
-        return ErrorFatType;\r
+        Status = ErrorFatType;\r
+        goto Done;\r
       }\r
       //\r
       // Some legacy BIOS require 0x80 discarding MBR.\r
@@ -495,7 +498,8 @@ ProcessBsOrMbr (
       // Use original partition table\r
       //\r
       if (!ReadFile (OutputHandle, DiskPartitionBackup, 0x200, &BytesReturn, NULL)) {\r
-        return ErrorFileReadWrite;\r
+        Status = ErrorFileReadWrite;\r
+        goto Done;\r
       }\r
       memcpy (DiskPartition + 0x1BE, DiskPartitionBackup + 0x1BE, 0x40);\r
       SetFilePointer (OutputHandle, 0, NULL, FILE_BEGIN);\r
@@ -507,13 +511,19 @@ ProcessBsOrMbr (
   // Write boot sector to taget disk/file\r
   // \r
   if (!WriteFile (OutputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) {\r
-    return ErrorFileReadWrite;\r
+    Status = ErrorFileReadWrite;\r
+    goto Done;\r
   }\r
 \r
-  CloseHandle (InputHandle);\r
-  CloseHandle (OutputHandle);\r
+Done:\r
+  if (InputHandle != INVALID_HANDLE_VALUE) {\r
+    CloseHandle (InputHandle);\r
+  }\r
+  if (OutputHandle != INVALID_HANDLE_VALUE) {\r
+    CloseHandle (OutputHandle);\r
+  }\r
 \r
-  return ErrorSuccess;\r
+  return Status;\r
 }\r
 \r
 void\r
@@ -630,7 +640,8 @@ GetPathInfo (
     if (f == NULL) {\r
       fprintf (stderr, "error E2003: File was not provided!\n");\r
       return ErrorPath;\r
-    }  \r
+    }\r
+    fclose (f);\r
   }\r
   PathInfo->Type = PathFile;\r
   strcpy(PathInfo->PhysicalPath, PathInfo->Path);\r