Skip to content

Commit

Permalink
ShellPkg/App: Fix memory leak and save resources.
Browse files Browse the repository at this point in the history
1) RunSplitCommand() allocates the initial SplitStdOut via
   CreateFileInterfaceMem(). Free SplitStdIn after the swap to fix
   the memory leak.

2) In RunSplitCommand(), SplitStdOut is checked for equality with
   StdIn. This cannot happen due to the if-check within the swap.
   Hence remove it.

3) UefiMain() doesn't free SplitList. Delete all list entries and
   reinitialize the list when in DEBUG. This does not include the
   CreateFileInterfaceMem()-allocated SplitStd mentioned in 1), so
   keep the ASSERT() until resolved.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Marvin Haeuser <[email protected]>
Reviewed-by: Qiu Shumin <[email protected]>
  • Loading branch information
Marvin H?user authored and Qiu Shumin committed May 25, 2016
1 parent dc99315 commit bd3fc81
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions ShellPkg/Application/Shell/Shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ UefiMain (
UINTN Size;
EFI_HANDLE ConInHandle;
EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn;
SPLIT_LIST *Split;

if (PcdGet8(PcdShellSupportLevel) > 3) {
return (EFI_UNSUPPORTED);
Expand Down Expand Up @@ -675,7 +676,17 @@ UefiMain (
}

if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){
ASSERT(FALSE); ///@todo finish this de-allocation.
ASSERT(FALSE); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).

for ( Split = (SPLIT_LIST*)GetFirstNode (&ShellInfoObject.SplitList.Link)
; !IsNull (&ShellInfoObject.SplitList.Link, &Split->Link)
; Split = (SPLIT_LIST *)GetNextNode (&ShellInfoObject.SplitList.Link, &Split->Link)
) {
RemoveEntryList (&Split->Link);
FreePool (Split);
}

DEBUG_CODE (InitializeListHead (&ShellInfoObject.SplitList.Link););
}

if (ShellInfoObject.ShellInitSettings.FileName != NULL) {
Expand Down Expand Up @@ -1743,11 +1754,12 @@ RunSplitCommand(
//
// Note that the original StdIn is now the StdOut...
//
if (Split->SplitStdOut != NULL && Split->SplitStdOut != StdIn) {
if (Split->SplitStdOut != NULL) {
ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));
}
if (Split->SplitStdIn != NULL) {
ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));
FreePool (Split->SplitStdIn);
}

FreePool(Split);
Expand Down

0 comments on commit bd3fc81

Please sign in to comment.