From 23f12576d02eb199e5f8aca42d93bcacd3fef8c8 Mon Sep 17 00:00:00 2001 From: Ruiyu Ni Date: Tue, 12 Jul 2016 18:00:01 +0800 Subject: ShellPkg/EfiDecompress: Handle memory allocation failure Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ruiyu Ni Reviewed-by: Jaben Carsey (cherry picked from commit aba0ca78c6a96433af9dba74e4e56487b0c27d2e) --- .../UefiShellDebug1CommandsLib/EfiDecompress.c | 52 ++++++++++++---------- 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'ShellPkg') diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c index c1457430d8..90eafe570b 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c @@ -2,7 +2,7 @@ Main file for EfiDecompress shell Debug1 function. (C) Copyright 2015 Hewlett-Packard Development Company, L.P.
- Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.
+ Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -119,14 +119,18 @@ ShellCommandRunEfiDecompress ( InSize = (UINTN)Temp64Bit; ASSERT_EFI_ERROR(Status); InBuffer = AllocateZeroPool(InSize); - ASSERT(InBuffer != NULL); - Status = gEfiShellProtocol->ReadFile(InFileHandle, &InSize, InBuffer); - ASSERT_EFI_ERROR(Status); + if (InBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + Status = gEfiShellProtocol->ReadFile (InFileHandle, &InSize, InBuffer); + ASSERT_EFI_ERROR (Status); - Status = gBS->LocateProtocol(&gEfiDecompressProtocolGuid, NULL, (VOID**)&Decompress); - ASSERT_EFI_ERROR(Status); + Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID**) &Decompress); + ASSERT_EFI_ERROR (Status); + + Status = Decompress->GetInfo (Decompress, InBuffer, (UINT32) InSize, &OutSize, &ScratchSize); + } - Status = Decompress->GetInfo(Decompress, InBuffer, (UINT32)InSize, &OutSize, &ScratchSize); if (EFI_ERROR(Status) || OutSize == 0) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_EFI_DECOMPRESS_NOPE), gShellDebug1HiiHandle, InFileName); ShellStatus = SHELL_NOT_FOUND; @@ -138,25 +142,25 @@ ShellCommandRunEfiDecompress ( } else { OutBuffer = AllocateZeroPool(OutSize); ScratchBuffer = AllocateZeroPool(ScratchSize); - ASSERT(OutBuffer != NULL); - ASSERT(ScratchBuffer != NULL); - - Status = Decompress->Decompress(Decompress, InBuffer, (UINT32)InSize, OutBuffer, OutSize, ScratchBuffer, ScratchSize); - ASSERT_EFI_ERROR(Status); - - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_EFI_DECOMPRESS_FAIL), gShellDebug1HiiHandle, Status); - ShellStatus = SHELL_DEVICE_ERROR; + if (OutBuffer == NULL || ScratchBuffer == NULL) { + Status = EFI_OUT_OF_RESOURCES; } else { - OutSizeTemp = OutSize; - Status = gEfiShellProtocol->WriteFile(OutFileHandle, &OutSizeTemp, OutBuffer); - OutSize = (UINT32)OutSizeTemp; - if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_WRITE_FAIL), gShellDebug1HiiHandle, L"efidecompress", OutFileName, Status); - ShellStatus = SHELL_DEVICE_ERROR; - } + Status = Decompress->Decompress (Decompress, InBuffer, (UINT32) InSize, OutBuffer, OutSize, ScratchBuffer, ScratchSize); } - } + } + } + + if (EFI_ERROR (Status)) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_EFI_DECOMPRESS_FAIL), gShellDebug1HiiHandle, Status); + ShellStatus = ((Status == EFI_OUT_OF_RESOURCES) ? SHELL_OUT_OF_RESOURCES : SHELL_DEVICE_ERROR); + } else { + OutSizeTemp = OutSize; + Status = gEfiShellProtocol->WriteFile (OutFileHandle, &OutSizeTemp, OutBuffer); + OutSize = (UINT32) OutSizeTemp; + if (EFI_ERROR (Status)) { + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_WRITE_FAIL), gShellDebug1HiiHandle, L"efidecompress", OutFileName, Status); + ShellStatus = SHELL_DEVICE_ERROR; + } } } } -- cgit v1.2.3