ShellPkg: InternalShellExecuteDevicePath: avoid memory leaks

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Brendan Jackman <Brendan.Jackman@arm.com>
Reviewed-by: Olivier Martin <olivier.martin@arm.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15224 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Brendan Jackman 2014-02-11 22:46:56 +00:00 committed by jcarsey
parent 3e2b20a1ad
commit fe6c94d2e1
1 changed files with 27 additions and 25 deletions

View File

@ -1397,6 +1397,7 @@ InternalShellExecuteDevicePath(
UINTN InternalExitDataSize; UINTN InternalExitDataSize;
UINTN *ExitDataSizePtr; UINTN *ExitDataSizePtr;
CHAR16 *ImagePath; CHAR16 *ImagePath;
UINTN Index;
// ExitDataSize is not OPTIONAL for gBS->BootServices, provide somewhere for // ExitDataSize is not OPTIONAL for gBS->BootServices, provide somewhere for
// it to be dumped if the caller doesn't want it. // it to be dumped if the caller doesn't want it.
@ -1482,7 +1483,7 @@ InternalShellExecuteDevicePath(
ShellParamsProtocol.Argv = AllocatePool (sizeof (CHAR16 *)); ShellParamsProtocol.Argv = AllocatePool (sizeof (CHAR16 *));
if (ShellParamsProtocol.Argv == NULL) { if (ShellParamsProtocol.Argv == NULL) {
Status = EFI_OUT_OF_RESOURCES; Status = EFI_OUT_OF_RESOURCES;
goto Cleanup; goto UnloadImage;
} }
ShellParamsProtocol.Argc = 1; ShellParamsProtocol.Argc = 1;
} else { } else {
@ -1506,37 +1507,38 @@ InternalShellExecuteDevicePath(
ExitDataSizePtr, ExitDataSizePtr,
ExitData ExitData
); );
}
//
// Cleanup (and dont overwrite errors)
//
if (EFI_ERROR(Status)) {
CleanupStatus = gBS->UninstallProtocolInterface(
NewHandle,
&gEfiShellParametersProtocolGuid,
&ShellParamsProtocol
);
ASSERT_EFI_ERROR(CleanupStatus);
} else {
CleanupStatus = gBS->UninstallProtocolInterface( CleanupStatus = gBS->UninstallProtocolInterface(
NewHandle, NewHandle,
&gEfiShellParametersProtocolGuid, &gEfiShellParametersProtocolGuid,
&ShellParamsProtocol &ShellParamsProtocol
); );
ASSERT_EFI_ERROR(CleanupStatus); ASSERT_EFI_ERROR(CleanupStatus);
goto FreeAlloc;
}
UnloadImage:
// Unload image - We should only get here if we didn't call StartImage
gBS->UnloadImage (NewHandle);
FreeAlloc:
// Free Argv (Allocated in UpdateArgcArgv)
if (ShellParamsProtocol.Argv != NULL) {
for (Index = 0; Index < ShellParamsProtocol.Argc; Index++) {
if (ShellParamsProtocol.Argv[Index] != NULL) {
FreePool (ShellParamsProtocol.Argv[Index]);
}
}
FreePool (ShellParamsProtocol.Argv);
} }
} }
// Restore environment variables
if (!IsListEmpty(&OrigEnvs)) { if (!IsListEmpty(&OrigEnvs)) {
if (EFI_ERROR(Status)) {
CleanupStatus = SetEnvironmentVariableList(&OrigEnvs);
ASSERT_EFI_ERROR(CleanupStatus);
} else {
CleanupStatus = SetEnvironmentVariableList(&OrigEnvs); CleanupStatus = SetEnvironmentVariableList(&OrigEnvs);
ASSERT_EFI_ERROR (CleanupStatus); ASSERT_EFI_ERROR (CleanupStatus);
} }
}
return(Status); return(Status);
} }