From 81cd2f536ef0097392b51f96405321ce7b01350c Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Thu, 26 Jun 2014 01:38:46 +0000 Subject: Refine code to make it more safely. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Eric Dong Reviewed-by: Jaben Carsey git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15593 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Library/UefiShellDebug1CommandsLib/Bcfg.c | 40 ++++++++++++++-------- .../UefiShellDebug1CommandsLib/Edit/FileBuffer.c | 22 +++++++++--- ShellPkg/Library/UefiShellDebug1CommandsLib/Mm.c | 3 +- .../Library/UefiShellInstall1CommandsLib/Bcfg.c | 26 +++++++++----- ShellPkg/Library/UefiShellLevel1CommandsLib/For.c | 31 +++++------------ 5 files changed, 70 insertions(+), 52 deletions(-) (limited to 'ShellPkg/Library') diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Bcfg.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Bcfg.c index b19d8d9..483aa58 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Bcfg.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Bcfg.c @@ -1,7 +1,7 @@ /** @file Main file for bcfg shell Debug1 function. - Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2014, 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 @@ -1027,12 +1027,14 @@ BcfgDisplayDumpDebug1( Buffer); if (Status == EFI_BUFFER_TOO_SMALL) { Buffer = AllocateZeroPool(BufferSize); - Status = gRT->GetVariable( - VariableName, - (EFI_GUID*)&gEfiGlobalVariableGuid, - NULL, - &BufferSize, - Buffer); + if (Buffer != NULL) { + Status = gRT->GetVariable( + VariableName, + (EFI_GUID*)&gEfiGlobalVariableGuid, + NULL, + &BufferSize, + Buffer); + } } if (EFI_ERROR(Status) || Buffer == NULL) { @@ -1042,8 +1044,12 @@ BcfgDisplayDumpDebug1( if ((*(UINT16*)(Buffer+4)) != 0) { DevPath = AllocateZeroPool(*(UINT16*)(Buffer+4)); - CopyMem(DevPath, Buffer+6+StrSize((CHAR16*)(Buffer+6)), *(UINT16*)(Buffer+4)); - DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE); + if (DevPath != NULL) { + CopyMem(DevPath, Buffer+6+StrSize((CHAR16*)(Buffer+6)), *(UINT16*)(Buffer+4)); + DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE); + } else { + DevPathString = NULL; + } } else { DevPath = NULL; DevPathString = NULL; @@ -1213,12 +1219,16 @@ ShellCommandRunBcfg ( CurrentOperation.Order); if (Status == EFI_BUFFER_TOO_SMALL) { CurrentOperation.Order = AllocateZeroPool(Length+(4*sizeof(CurrentOperation.Order[0]))); - Status = gRT->GetVariable( - CurrentOperation.Target == BcfgTargetBootOrder?(CHAR16*)L"BootOrder":(CHAR16*)L"DriverOrder", - (EFI_GUID*)&gEfiGlobalVariableGuid, - NULL, - &Length, - CurrentOperation.Order); + if (CurrentOperation.Order != NULL) { + Status = gRT->GetVariable( + CurrentOperation.Target == BcfgTargetBootOrder?(CHAR16*)L"BootOrder":(CHAR16*)L"DriverOrder", + (EFI_GUID*)&gEfiGlobalVariableGuid, + NULL, + &Length, + CurrentOperation.Order); + } else { + ShellStatus = SHELL_OUT_OF_RESOURCES; + } } } diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/FileBuffer.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/FileBuffer.c index 1795598..897700b 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/FileBuffer.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/FileBuffer.c @@ -1,7 +1,7 @@ /** @file Implements filebuffer interface functions. - Copyright (c) 2005 - 2012, Intel Corporation. All rights reserved.
+ Copyright (c) 2005 - 2014, 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 @@ -489,8 +489,9 @@ FileBufferPrintLine ( CHAR16 *Buffer; UINTN Limit; - CHAR16 PrintLine[200]; - CHAR16 PrintLine2[250]; + CHAR16 *PrintLine; + CHAR16 *PrintLine2; + UINTN BufLen; // // print start from correct character @@ -502,13 +503,21 @@ FileBufferPrintLine ( Limit = 0; } - StrnCpy (PrintLine, Buffer, MIN(MIN(Limit,MainEditor.ScreenSize.Column), 200)); + BufLen = (MainEditor.ScreenSize.Column + 1) * sizeof (CHAR16); + PrintLine = AllocatePool (BufLen); + ASSERT (PrintLine != NULL); + + StrnCpy (PrintLine, Buffer, MIN(Limit, MainEditor.ScreenSize.Column)); for (; Limit < MainEditor.ScreenSize.Column; Limit++) { PrintLine[Limit] = L' '; } PrintLine[MainEditor.ScreenSize.Column] = CHAR_NULL; - ShellCopySearchAndReplace(PrintLine, PrintLine2, 250, L"%", L"^%", FALSE, FALSE); + + PrintLine2 = AllocatePool (BufLen * 2); + ASSERT (PrintLine2 != NULL); + + ShellCopySearchAndReplace(PrintLine, PrintLine2, BufLen * 2, L"%", L"^%", FALSE, FALSE); ShellPrintEx ( 0, @@ -517,6 +526,9 @@ FileBufferPrintLine ( PrintLine2 ); + FreePool (PrintLine); + FreePool (PrintLine2); + return EFI_SUCCESS; } diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Mm.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Mm.c index 636acb2..29411df 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Mm.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Mm.c @@ -1,7 +1,7 @@ /** @file Main file for Mm shell Debug1 function. - Copyright (c) 2005 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2005 - 2014, 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 @@ -229,6 +229,7 @@ ShellCommandRunMm ( SHELL_STATUS ShellStatus; CONST CHAR16 *Temp; + Value = 0; Address = 0; PciEAddress = 0; IoDev = NULL; diff --git a/ShellPkg/Library/UefiShellInstall1CommandsLib/Bcfg.c b/ShellPkg/Library/UefiShellInstall1CommandsLib/Bcfg.c index b91b34f..a7893f5 100644 --- a/ShellPkg/Library/UefiShellInstall1CommandsLib/Bcfg.c +++ b/ShellPkg/Library/UefiShellInstall1CommandsLib/Bcfg.c @@ -1,7 +1,7 @@ /** @file Main file for bcfg shell Install1 function. - Copyright (c) 2010 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2010 - 2014, 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 @@ -1040,8 +1040,12 @@ BcfgDisplayDumpInstall1( if ((*(UINT16*)(Buffer+4)) != 0) { DevPath = AllocateZeroPool(*(UINT16*)(Buffer+4)); - CopyMem(DevPath, Buffer+6+StrSize((CHAR16*)(Buffer+6)), *(UINT16*)(Buffer+4)); - DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE); + if (DevPath == NULL) { + DevPathString = NULL; + } else { + CopyMem(DevPath, Buffer+6+StrSize((CHAR16*)(Buffer+6)), *(UINT16*)(Buffer+4)); + DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE); + } } else { DevPath = NULL; DevPathString = NULL; @@ -1211,12 +1215,16 @@ ShellCommandRunBcfgInstall ( CurrentOperation.Order); if (Status == EFI_BUFFER_TOO_SMALL) { CurrentOperation.Order = AllocateZeroPool(Length+(4*sizeof(CurrentOperation.Order[0]))); - Status = gRT->GetVariable( - CurrentOperation.Target == BcfgTargetBootOrder?(CHAR16*)L"BootOrder":(CHAR16*)L"DriverOrder", - (EFI_GUID*)&gEfiGlobalVariableGuid, - NULL, - &Length, - CurrentOperation.Order); + if (CurrentOperation.Order == NULL) { + ShellStatus = SHELL_OUT_OF_RESOURCES; + } else { + Status = gRT->GetVariable( + CurrentOperation.Target == BcfgTargetBootOrder?(CHAR16*)L"BootOrder":(CHAR16*)L"DriverOrder", + (EFI_GUID*)&gEfiGlobalVariableGuid, + NULL, + &Length, + CurrentOperation.Order); + } } } diff --git a/ShellPkg/Library/UefiShellLevel1CommandsLib/For.c b/ShellPkg/Library/UefiShellLevel1CommandsLib/For.c index 85b85d8..4fdbdd0 100644 --- a/ShellPkg/Library/UefiShellLevel1CommandsLib/For.c +++ b/ShellPkg/Library/UefiShellLevel1CommandsLib/For.c @@ -1,7 +1,7 @@ /** @file Main file for endfor and for shell level 1 functions. - Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2014, 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 @@ -333,7 +333,7 @@ ShellCommandRunFor ( CurrentScriptFile = ShellCommandGetCurrentScriptFile(); ASSERT(CurrentScriptFile != NULL); - if (CurrentScriptFile->CurrentCommand->Data == NULL) { + if ((CurrentScriptFile->CurrentCommand != NULL) && (CurrentScriptFile->CurrentCommand->Data == NULL)) { FirstPass = TRUE; // @@ -348,8 +348,7 @@ ShellCommandRunFor ( gShellLevel1HiiHandle, L"EndFor", L"For", - CurrentScriptFile->CurrentCommand!=NULL - ?CurrentScriptFile->CurrentCommand->Line:0); + CurrentScriptFile->CurrentCommand->Line); return (SHELL_DEVICE_ERROR); } @@ -459,9 +458,7 @@ ShellCommandRunFor ( STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT), gShellLevel1HiiHandle, ArgSet, - CurrentScriptFile!=NULL - && CurrentScriptFile->CurrentCommand!=NULL - ? CurrentScriptFile->CurrentCommand->Line:0); + CurrentScriptFile->CurrentCommand->Line); ShellStatus = SHELL_INVALID_PARAMETER; } else { TempSpot = StrStr(ArgSetWalker, L")"); @@ -483,9 +480,7 @@ ShellCommandRunFor ( NULL, STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT), gShellLevel1HiiHandle, - CurrentScriptFile!=NULL - && CurrentScriptFile->CurrentCommand!=NULL - ? CurrentScriptFile->CurrentCommand->Line:0); + CurrentScriptFile->CurrentCommand->Line); ShellStatus = SHELL_INVALID_PARAMETER; } else { *TempSpot = CHAR_NULL; @@ -501,9 +496,7 @@ ShellCommandRunFor ( STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT), gShellLevel1HiiHandle, ArgSet, - CurrentScriptFile!=NULL - && CurrentScriptFile->CurrentCommand!=NULL - ? CurrentScriptFile->CurrentCommand->Line:0); + CurrentScriptFile->CurrentCommand->Line); ShellStatus = SHELL_INVALID_PARAMETER; } else { if (ArgSetWalker[0] == L'-') { @@ -523,9 +516,7 @@ ShellCommandRunFor ( STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT), gShellLevel1HiiHandle, ArgSet, - CurrentScriptFile!=NULL - && CurrentScriptFile->CurrentCommand!=NULL - ? CurrentScriptFile->CurrentCommand->Line:0); + CurrentScriptFile->CurrentCommand->Line); ShellStatus = SHELL_INVALID_PARAMETER; } else { if (ArgSetWalker[0] == L'-') { @@ -552,9 +543,7 @@ ShellCommandRunFor ( STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT), gShellLevel1HiiHandle, ArgSet, - CurrentScriptFile!=NULL - && CurrentScriptFile->CurrentCommand!=NULL - ? CurrentScriptFile->CurrentCommand->Line:0); + CurrentScriptFile->CurrentCommand->Line); ShellStatus = SHELL_INVALID_PARAMETER; } else { if (*ArgSetWalker == L')') { @@ -574,9 +563,7 @@ ShellCommandRunFor ( STRING_TOKEN (STR_GEN_PROBLEM_SCRIPT), gShellLevel1HiiHandle, ArgSet, - CurrentScriptFile!=NULL - && CurrentScriptFile->CurrentCommand!=NULL - ? CurrentScriptFile->CurrentCommand->Line:0); + CurrentScriptFile->CurrentCommand->Line); ShellStatus = SHELL_INVALID_PARAMETER; } } -- cgit v1.1