ShellPkg: UefiShellLevel2CommandsLib: CodeQL Fixes

Includes changes across the module for the following CodeQL rules:
- cpp/comparison-with-wider-type
- cpp/overflow-buffer
- cpp/redundant-null-check-param
- cpp/uselesstest

Co-authored-by: Taylor Beebe <taylor.d.beebe@gmail.com>

Signed-off-by: Oliver Smith-Denny <osde@linux.microsoft.com>
This commit is contained in:
Oliver Smith-Denny 2024-10-03 10:25:05 -07:00 committed by mergify[bot]
parent 040afc1e3b
commit 17ad30ae23
7 changed files with 85 additions and 37 deletions

View File

@ -304,7 +304,12 @@ ShellCommandRunCd (
if (!EFI_ERROR (Status)) {
Param1Copy = PathCleanUpDirectories (Param1Copy);
Status = ExtractDriveAndPath (Param1Copy, &Drive, &Path);
if (Param1Copy == NULL) {
Status = EFI_NOT_FOUND;
ShellStatus = SHELL_INVALID_PARAMETER;
} else {
Status = ExtractDriveAndPath (Param1Copy, &Drive, &Path);
}
}
if (!EFI_ERROR (Status) && (Drive != NULL) && (Path != NULL)) {

View File

@ -203,12 +203,17 @@ CopySingleFile (
if (Status == EFI_BUFFER_TOO_SMALL) {
DestVolumeInfo = AllocateZeroPool (DestVolumeInfoSize);
Status = DestVolumeFP->GetInfo (
DestVolumeFP,
&gEfiFileSystemInfoGuid,
&DestVolumeInfoSize,
DestVolumeInfo
);
if (DestVolumeInfo == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"cp");
return (SHELL_OUT_OF_RESOURCES);
}
Status = DestVolumeFP->GetInfo (
DestVolumeFP,
&gEfiFileSystemInfoGuid,
&DestVolumeInfoSize,
DestVolumeInfo
);
}
//

View File

@ -42,6 +42,9 @@ PrintSfoVolumeInfoTableEntry (
if (Node->Handle == NULL) {
DirectoryName = GetFullyQualifiedPath (((EFI_SHELL_FILE_INFO *)GetFirstNode (&TheList->Link))->FullName);
if (DirectoryName == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
//
// We need to open something up to get system information
@ -70,12 +73,16 @@ PrintSfoVolumeInfoTableEntry (
if (Status == EFI_BUFFER_TOO_SMALL) {
SysInfo = AllocateZeroPool (SysInfoSize);
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
if (SysInfo == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
}
ASSERT_EFI_ERROR (Status);
@ -97,12 +104,16 @@ PrintSfoVolumeInfoTableEntry (
if (Status == EFI_BUFFER_TOO_SMALL) {
SysInfo = AllocateZeroPool (SysInfoSize);
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
if (SysInfo == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
}
ASSERT_EFI_ERROR (Status);
@ -616,7 +627,11 @@ PrintLsOutput (
}
CorrectedPath = StrnCatGrow (&CorrectedPath, &LongestPath, L"*", 0);
Status = ShellOpenFileMetaArg ((CHAR16 *)CorrectedPath, EFI_FILE_MODE_READ, &ListHead);
if (CorrectedPath == NULL) {
return SHELL_OUT_OF_RESOURCES;
}
Status = ShellOpenFileMetaArg ((CHAR16 *)CorrectedPath, EFI_FILE_MODE_READ, &ListHead);
if (!EFI_ERROR (Status)) {
for ( Node = (EFI_SHELL_FILE_INFO *)GetFirstNode (&ListHead->Link)

View File

@ -82,7 +82,11 @@ SearchList (
)
{
TempList = StrnCatGrow (&TempList, NULL, ListWalker, 0);
ASSERT (TempList != NULL);
if (TempList == NULL) {
ASSERT (TempList != NULL);
return (FALSE);
}
TempSpot = StrStr (TempList, Target);
if (TempSpot != NULL) {
*TempSpot = CHAR_NULL;

View File

@ -315,7 +315,11 @@ ShellCommandRunParse (
ShellCommandInstance = ShellStrToUintn (ShellCommandLineGetValue (Package, L"-s"));
}
ShellStatus = PerformParsing (FileName, TableName, ShellStrToUintn (ColumnString), TableNameInstance, ShellCommandInstance, StreamingUnicode);
if ((FileName != NULL) && (TableName != NULL) && (ColumnString != NULL)) {
ShellStatus = PerformParsing (FileName, TableName, ShellStrToUintn (ColumnString), TableNameInstance, ShellCommandInstance, StreamingUnicode);
} else {
ShellStatus = SHELL_INVALID_PARAMETER;
}
}
}

View File

@ -220,7 +220,7 @@ IsValidDeleteTarget (
}
TempLocation = StrStr (Node->FullName, L":");
if (StrLen (TempLocation) <= 2) {
if ((TempLocation == NULL) || (StrLen (TempLocation) <= 2)) {
//
// Deleting the root directory is invalid.
//
@ -242,6 +242,11 @@ IsValidDeleteTarget (
Pattern = StrnCatGrow (&Pattern, &Size, L"\\", 0);
Size = 0;
SearchString = StrnCatGrow (&SearchString, &Size, Node->FullName, 0);
if (SearchString == NULL) {
RetVal = FALSE;
goto Done;
}
if (!EFI_ERROR (ShellIsDirectory (SearchString))) {
SearchString = StrnCatGrow (&SearchString, &Size, L"\\", 0);
SearchString = StrnCatGrow (&SearchString, &Size, L"*", 0);
@ -256,6 +261,7 @@ IsValidDeleteTarget (
}
}
Done:
SHELL_FREE_NON_NULL (Pattern);
SHELL_FREE_NON_NULL (SearchString);

View File

@ -88,15 +88,19 @@ HandleVol (
if (Status == EFI_BUFFER_TOO_SMALL) {
SysInfo = AllocateZeroPool (SysInfoSize);
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
}
if (SysInfo == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"vol");
ASSERT (FALSE);
return SHELL_OUT_OF_RESOURCES;
}
ASSERT (SysInfo != NULL);
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
}
if (Delete) {
*((CHAR16 *)SysInfo->VolumeLabel) = CHAR_NULL;
@ -155,12 +159,17 @@ HandleVol (
if (Status == EFI_BUFFER_TOO_SMALL) {
SysInfo = AllocateZeroPool (SysInfoSize);
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
if (SysInfo == NULL) {
ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"vol");
return SHELL_OUT_OF_RESOURCES;
}
Status = EfiFpHandle->GetInfo (
EfiFpHandle,
&gEfiFileSystemInfoGuid,
&SysInfoSize,
SysInfo
);
}
gEfiShellProtocol->CloseFile (ShellFileHandle);