Skip to content

Commit bd3fc81

Browse files
Marvin H?userQiu Shumin
Marvin H?user
authored and
Qiu Shumin
committed
ShellPkg/App: Fix memory leak and save resources.
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]>
1 parent dc99315 commit bd3fc81

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

ShellPkg/Application/Shell/Shell.c

+14-2
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ UefiMain (
342342
UINTN Size;
343343
EFI_HANDLE ConInHandle;
344344
EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn;
345+
SPLIT_LIST *Split;
345346

346347
if (PcdGet8(PcdShellSupportLevel) > 3) {
347348
return (EFI_UNSUPPORTED);
@@ -675,7 +676,17 @@ UefiMain (
675676
}
676677

677678
if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){
678-
ASSERT(FALSE); ///@todo finish this de-allocation.
679+
ASSERT(FALSE); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).
680+
681+
for ( Split = (SPLIT_LIST*)GetFirstNode (&ShellInfoObject.SplitList.Link)
682+
; !IsNull (&ShellInfoObject.SplitList.Link, &Split->Link)
683+
; Split = (SPLIT_LIST *)GetNextNode (&ShellInfoObject.SplitList.Link, &Split->Link)
684+
) {
685+
RemoveEntryList (&Split->Link);
686+
FreePool (Split);
687+
}
688+
689+
DEBUG_CODE (InitializeListHead (&ShellInfoObject.SplitList.Link););
679690
}
680691

681692
if (ShellInfoObject.ShellInitSettings.FileName != NULL) {
@@ -1743,11 +1754,12 @@ RunSplitCommand(
17431754
//
17441755
// Note that the original StdIn is now the StdOut...
17451756
//
1746-
if (Split->SplitStdOut != NULL && Split->SplitStdOut != StdIn) {
1757+
if (Split->SplitStdOut != NULL) {
17471758
ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));
17481759
}
17491760
if (Split->SplitStdIn != NULL) {
17501761
ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));
1762+
FreePool (Split->SplitStdIn);
17511763
}
17521764

17531765
FreePool(Split);

0 commit comments

Comments
 (0)