From 107d05a433c3ea5d877baaa4867ede7708323d38 Mon Sep 17 00:00:00 2001 From: Ruiyu Ni Date: Tue, 12 Jul 2016 17:38:06 +0800 Subject: [PATCH] ShellPkg/UefiShellCommandLib.c: Handle memory allocation failure Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ruiyu Ni Reviewed-by: Jaben Carsey --- .../UefiShellCommandLib/UefiShellCommandLib.c | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c index 48e4d4af0d..35e0611a8e 100644 --- a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c +++ b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c @@ -1,7 +1,7 @@ /** @file Provides interface to shell internal functions for shell commands. - Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
(C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
(C) Copyright 2016 Hewlett Packard Enterprise Development LP
@@ -546,9 +546,14 @@ ShellCommandRegisterCommandName ( // allocate memory for new struct // Node = AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY)); - ASSERT(Node != NULL); + if (Node == NULL) { + return RETURN_OUT_OF_RESOURCES; + } Node->CommandString = AllocateCopyPool(StrSize(CommandString), CommandString); - ASSERT(Node->CommandString != NULL); + if (Node->CommandString == NULL) { + FreePool (Node); + return RETURN_OUT_OF_RESOURCES; + } Node->GetManFileName = GetManFileName; Node->CommandHandler = CommandHandler; @@ -807,11 +812,20 @@ ShellCommandRegisterAlias ( // allocate memory for new struct // Node = AllocateZeroPool(sizeof(ALIAS_LIST)); - ASSERT(Node != NULL); + if (Node == NULL) { + return RETURN_OUT_OF_RESOURCES; + } Node->CommandString = AllocateCopyPool(StrSize(Command), Command); + if (Node->CommandString == NULL) { + FreePool (Node); + return RETURN_OUT_OF_RESOURCES; + } Node->Alias = AllocateCopyPool(StrSize(Alias), Alias); - ASSERT(Node->CommandString != NULL); - ASSERT(Node->Alias != NULL); + if (Node->Alias == NULL) { + FreePool (Node->CommandString); + FreePool (Node); + return RETURN_OUT_OF_RESOURCES; + } InsertHeadList (&mAliasList.Link, &Node->Link); @@ -1303,7 +1317,10 @@ ShellCommandCreateInitialMappingsAndPaths( // Get all Device Paths // DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count); - ASSERT(DevicePathList != NULL); + if (DevicePathList == NULL) { + SHELL_FREE_NON_NULL (HandleList); + return EFI_OUT_OF_RESOURCES; + } for (Count = 0 ; HandleList[Count] != NULL ; Count++) { DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]); @@ -1360,7 +1377,10 @@ ShellCommandCreateInitialMappingsAndPaths( // Get all Device Paths // DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count); - ASSERT(DevicePathList != NULL); + if (DevicePathList == NULL) { + SHELL_FREE_NON_NULL (HandleList); + return EFI_OUT_OF_RESOURCES; + } for (Count = 0 ; HandleList[Count] != NULL ; Count++) { DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]); -- 2.39.2