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 6878E1A1F31 for ; Wed, 12 Oct 2016 05:21:54 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP; 12 Oct 2016 05:21:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,482,1473145200"; d="scan'208";a="1063788866" Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.34]) by orsmga002.jf.intel.com with ESMTP; 12 Oct 2016 05:21:53 -0700 From: Hao Wu To: edk2-devel@lists.01.org Cc: Hao Wu , Liming Gao , Yonghong Zhu Date: Wed, 12 Oct 2016 20:20:19 +0800 Message-Id: <1476274836-10544-36-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 35/52] BaseTools/Split: Fix potential memory and resource leak 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:21:54 -0000 Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu --- BaseTools/Source/C/Split/Split.c | 41 +++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/BaseTools/Source/C/Split/Split.c b/BaseTools/Source/C/Split/Split.c index c6f547c..eb83d4c 100644 --- a/BaseTools/Source/C/Split/Split.c +++ b/BaseTools/Source/C/Split/Split.c @@ -223,14 +223,15 @@ Returns: --*/ { EFI_STATUS Status = EFI_SUCCESS; + INTN ReturnStatus = STATUS_SUCCESS; FILE *In; CHAR8 *InputFileName = NULL; CHAR8 *OutputDir = NULL; CHAR8 *OutFileName1 = NULL; CHAR8 *OutFileName2 = NULL; UINT64 SplitValue = (UINT64) -1; - FILE *Out1; - FILE *Out2; + FILE *Out1 = NULL; + FILE *Out2 = NULL; CHAR8 *OutName1 = NULL; CHAR8 *OutName2 = NULL; CHAR8 *CurrentDir = NULL; @@ -366,7 +367,8 @@ Returns: OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16); if (OutName1 == NULL) { Warning (NULL, 0, 0, NULL, "Memory Allocation Fail."); - return STATUS_ERROR; + ReturnStatus = STATUS_ERROR; + goto Finish; } strcpy (OutName1, InputFileName); strcat (OutName1, "1"); @@ -377,7 +379,8 @@ Returns: OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16); if (OutName2 == NULL) { Warning (NULL, 0, 0, NULL, "Memory Allocation Fail."); - return STATUS_ERROR; + ReturnStatus = STATUS_ERROR; + goto Finish; } strcpy (OutName2, InputFileName); strcat (OutName2, "2"); @@ -389,20 +392,23 @@ Returns: //OutputDirSpecified = TRUE; if (chdir(OutputDir) != 0) { Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail."); - return STATUS_ERROR; + ReturnStatus = STATUS_ERROR; + goto Finish; } } CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0); if (EFI_ERROR(CreateDir(&OutFileName1))) { Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL); - return STATUS_ERROR; + ReturnStatus = STATUS_ERROR; + goto Finish; } chdir(CurrentDir); if (EFI_ERROR(CreateDir(&OutFileName2))) { Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL); - return STATUS_ERROR; + ReturnStatus = STATUS_ERROR; + goto Finish; } chdir(CurrentDir); free(CurrentDir); @@ -411,14 +417,16 @@ Returns: if (Out1 == NULL) { // ("Unable to open file \"%s\"\n", OutFileName1); Error (OutFileName1, 0, 1, "File open failure", NULL); - return STATUS_ERROR; + ReturnStatus = STATUS_ERROR; + goto Finish; } Out2 = fopen (LongFilePath (OutFileName2), "wb"); if (Out2 == NULL) { // ("Unable to open file \"%s\"\n", OutFileName2); Error (OutFileName2, 0, 1, "File open failure", NULL); - return STATUS_ERROR; + ReturnStatus = STATUS_ERROR; + goto Finish; } for (Index = 0; Index < SplitValue; Index++) { @@ -439,15 +447,22 @@ Returns: fputc (CharC, Out2); } +Finish: if (OutName1 != NULL) { free(OutName1); } if (OutName2 != NULL) { free(OutName2); } - fclose (In); - fclose (Out1); - fclose (Out2); + if (In != NULL) { + fclose (In); + } + if (Out1 != NULL) { + fclose (Out1); + } + if (Out2 != NULL) { + fclose (Out2); + } - return STATUS_SUCCESS; + return ReturnStatus; } -- 1.9.5.msysgit.0