From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1 with cipher CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id A9D071A1F44 for ; Wed, 12 Oct 2016 05:22:01 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP; 12 Oct 2016 05:22:02 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,482,1473145200"; d="scan'208";a="1063788922" Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.34]) by orsmga002.jf.intel.com with ESMTP; 12 Oct 2016 05:22:00 -0700 From: Hao Wu To: edk2-devel@lists.01.org Cc: Hao Wu , Liming Gao , Yonghong Zhu Date: Wed, 12 Oct 2016 20:20:24 +0800 Message-Id: <1476274836-10544-41-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1476274836-10544-1-git-send-email-hao.a.wu@intel.com> References: <1476274836-10544-1-git-send-email-hao.a.wu@intel.com> Subject: [PATCH 40/52] BaseTools/GenBootSector: Fix file handles not being closed X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 12 Oct 2016 12:22:01 -0000 Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu --- BaseTools/Source/C/GenBootSector/GenBootSector.c | 35 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/BaseTools/Source/C/GenBootSector/GenBootSector.c b/BaseTools/Source/C/GenBootSector/GenBootSector.c index efdfcd2..2ab2d2a 100644 --- a/BaseTools/Source/C/GenBootSector/GenBootSector.c +++ b/BaseTools/Source/C/GenBootSector/GenBootSector.c @@ -201,6 +201,7 @@ Return: // // Only care about the disk. // + CloseHandle(VolumeHandle); return FALSE; } else{ DriveInfo->DiskNumber = StorageDeviceNumber.DeviceNumber; @@ -437,8 +438,8 @@ ProcessBsOrMbr ( BYTE DiskPartitionBackup[0x200] = {0}; DWORD BytesReturn; INT DrvNumOffset; - HANDLE InputHandle; - HANDLE OutputHandle; + HANDLE InputHandle = INVALID_HANDLE_VALUE; + HANDLE OutputHandle = INVALID_HANDLE_VALUE; ERROR_STATUS Status; DWORD InputDbrOffset; DWORD OutputDbrOffset; @@ -448,7 +449,7 @@ ProcessBsOrMbr ( // Status = GetFileHandle(InputInfo, ProcessMbr, &InputHandle, &InputDbrOffset); if (Status != ErrorSuccess) { - return Status; + goto Done; } // @@ -456,14 +457,15 @@ ProcessBsOrMbr ( // Status = GetFileHandle(OutputInfo, ProcessMbr, &OutputHandle, &OutputDbrOffset); if (Status != ErrorSuccess) { - return Status; + goto Done; } // // Read boot sector from source disk/file // if (!ReadFile (InputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) { - return ErrorFileReadWrite; + Status = ErrorFileReadWrite; + goto Done; } if (InputInfo->Type == PathUsb) { @@ -473,7 +475,8 @@ ProcessBsOrMbr ( // DrvNumOffset = GetDrvNumOffset (DiskPartition); if (DrvNumOffset == -1) { - return ErrorFatType; + Status = ErrorFatType; + goto Done; } // // Some legacy BIOS require 0x80 discarding MBR. @@ -495,7 +498,8 @@ ProcessBsOrMbr ( // Use original partition table // if (!ReadFile (OutputHandle, DiskPartitionBackup, 0x200, &BytesReturn, NULL)) { - return ErrorFileReadWrite; + Status = ErrorFileReadWrite; + goto Done; } memcpy (DiskPartition + 0x1BE, DiskPartitionBackup + 0x1BE, 0x40); SetFilePointer (OutputHandle, 0, NULL, FILE_BEGIN); @@ -507,13 +511,19 @@ ProcessBsOrMbr ( // Write boot sector to taget disk/file // if (!WriteFile (OutputHandle, DiskPartition, 0x200, &BytesReturn, NULL)) { - return ErrorFileReadWrite; + Status = ErrorFileReadWrite; + goto Done; } - CloseHandle (InputHandle); - CloseHandle (OutputHandle); +Done: + if (InputHandle != INVALID_HANDLE_VALUE) { + CloseHandle (InputHandle); + } + if (OutputHandle != INVALID_HANDLE_VALUE) { + CloseHandle (OutputHandle); + } - return ErrorSuccess; + return Status; } void @@ -630,7 +640,8 @@ GetPathInfo ( if (f == NULL) { fprintf (stderr, "error E2003: File was not provided!\n"); return ErrorPath; - } + } + fclose (f); } PathInfo->Type = PathFile; strcpy(PathInfo->PhysicalPath, PathInfo->Path); -- 1.9.5.msysgit.0