From 4b5168d852be0d641be73e8399622704998c183f Mon Sep 17 00:00:00 2001 From: Jaben Carsey Date: Mon, 31 Mar 2014 20:43:04 +0000 Subject: ShellPkg: Fix potential memory leak when failing to fully create a structure Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jaben Carsey Reviewed-by: Erik Bjorge git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15423 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Application/Shell/ShellProtocol.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'ShellPkg/Application') diff --git a/ShellPkg/Application/Shell/ShellProtocol.c b/ShellPkg/Application/Shell/ShellProtocol.c index 65c3c46a46..c702a35914 100644 --- a/ShellPkg/Application/Shell/ShellProtocol.c +++ b/ShellPkg/Application/Shell/ShellProtocol.c @@ -1770,6 +1770,19 @@ EfiShellRemoveDupInFileList( } return (EFI_SUCCESS); } + +// +// This is the same structure as the external version, but it has no CONST qualifiers. +// +typedef struct { + LIST_ENTRY Link; ///< Linked list members. + EFI_STATUS Status; ///< Status of opening the file. Valid only if Handle != NULL. + CHAR16 *FullName; ///< Fully qualified filename. + CHAR16 *FileName; ///< name of this file. + SHELL_FILE_HANDLE Handle; ///< Handle for interacting with the opened file or NULL if closed. + EFI_FILE_INFO *Info; ///< Pointer to the FileInfo struct for this file or NULL. +} EFI_SHELL_FILE_INFO_NO_CONST; + /** Allocates and duplicates a EFI_SHELL_FILE_INFO node. @@ -1786,7 +1799,12 @@ InternalDuplicateShellFileInfo( IN BOOLEAN Save ) { - EFI_SHELL_FILE_INFO *NewNode; + EFI_SHELL_FILE_INFO_NO_CONST *NewNode; + + // + // try to confirm that the objects are in sync + // + ASSERT(sizeof(EFI_SHELL_FILE_INFO_NO_CONST) == sizeof(EFI_SHELL_FILE_INFO)); NewNode = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO)); if (NewNode == NULL) { @@ -1799,7 +1817,11 @@ InternalDuplicateShellFileInfo( if ( NewNode->FullName == NULL || NewNode->FileName == NULL || NewNode->Info == NULL - ){ + ){ + SHELL_FREE_NON_NULL(NewNode->FullName); + SHELL_FREE_NON_NULL(NewNode->FileName); + SHELL_FREE_NON_NULL(NewNode->Info); + SHELL_FREE_NON_NULL(NewNode); return(NULL); } NewNode->Status = Node->Status; @@ -1811,7 +1833,7 @@ InternalDuplicateShellFileInfo( StrCpy((CHAR16*)NewNode->FileName, Node->FileName); CopyMem(NewNode->Info, Node->Info, (UINTN)Node->Info->Size); - return(NewNode); + return((EFI_SHELL_FILE_INFO*)NewNode); } /** -- cgit v1.2.3