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.120; helo=mga04.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) (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 5BE1F202E53B2 for ; Tue, 12 Feb 2019 19:42:41 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Feb 2019 19:42:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,364,1544515200"; d="scan'208";a="133151740" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by FMSMGA003.fm.intel.com with ESMTP; 12 Feb 2019 19:42:41 -0800 Received: from fmsmsx116.amr.corp.intel.com (10.18.116.20) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 12 Feb 2019 19:42:41 -0800 Received: from shsmsx107.ccr.corp.intel.com (10.239.4.96) by fmsmsx116.amr.corp.intel.com (10.18.116.20) with Microsoft SMTP Server (TLS) id 14.3.408.0; Tue, 12 Feb 2019 19:42:40 -0800 Received: from shsmsx104.ccr.corp.intel.com ([169.254.5.102]) by SHSMSX107.ccr.corp.intel.com ([169.254.9.162]) with mapi id 14.03.0415.000; Wed, 13 Feb 2019 11:42:39 +0800 From: "Wu, Hao A" To: "Chen, Chen A" , "edk2-devel@lists.01.org" CC: "Wang, Jian J" , "Zhang, Chao B" , "Gao, Liming" Thread-Topic: [PATCH v3] MdeModulePkg/CapsuleApp: Fix memory leak issue. Thread-Index: AQHUwzaFGQYIXMgfR0KfEKB2nJ+AR6XdFclg Date: Wed, 13 Feb 2019 03:42:38 +0000 Message-ID: References: <20190213005309.417204-1-chen.a.chen@intel.com> In-Reply-To: <20190213005309.417204-1-chen.a.chen@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v3] 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: Wed, 13 Feb 2019 03:42:42 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable > -----Original Message----- > From: Chen, Chen A > Sent: Wednesday, February 13, 2019 8:53 AM > To: edk2-devel@lists.01.org > Cc: Chen, Chen A; Wang, Jian J; Wu, Hao A; Zhang, Chao B; Gao, Liming > Subject: [PATCH v3] MdeModulePkg/CapsuleApp: Fix memory leak issue. >=20 > 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. >=20 > 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 | 87 > ++++++++++++++++------- > 1 file changed, 60 insertions(+), 27 deletions(-) >=20 > diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c > b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c > index ba2583accb..9fd4af4e55 100644 > --- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c > +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c > @@ -806,48 +806,69 @@ DumpCapsuleFromDisk ( > Status =3D Fs->OpenVolume (Fs, &Root); > if (EFI_ERROR (Status)) { > Print (L"Cannot open volume. Status =3D %r\n", Status); > - return EFI_NOT_FOUND; > + goto Done; > } >=20 > Status =3D 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 =3D %r\n", EFI_CAPSULE_FILE_DIRECTOR= Y, > Status); > - return EFI_NOT_FOUND; > + goto Done; > } >=20 > // > // Get file count first > // > - for ( Status =3D FileHandleFindFirstFile (DirHandle, &FileInfo) > - ; !EFI_ERROR(Status) && !NoFile > - ; Status =3D FileHandleFindNextFile (DirHandle, FileInfo, &NoFile) > - ){ > - if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) =3D= =3D 0) { > - continue; > + do { > + Status =3D FileHandleFindFirstFile (DirHandle, &FileInfo); > + if (EFI_ERROR (Status) || FileInfo =3D=3D NULL) { > + Print (L"Get File Info Fail. Status =3D %r\n", Status); > + goto Done; > } > - FileCount++; > - } > + > + if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) != =3D 0) { > + FileCount++; > + } > + > + Status =3D FileHandleFindNextFile (DirHandle, FileInfo, &NoFile); > + if (EFI_ERROR (Status)) { > + Print (L"Get Next File Fail. Status =3D %r\n", Status); > + goto Done; > + } > + } while (!NoFile); >=20 > if (FileCount =3D=3D 0) { > Print (L"Error: No capsule file found!\n"); > - return EFI_NOT_FOUND; > + Status =3D EFI_NOT_FOUND; > + goto Done; > } >=20 > - FileInfoBuffer =3D AllocatePool (sizeof(FileInfo) * FileCount); > + FileInfoBuffer =3D AllocateZeroPool (sizeof (FileInfo) * FileCount); Reviewed-by: Hao Wu Best Regards, Hao Wu > + if (FileInfoBuffer =3D=3D NULL) { > + Status =3D EFI_OUT_OF_RESOURCES; > + goto Done; > + } > NoFile =3D FALSE; >=20 > // > // Get all file info > // > - for ( Status =3D FileHandleFindFirstFile (DirHandle, &FileInfo) > - ; !EFI_ERROR (Status) && !NoFile > - ; Status =3D FileHandleFindNextFile (DirHandle, FileInfo, &NoFile) > - ){ > - if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) =3D= =3D 0) { > - continue; > + do { > + Status =3D FileHandleFindFirstFile (DirHandle, &FileInfo); > + if (EFI_ERROR (Status) || FileInfo =3D=3D NULL) { > + Print (L"Get File Info Fail. Status =3D %r\n", Status); > + goto Done; > + } > + > + if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) != =3D 0) { > + FileInfoBuffer[Index++] =3D AllocateCopyPool ((UINTN)FileInfo->Siz= e, > FileInfo); > } > - FileInfoBuffer[Index++] =3D AllocateCopyPool ((UINTN)FileInfo->Size, > FileInfo); > - } > + > + Status =3D FileHandleFindNextFile (DirHandle, FileInfo, &NoFile); > + if (EFI_ERROR (Status)) { > + Print (L"Get Next File Fail. Status =3D %r\n", Status); > + goto Done; > + } > + } while (!NoFile); >=20 > // > // Sort FileInfoBuffer by alphabet order > @@ -866,7 +887,8 @@ DumpCapsuleFromDisk ( > } >=20 > if (!DumpCapsuleInfo) { > - return EFI_SUCCESS; > + Status =3D EFI_SUCCESS; > + goto Done; > } >=20 > Print(L"The infomation of the capsules:\n"); > @@ -875,27 +897,28 @@ DumpCapsuleFromDisk ( > FileHandle =3D NULL; > Status =3D DirHandle->Open (DirHandle, &FileHandle, FileInfoBuffer[I= ndex]- > >FileName, EFI_FILE_MODE_READ, 0); > if (EFI_ERROR (Status)) { > - break; > + goto Done; > } >=20 > Status =3D FileHandleGetSize (FileHandle, (UINT64 *) &FileSize); > if (EFI_ERROR (Status)) { > Print (L"Cannot read file %s. Status =3D %r\n", FileInfoBuffer[Ind= ex]- > >FileName, Status); > FileHandleClose (FileHandle); > - return Status; > + goto Done; > } >=20 > FileBuffer =3D AllocatePool (FileSize); > if (FileBuffer =3D=3D NULL) { > - return RETURN_OUT_OF_RESOURCES; > + Status =3D EFI_OUT_OF_RESOURCES; > + goto Done; > } >=20 > Status =3D FileHandleRead (FileHandle, &FileSize, FileBuffer); > if (EFI_ERROR (Status)) { > Print (L"Cannot read file %s. Status =3D %r\n", FileInfoBuffer[Ind= ex]- > >FileName, Status); > - FreePool (FileBuffer); > FileHandleClose (FileHandle); > - return Status; > + FreePool (FileBuffer); > + goto Done; > } >=20 > Print (L"**************************\n"); > @@ -906,7 +929,17 @@ DumpCapsuleFromDisk ( > FreePool (FileBuffer); > } >=20 > - return EFI_SUCCESS; > +Done: > + if (FileInfoBuffer !=3D NULL) { > + for (Index =3D 0; Index < FileCount; Index++) { > + if (FileInfoBuffer[Index] !=3D NULL) { > + FreePool (FileInfoBuffer[Index]); > + } > + } > + FreePool (FileInfoBuffer); > + } > + > + return Status; > } >=20 > /** > -- > 2.16.2.windows.1