Skip to content

Commit

Permalink
[Cherry-pick] CodeQL Fixes - Second Pass from 202202
Browse files Browse the repository at this point in the history
Makes changes to comply with alerts raised by CodeQL.

A prior pass with a subset of queries used here was already done so this
is mostly addressing issues discovered by enabling a few new queries.

Most of the issues here fall into the following two categories:

1. Potential use of uninitialized pointer
2. Inconsistent integer width used in loop comparison

- [ ] Breaking change?
- Will this change break pre-existing builds or functionality without
action being taken?
  **No** - Should just be refactoring to address alerts

1. Verified CI build
2. Verified boot to EFI shell on QemuQ35Pkg

N/A

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
(cherry picked from commit 43e657c)
  • Loading branch information
makubacki authored and kenlautner committed May 9, 2023
1 parent 3c8294b commit 881e889
Show file tree
Hide file tree
Showing 25 changed files with 121 additions and 59 deletions.
2 changes: 1 addition & 1 deletion MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1859,7 +1859,7 @@ PciProgramResizableBar (
);
ASSERT_EFI_ERROR (Status);

for (Index = 0; Index < ResizableBarNumber; Index++) {
for (Index = 0; (UINTN)Index < ResizableBarNumber; Index++) {
//
// When the bit of Capabilities Set, indicates that the Function supports
// operating with the BAR sized to (2^Bit) MB.
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Bus/Scsi/ScsiDiskDxe/ScsiDisk.c
Original file line number Diff line number Diff line change
Expand Up @@ -2615,7 +2615,7 @@ ScsiDiskInquiryDevice (
//
// Locate the code for the Block Limits VPD page
//
for (Index = 0; Index < PageLength; Index++) {
for (Index = 0; (UINTN)Index < PageLength; Index++) {
//
// Sanity check
//
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ UfsInitUtpPrdt (
Remaining = Buffer;
PrdtNumber = (UINTN)DivU64x32 ((UINT64)BufferSize + UFS_MAX_DATA_LEN_PER_PRD - 1, UFS_MAX_DATA_LEN_PER_PRD);

for (PrdtIndex = 0; PrdtIndex < PrdtNumber; PrdtIndex++) {
for (PrdtIndex = 0; (UINTN)PrdtIndex < PrdtNumber; PrdtIndex++) {
if (RemainingLen < UFS_MAX_DATA_LEN_PER_PRD) {
Prdt[PrdtIndex].DbCount = (UINT32)RemainingLen - 1;
} else {
Expand Down
11 changes: 1 addition & 10 deletions MdeModulePkg/Core/Dxe/Hand/Handle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1166,16 +1166,7 @@ CoreOpenProtocol (
// Keep Interface unmodified in case of any Error
// except EFI_ALREADY_STARTED and EFI_UNSUPPORTED.
//
if (!EFI_ERROR (Status) || (Status == EFI_ALREADY_STARTED)) {
//
// According to above logic, if 'Prot' is NULL, then the 'Status' must be
// EFI_UNSUPPORTED. Here the 'Status' is not EFI_UNSUPPORTED, so 'Prot'
// must be not NULL.
//
// The ASSERT here is for addressing a false positive NULL pointer
// dereference issue raised from static analysis.
//
ASSERT (Prot != NULL);
if ((!EFI_ERROR (Status) || (Status == EFI_ALREADY_STARTED)) && (Prot != NULL)) {
//
// EFI_ALREADY_STARTED is not an error for bus driver.
// Return the corresponding protocol interface.
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Core/Pei/FwVol/FwVol.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ FindFileEx (
FileOffset = (UINT32)((UINT8 *)FfsFileHeader - (UINT8 *)FwVolHeader);
ASSERT (FileOffset <= 0xFFFFFFFF);

while (FileOffset < (FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
while ((UINTN)FileOffset < (UINTN)(FvLength - sizeof (EFI_FFS_FILE_HEADER))) {
//
// Get FileState which is the highest bit of the State
//
Expand Down
12 changes: 10 additions & 2 deletions MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxMmLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,11 @@ SaveLockBox (
));

LockBoxQueue = InternalGetLockBoxQueue ();
ASSERT (LockBoxQueue != NULL);
if (LockBoxQueue == NULL) {
ASSERT (LockBoxQueue != NULL);
return EFI_OUT_OF_RESOURCES;
}

InsertTailList (LockBoxQueue, &LockBox->Link);

//
Expand Down Expand Up @@ -840,6 +844,7 @@ RestoreLockBox (
@retval RETURN_SUCCESS the information is restored successfully.
@retval RETURN_NOT_STARTED it is too early to invoke this interface
@retval RETURN_UNSUPPORTED the service is not supported by implementaion.
@retval RETURN_OUT_OF_RESOURCES noT enough resourceS to save the information.
**/
RETURN_STATUS
EFIAPI
Expand All @@ -854,7 +859,10 @@ RestoreAllLockBoxInPlace (
DEBUG ((DEBUG_INFO, "SmmLockBoxSmmLib RestoreAllLockBoxInPlace - Enter\n"));

LockBoxQueue = InternalGetLockBoxQueue ();
ASSERT (LockBoxQueue != NULL);
if (LockBoxQueue == NULL) {
ASSERT (LockBoxQueue != NULL);
return EFI_OUT_OF_RESOURCES;
}

//
// Restore all, Buffer and Length MUST be NULL
Expand Down
30 changes: 27 additions & 3 deletions MdeModulePkg/Library/UefiBootManagerLib/BmBoot.c
Original file line number Diff line number Diff line change
Expand Up @@ -2304,12 +2304,20 @@ BmEnumerateBootOptions (
}

Description = BmGetBootDescription (Handles[Index]);
if (Description == NULL) {
continue;
}

BootOptions = ReallocatePool (
sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),
sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),
BootOptions
);
ASSERT (BootOptions != NULL);
if (BootOptions == NULL) {
ASSERT (BootOptions != NULL);
FreePool (Description);
continue;
}

Status = EfiBootManagerInitializeLoadOption (
&BootOptions[(*BootOptionCount)++],
Expand Down Expand Up @@ -2355,12 +2363,20 @@ BmEnumerateBootOptions (
}

Description = BmGetBootDescription (Handles[Index]);
if (Description == NULL) {
continue;
}

BootOptions = ReallocatePool (
sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),
sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),
BootOptions
);
ASSERT (BootOptions != NULL);
if (BootOptions == NULL) {
ASSERT (BootOptions != NULL);
FreePool (Description);
continue;
}

Status = EfiBootManagerInitializeLoadOption (
&BootOptions[(*BootOptionCount)++],
Expand Down Expand Up @@ -2399,12 +2415,20 @@ BmEnumerateBootOptions (
}

Description = BmGetBootDescription (Handles[Index]);
if (Description == NULL) {
continue;
}

BootOptions = ReallocatePool (
sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount),
sizeof (EFI_BOOT_MANAGER_LOAD_OPTION) * (*BootOptionCount + 1),
BootOptions
);
ASSERT (BootOptions != NULL);
if (BootOptions == NULL) {
ASSERT (BootOptions != NULL);
FreePool (Description);
continue;
}

Status = EfiBootManagerInitializeLoadOption (
&BootOptions[(*BootOptionCount)++],
Expand Down
24 changes: 13 additions & 11 deletions MdeModulePkg/Library/UefiBootManagerLib/BmConsole.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,17 +484,19 @@ EfiBootManagerUpdateConsoleVariable (
}
}

//
// Finally, Update the variable of the default console by NewDevicePath
//
Status = gRT->SetVariable (
mConVarName[ConsoleType],
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
| ((ConsoleType < ConInDev) ? EFI_VARIABLE_NON_VOLATILE : 0),
GetDevicePathSize (NewDevicePath),
NewDevicePath
);
if (NewDevicePath != NULL) {
//
// Finally, Update the variable of the default console by NewDevicePath
//
Status = gRT->SetVariable (
mConVarName[ConsoleType],
&gEfiGlobalVariableGuid,
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
| ((ConsoleType < ConInDev) ? EFI_VARIABLE_NON_VOLATILE : 0),
GetDevicePathSize (NewDevicePath),
NewDevicePath
);
}

if (VarConsole == NewDevicePath) {
if (VarConsole != NULL) {
Expand Down
3 changes: 3 additions & 0 deletions MdeModulePkg/Library/UefiBootManagerLib/BmLoadOption.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ EfiBootManagerSortLoadOptionVariable (
UINT16 *OptionOrder;

LoadOption = EfiBootManagerGetLoadOptions (&LoadOptionCount, OptionType);
if (LoadOption == NULL) {
return;
}

//
// Insertion sort algorithm
Expand Down
5 changes: 4 additions & 1 deletion MdeModulePkg/Library/UefiBootManagerLib/BmMisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ BmDelPartMatchInstance (
}
}

FreePool (Instance);
if (Instance != NULL) {
FreePool (Instance);
}

Instance = GetNextDevicePathInstance (&Multi, &InstanceSize);
InstanceSize -= END_DEVICE_PATH_LENGTH;
}
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Library/UefiHiiLib/HiiLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ ValidateQuestionFromVfr (
// Check IFR value is in block data, then Validate Value
//
PackageOffset = sizeof (EFI_HII_PACKAGE_LIST_HEADER);
while (PackageOffset < PackageListLength) {
while ((UINTN)PackageOffset < PackageListLength) {
CopyMem (&PackageHeader, (UINT8 *)HiiPackageList + PackageOffset, sizeof (PackageHeader));

//
Expand Down
7 changes: 5 additions & 2 deletions MdeModulePkg/Universal/Acpi/AcpiTableDxe/AmlNamespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ AmlConstructNodeList (
@param[in] Parent AML parent node list.
@param[in] AmlByteEncoding AML Byte Encoding.
@return AML Node.
@return AML Node or NULL if insufficient resources to allocate a buffer
**/
EFI_AML_NODE_LIST *
AmlCreateNode (
Expand All @@ -44,7 +44,10 @@ AmlCreateNode (
EFI_AML_NODE_LIST *AmlNodeList;

AmlNodeList = AllocatePool (sizeof (*AmlNodeList));
ASSERT (AmlNodeList != NULL);
if (AmlNodeList == NULL) {
ASSERT (AmlNodeList != NULL);
return NULL;
}

AmlNodeList->Signature = EFI_AML_NODE_LIST_SIGNATURE;
CopyMem (AmlNodeList->Name, NameSeg, AML_NAME_SEG_SIZE);
Expand Down
7 changes: 5 additions & 2 deletions MdeModulePkg/Universal/Acpi/AcpiTableDxe/AmlString.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ AmlUpperCaseCopyMem (
@param[in] AslPath ASL name.
@return AmlName
@return AmlName or NULL if insufficient resources to allocate a buffer
**/
UINT8 *
AmlNameFromAslName (
Expand All @@ -401,7 +401,10 @@ AmlNameFromAslName (
}

AmlPath = AllocatePool (TotalLength);
ASSERT (AmlPath != NULL);
if (AmlPath == NULL) {
ASSERT (AmlPath != NULL);
return NULL;
}

AmlBuffer = AmlPath;
Buffer = AslPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ GraphicsConsoleControllerDriverStart (
//
MaxMode = Private->GraphicsOutput->Mode->MaxMode;

for (ModeIndex = 0; ModeIndex < MaxMode; ModeIndex++) {
for (ModeIndex = 0; (UINTN)ModeIndex < MaxMode; ModeIndex++) {
Status = Private->GraphicsOutput->QueryMode (
Private->GraphicsOutput,
ModeIndex,
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Universal/Disk/PartitionDxe/Mbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ PartitionInstallMbrChildHandles (
if (ExtMbrStartingLba == 0) {
break;
}
} while (ExtMbrStartingLba < ParentHdDev.PartitionSize);
} while ((UINT64)ExtMbrStartingLba < ParentHdDev.PartitionSize);
}

Done:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ FtwGetLastWriteRecord (
//
// Try to find the last write record "that has not completed"
//
for (Index = 0; Index < FtwWriteHeader->NumberOfWrites; Index += 1) {
for (Index = 0; (UINT64)Index < FtwWriteHeader->NumberOfWrites; Index += 1) {
if (FtwRecord->DestinationComplete != FTW_VALID_STATE) {
//
// The last write record is found
Expand Down
6 changes: 3 additions & 3 deletions MdeModulePkg/Universal/HiiDatabaseDxe/Font.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ GlyphToBlt (
// The glyph's upper left hand corner pixel is the most significant bit of the
// first bitmap byte.
//
for (Ypos = 0; Ypos < Cell->Height && (((UINT32)Ypos + YposOffset) < RowHeight); Ypos++) {
for (Ypos = 0; Ypos < Cell->Height && ((UINTN)((UINT32)Ypos + YposOffset) < RowHeight); Ypos++) {
OffsetY = BITMAP_LEN_1_BIT (Cell->Width, Ypos);

//
// All bits in these bytes are meaningful.
//
for (Xpos = 0; Xpos < Cell->Width / 8; Xpos++) {
Data = *(GlyphBuffer + OffsetY + Xpos);
for (Index = 0; Index < 8 && (((UINT32)Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {
for (Index = 0; Index < 8 && ((UINTN)((UINT32)Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {
if ((Data & (1 << (8 - Index - 1))) != 0) {
BltBuffer[Ypos * ImageWidth + Xpos * 8 + Index] = Foreground;
} else {
Expand All @@ -436,7 +436,7 @@ GlyphToBlt (
// There are some padding bits in this byte. Ignore them.
//
Data = *(GlyphBuffer + OffsetY + Xpos);
for (Index = 0; Index < Cell->Width % 8 && (((UINT32)Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {
for (Index = 0; Index < (UINT16)(Cell->Width % 8) && ((UINTN)((UINT32)Xpos * 8 + Index + Cell->OffsetX) < RowWidth); Index++) {
if ((Data & (1 << (8 - Index - 1))) != 0) {
BltBuffer[Ypos * ImageWidth + Xpos * 8 + Index] = Foreground;
} else {
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Universal/HiiDatabaseDxe/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Output1bitPixel (
// Padding bits in this byte should be ignored.
//
Byte = *(Data + OffsetY + Xpos);
for (Index = 0; Index < Image->Width % 8; Index++) {
for (Index = 0; (UINT16)Index < (UINT16)(Image->Width % 8); Index++) {
if ((Byte & (1 << (8 - Index - 1))) != 0) {
CopyMem (&BitMapPtr[Ypos * Image->Width + Xpos * 8 + Index], &PaletteValue[1], sizeof (*BitMapPtr));
} else {
Expand Down
2 changes: 1 addition & 1 deletion MdeModulePkg/Universal/PCD/Pei/Pcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ PeiPcdSetSku (
}

SkuIdTable = (SKU_ID *)((UINT8 *)PeiPcdDb + PeiPcdDb->SkuIdTableOffset);
for (Index = 0; Index < SkuIdTable[0]; Index++) {
for (Index = 0; (UINT64)Index < SkuIdTable[0]; Index++) {
if (SkuId == SkuIdTable[Index + 1]) {
DEBUG ((DEBUG_INFO, "PcdPei - SkuId is found in SkuId table.\n"));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ InternalProtocolGetVariablePolicyInfo (
UINTN BufferSize;
UINTN VariableNameSize;

PolicyHeader = NULL;

if ((VariableName == NULL) || (VendorGuid == NULL) || (VariablePolicy == NULL)) {
return EFI_INVALID_PARAMETER;
}
Expand Down Expand Up @@ -611,7 +613,11 @@ InternalProtocolGetVariablePolicyInfo (
Done:
ReleaseLockOnlyAtBootTime (&mMmCommunicationLock);

return (EFI_ERROR (Status)) ? Status : PolicyHeader->Result;
if (EFI_ERROR (Status)) {
return Status;
}

return (PolicyHeader != NULL) ? PolicyHeader->Result : EFI_SUCCESS;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion MdePkg/Library/BasePeCoffLib/BasePeCoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ PeCoffLoaderGetPeHeader (
UINTN Size;
UINTN ReadSize;
UINT32 SectionHeaderOffset;
UINT32 Index;
UINTN Index;
UINT32 HeaderWithoutDataDir;
CHAR8 BufferData;
UINTN NumberOfSections;
Expand Down
Loading

0 comments on commit 881e889

Please sign in to comment.