From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=chen.a.chen@intel.com; receiver=edk2-devel@lists.01.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 5A6B8208D6135 for ; Sun, 10 Feb 2019 22:17:00 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Feb 2019 22:16:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,358,1544515200"; d="scan'208";a="317946956" Received: from chenche4.ccr.corp.intel.com ([10.239.9.12]) by fmsmga006.fm.intel.com with ESMTP; 10 Feb 2019 22:16:58 -0800 From: Chen A Chen To: edk2-devel@lists.01.org Cc: Chen A Chen , Jian J Wang , Hao Wu , Zhang Chao B , Liming Gao Date: Mon, 11 Feb 2019 14:16:54 +0800 Message-Id: <20190211061654.53812-1-chen.a.chen@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 Subject: [PATCH] MdeModulePkg/CapsuleApp: Fix memory leak issue. X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 11 Feb 2019 06:17:00 -0000 This issue is caused by FileInfoBuffer variable. This is a pointer array and each elements also pointer to a memory buffer that is allocated and returned by AllocateCopyPool function. Cc: Jian J Wang Cc: Hao Wu Cc: Zhang Chao B Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Chen A Chen --- MdeModulePkg/Application/CapsuleApp/CapsuleDump.c | 81 ++++++++++++++++------- 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c index 7bef5a1378..00cf45d66a 100644 --- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c @@ -806,48 +806,69 @@ DumpCapsuleFromDisk ( Status = Fs->OpenVolume (Fs, &Root); if (EFI_ERROR (Status)) { Print (L"Cannot open volume. Status = %r\n", Status); - return EFI_NOT_FOUND; + goto Done; } Status = Root->Open (Root, &DirHandle, EFI_CAPSULE_FILE_DIRECTORY, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE , 0); if (EFI_ERROR (Status)) { Print (L"Cannot open %s. Status = %r\n", EFI_CAPSULE_FILE_DIRECTORY, Status); - return EFI_NOT_FOUND; + goto Done; } // // Get file count first // - for ( Status = FileHandleFindFirstFile (DirHandle, &FileInfo) - ; !EFI_ERROR(Status) && !NoFile - ; Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile) - ){ - if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) == 0) { - continue; + do { + Status = FileHandleFindFirstFile (DirHandle, &FileInfo); + if (EFI_ERROR (Status) || FileInfo == NULL) { + Print (L"Get File Info Fail. Status = %r\n", Status); + goto Done; } - FileCount++; - } + + if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) != 0) { + FileCount++; + } + + Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile); + if (EFI_ERROR (Status)) { + Print (L"Get Next File Fail. Status = %r\n", Status); + goto Done; + } + } while (!NoFile); if (FileCount == 0) { Print (L"Error: No capsule file found!\n"); - return EFI_NOT_FOUND; + Status = EFI_NOT_FOUND; + goto Done; } FileInfoBuffer = AllocatePool (sizeof(FileInfo) * FileCount); + if (FileInfoBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto Done; + } NoFile = FALSE; // // Get all file info // - for ( Status = FileHandleFindFirstFile (DirHandle, &FileInfo) - ; !EFI_ERROR (Status) && !NoFile - ; Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile) - ){ - if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) == 0) { - continue; + do { + Status = FileHandleFindFirstFile (DirHandle, &FileInfo); + if (EFI_ERROR (Status) || FileInfo == NULL) { + Print (L"Get File Info Fail. Status = %r\n", Status); + goto Done; } - FileInfoBuffer[Index++] = AllocateCopyPool ((UINTN)FileInfo->Size, FileInfo); - } + + if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) != 0) { + FileInfoBuffer[Index++] = AllocateCopyPool ((UINTN)FileInfo->Size, FileInfo); + } + + Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile); + if (EFI_ERROR (Status)) { + Print (L"Get Next File Fail. Status = %r\n", Status); + goto Done; + } + } while (!NoFile); // // Sort FileInfoBuffer by alphabet order @@ -866,7 +887,8 @@ DumpCapsuleFromDisk ( } if (!DumpCapsuleInfo) { - return EFI_SUCCESS; + Status = EFI_SUCCESS; + goto Done; } Print(L"The infomation of the capsules:\n"); @@ -875,19 +897,20 @@ DumpCapsuleFromDisk ( FileHandle = NULL; Status = DirHandle->Open (DirHandle, &FileHandle, FileInfoBuffer[Index]->FileName, EFI_FILE_MODE_READ, 0); if (EFI_ERROR (Status)) { - break; + goto Done; } Status = FileHandleGetSize (FileHandle, (UINT64 *) &FileSize); if (EFI_ERROR (Status)) { Print (L"Cannot read file %s. Status = %r\n", FileInfoBuffer[Index]->FileName, Status); FileHandleClose (FileHandle); - return Status; + goto Done; } FileBuffer = AllocatePool (FileSize); if (FileBuffer == NULL) { - return RETURN_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto Done; } Status = FileHandleRead (FileHandle, &FileSize, FileBuffer); @@ -895,7 +918,7 @@ DumpCapsuleFromDisk ( Print (L"Cannot read file %s. Status = %r\n", FileInfoBuffer[Index]->FileName, Status); FreePool (FileBuffer); FileHandleClose (FileHandle); - return Status; + goto Done; } Print (L"**************************\n"); @@ -906,7 +929,15 @@ DumpCapsuleFromDisk ( FreePool (FileBuffer); } - return EFI_SUCCESS; +Done: + if (FileInfoBuffer != NULL) { + for (Index = 0; Index < FileCount; Index++) { + FreePool (FileInfoBuffer[Index]); + } + FreePool (FileInfoBuffer); + } + + return Status; } /** -- 2.16.2.windows.1