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
This commit is contained in:
jcarsey 2011-10-10 20:32:17 +00:00
parent beab0fc5e2
commit ecae51177e
9 changed files with 56 additions and 21 deletions

View File

@ -1327,7 +1327,8 @@ FileInterfaceMemGetPosition(
@param[in, out] BufferSize Size in bytes of Buffer.
@param[in] Buffer The pointer to the buffer to write.
@retval EFI_SUCCESS The data was written.
@retval EFI_OUT_OF_RESOURCES The operation failed due to lack of resources.
@retval EFI_SUCCESS The data was written.
**/
EFI_STATUS
EFIAPI
@ -1354,6 +1355,9 @@ FileInterfaceMemWrite(
// Ascii
//
AsciiBuffer = AllocateZeroPool(*BufferSize);
if (AsciiBuffer == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
AsciiSPrint(AsciiBuffer, *BufferSize, "%S", Buffer);
if ((UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->Position + AsciiStrSize(AsciiBuffer)) > (UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize)) {
((EFI_FILE_PROTOCOL_MEM*)This)->Buffer = ReallocatePool((UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize), (UINTN)(((EFI_FILE_PROTOCOL_MEM*)This)->BufferSize) + AsciiStrSize(AsciiBuffer) + 10, ((EFI_FILE_PROTOCOL_MEM*)This)->Buffer);

View File

@ -548,7 +548,8 @@ ManFileFindTitleSection(
@retval EFI_SUCCESS The help text was returned.
@retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
returned help text.
@retval EFI_INVALID_PARAMETER HelpText is NULL
@retval EFI_INVALID_PARAMETER HelpText is NULL.
@retval EFI_INVALID_PARAMETER ManFileName is invalid.
@retval EFI_NOT_FOUND There is no help text available for Command.
**/
EFI_STATUS
@ -594,6 +595,9 @@ ProcessManFile(
} else {
FileHandle = NULL;
TempString = GetManFileName(ManFileName);
if (TempString == NULL) {
return (EFI_INVALID_PARAMETER);
}
Status = SearchPathForFile(TempString, &FileHandle);
if (EFI_ERROR(Status)) {

View File

@ -206,8 +206,10 @@ TxtOutProtocolDumpInformation(
RetVal = AllocateZeroPool(Size);
Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_TXT_OUT_DUMP_HEADER), NULL);
UnicodeSPrint(RetVal, Size, Temp, Dev, Dev->Mode->Attribute);
FreePool(Temp);
if (Temp != NULL) {
UnicodeSPrint(RetVal, Size, Temp, Dev, Dev->Mode->Attribute);
FreePool(Temp);
}
//
// Dump TextOut Info
@ -219,7 +221,7 @@ TxtOutProtocolDumpInformation(
UnicodeSPrint(
RetVal + StrLen(RetVal),
NewSize,
Temp,
Temp == NULL?L"":Temp,
Index == Dev->Mode->Mode ? L'*' : L' ',
Index,
!EFI_ERROR(Status)?Col:-1,

View File

@ -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;
}
}

View File

@ -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);
}
/**

View File

@ -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)) {

View File

@ -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){

View File

@ -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){

View File

@ -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);