From ecae51177e83db0d99f8b4888ae4b866c18651b6 Mon Sep 17 00:00:00 2001 From: jcarsey Date: Mon, 10 Oct 2011 20:32:17 +0000 Subject: ShellPkg: Add checks for NULL pointers. This adds lots of pointer verification with ASSERTs only used when the condition should be impossible and never for memory allocation. signed-off-by: jcarsey reviewed-by: geekboy15a git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12523 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c | 17 +++++++++++------ .../Library/UefiShellDebug1CommandsLib/Compress.c | 19 +++++++++++++++---- .../Library/UefiShellDebug1CommandsLib/Edit/Edit.c | 5 ++++- .../Library/UefiShellDebug1CommandsLib/EfiCompress.c | 7 +++++-- .../UefiShellDebug1CommandsLib/EfiDecompress.c | 7 +++++-- .../UefiShellDebug1CommandsLib/HexEdit/FileImage.c | 2 +- 6 files changed, 41 insertions(+), 16 deletions(-) (limited to 'ShellPkg/Library/UefiShellDebug1CommandsLib') diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c index b59ef31682..01ef1997b2 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c @@ -49,6 +49,7 @@ ShellCommandRunComp ( UINTN DataSizeFromFile2; CHAR16 *FileName1; CHAR16 *FileName2; + CONST CHAR16 *TempParam; ErrorCount = 0; ShellStatus = SHELL_SUCCESS; @@ -88,25 +89,29 @@ ShellCommandRunComp ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle); ShellStatus = SHELL_INVALID_PARAMETER; } else { - FileName1 = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + FileName1 = ShellFindFilePath(TempParam); if (FileName1 == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { Status = ShellOpenFileByName(FileName1, &FileHandle1, EFI_FILE_MODE_READ, 0); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1), Status); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, TempParam, Status); ShellStatus = SHELL_NOT_FOUND; } } - FileName2 = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 2)); + TempParam = ShellCommandLineGetRawValue(Package, 2); + ASSERT(TempParam != NULL); + FileName2 = ShellFindFilePath(TempParam); if (FileName2 == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 2)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { Status = ShellOpenFileByName(FileName2, &FileHandle2, EFI_FILE_MODE_READ, 0); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 2), Status); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_OPEN_FAIL), gShellDebug1HiiHandle, TempParam, Status); ShellStatus = SHELL_NOT_FOUND; } } diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c index 06a3210ea0..b1f8327f4a 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Compress.c @@ -607,8 +607,10 @@ FreadCrc ( Advance the current position (read in new data if needed). Delete outdated string info. Find a match string for current position. + @retval TRUE The operation was successful. + @retval FALSE The operation failed due to insufficient memory. **/ -VOID +BOOLEAN EFIAPI GetNextMatch ( VOID @@ -621,6 +623,9 @@ GetNextMatch ( mPos++; if (mPos == WNDSIZ * 2) { Temp = AllocateZeroPool (WNDSIZ + MAXMATCH); + if (Temp == NULL) { + return (FALSE); + } CopyMem (Temp, &mText[WNDSIZ], WNDSIZ + MAXMATCH); CopyMem (&mText[0], Temp, WNDSIZ + MAXMATCH); FreePool (Temp); @@ -631,6 +636,8 @@ GetNextMatch ( DeleteNode (); InsertNode (); + + return (TRUE); } /** @@ -1286,7 +1293,9 @@ Encode ( while (mRemainder > 0) { LastMatchLen = mMatchLen; LastMatchPos = mMatchPos; - GetNextMatch (); + if (!GetNextMatch ()) { + Status = EFI_OUT_OF_RESOURCES; + } if (mMatchLen > mRemainder) { mMatchLen = mRemainder; } @@ -1306,7 +1315,9 @@ Encode ( (mPos - LastMatchPos - 2) & (WNDSIZ - 1)); LastMatchLen--; while (LastMatchLen > 0) { - GetNextMatch (); + if (!GetNextMatch ()) { + Status = EFI_OUT_OF_RESOURCES; + } LastMatchLen--; } @@ -1318,7 +1329,7 @@ Encode ( HufEncodeEnd (); FreeMemory (); - return EFI_SUCCESS; + return (Status); } /** diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c index c4dbf80c96..c28f9bc591 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c @@ -36,6 +36,7 @@ ShellCommandRunEdit ( CONST CHAR16 *Cwd; CHAR16 *Nfs; CHAR16 *Spot; + CONST CHAR16 *TempParam; // SHELL_FILE_HANDLE TempHandle; Buffer = NULL; @@ -101,7 +102,9 @@ ShellCommandRunEdit ( // if editor launched with file named // if (ShellCommandLineGetCount(Package) == 2) { - FileBufferSetFileName (ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + FileBufferSetFileName (TempParam); // if (EFI_ERROR(ShellFileExists(MainEditor.FileBuffer->FileName))) { // Status = ShellOpenFileByName(MainEditor.FileBuffer->FileName, &TempHandle, EFI_FILE_MODE_CREATE|EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0); // if (!EFI_ERROR(Status)) { diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c index d4bb6217b0..25e8718980 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c @@ -42,6 +42,7 @@ ShellCommandRunEfiCompress ( VOID *InBuffer; CHAR16 *InFileName; CONST CHAR16 *OutFileName; + CONST CHAR16 *TempParam; InFileName = NULL; OutFileName = NULL; @@ -82,10 +83,12 @@ ShellCommandRunEfiCompress ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle); ShellStatus = SHELL_INVALID_PARAMETER; } else { - InFileName = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + InFileName = ShellFindFilePath(TempParam); OutFileName = ShellCommandLineGetRawValue(Package, 2); if (InFileName == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { if (ShellIsDirectory(InFileName) == EFI_SUCCESS){ diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c index 4b4671b455..35a32f1f3b 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c @@ -46,6 +46,7 @@ ShellCommandRunEfiDecompress ( UINT32 ScratchSize; VOID *ScratchBuffer; EFI_DECOMPRESS_PROTOCOL *Decompress; + CONST CHAR16 *TempParam; InFileName = NULL; OutFileName = NULL; @@ -87,10 +88,12 @@ ShellCommandRunEfiDecompress ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle); ShellStatus = SHELL_INVALID_PARAMETER; } else { - InFileName = ShellFindFilePath(ShellCommandLineGetRawValue(Package, 1)); + TempParam = ShellCommandLineGetRawValue(Package, 1); + ASSERT(TempParam != NULL); + InFileName = ShellFindFilePath(TempParam); OutFileName = ShellCommandLineGetRawValue(Package, 2); if (InFileName == NULL) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, ShellCommandLineGetRawValue(Package, 1)); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, TempParam); ShellStatus = SHELL_NOT_FOUND; } else { if (ShellIsDirectory(InFileName) == EFI_SUCCESS){ diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c index 8562662638..f5fb7d262a 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c @@ -169,7 +169,7 @@ HFileImageRead ( // you should set the status string // Status = ReadFileIntoBuffer (FileName, (VOID**)&Buffer, &HFileImage.Size, &HFileImage.ReadOnly); - if (EFI_ERROR(Status)) { + if (EFI_ERROR(Status) || Buffer == NULL) { UnicodeBuffer = CatSPrint(NULL, L"Read error on file &s: %r", FileName, Status); if (UnicodeBuffer == NULL) { SHELL_FREE_NON_NULL(Buffer); -- cgit v1.2.3