Skip to content

Commit

Permalink
MdePkg: CodeQL Fixes.
Browse files Browse the repository at this point in the history
Includes changes across the repo 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 <tabeebe@microsoft.com>
Co-authored-by: kenlautner <85201046+kenlautner@users.noreply.github.com>
  • Loading branch information
3 people authored and apop5 committed Aug 23, 2024
1 parent 3dc1746 commit 413351c
Show file tree
Hide file tree
Showing 11 changed files with 620 additions and 211 deletions.
6 changes: 3 additions & 3 deletions MdePkg/Include/IndustryStandard/PciExpress21.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,9 @@ typedef struct {
UINT16 DpaControl;
UINT8 DpaPowerAllocationArray[1];
} PCI_EXPRESS_EXTENDED_CAPABILITIES_DYNAMIC_POWER_ALLOCATION;

#define PCI_EXPRESS_EXTENDED_CAPABILITY_DYNAMIC_POWER_ALLOCATION_GET_SUBSTATE_MAX(POWER) (UINT16)(((POWER->DpaCapability)&0x0000000F))

// MU_CHANGE Start - CodeQL Change - comparison-with-wider-type
#define PCI_EXPRESS_EXTENDED_CAPABILITY_DYNAMIC_POWER_ALLOCATION_GET_SUBSTATE_MAX(POWER) (UINT32)(((POWER->DpaCapability)&0x0000000F))
// MU_CHANGE End - CodeQL Change - comparison-with-wider-type
#define PCI_EXPRESS_EXTENDED_CAPABILITY_LATENCE_TOLERANCE_REPORTING_ID 0x0018
#define PCI_EXPRESS_EXTENDED_CAPABILITY_LATENCE_TOLERANCE_REPORTING_VER1 0x1

Expand Down
64 changes: 48 additions & 16 deletions MdePkg/Library/BaseLib/String.c
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,13 @@ StrDecimalToUintn (
)
{
UINTN Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (StrDecimalToUintnS (String, (CHAR16 **)NULL, &Result))) {
return MAX_UINTN;
Status = StrDecimalToUintnS (String, (CHAR16 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down Expand Up @@ -456,9 +460,13 @@ StrDecimalToUint64 (
)
{
UINT64 Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (StrDecimalToUint64S (String, (CHAR16 **)NULL, &Result))) {
return MAX_UINT64;
Status = StrDecimalToUint64S (String, (CHAR16 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down Expand Up @@ -506,9 +514,13 @@ StrHexToUintn (
)
{
UINTN Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (StrHexToUintnS (String, (CHAR16 **)NULL, &Result))) {
return MAX_UINTN;
Status = StrHexToUintnS (String, (CHAR16 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down Expand Up @@ -556,9 +568,13 @@ StrHexToUint64 (
)
{
UINT64 Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (StrHexToUint64S (String, (CHAR16 **)NULL, &Result))) {
return MAX_UINT64;
Status = StrHexToUint64S (String, (CHAR16 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down Expand Up @@ -1000,9 +1016,13 @@ AsciiStrDecimalToUintn (
)
{
UINTN Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (AsciiStrDecimalToUintnS (String, (CHAR8 **)NULL, &Result))) {
return MAX_UINTN;
Status = AsciiStrDecimalToUintnS (String, (CHAR8 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down Expand Up @@ -1045,9 +1065,13 @@ AsciiStrDecimalToUint64 (
)
{
UINT64 Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (AsciiStrDecimalToUint64S (String, (CHAR8 **)NULL, &Result))) {
return MAX_UINT64;
Status = AsciiStrDecimalToUint64S (String, (CHAR8 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down Expand Up @@ -1094,9 +1118,13 @@ AsciiStrHexToUintn (
)
{
UINTN Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (AsciiStrHexToUintnS (String, (CHAR8 **)NULL, &Result))) {
return MAX_UINTN;
Status = AsciiStrHexToUintnS (String, (CHAR8 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down Expand Up @@ -1143,9 +1171,13 @@ AsciiStrHexToUint64 (
)
{
UINT64 Result;
// MU_CHANGE Start - CodeQL Change
RETURN_STATUS Status;

if (RETURN_ERROR (AsciiStrHexToUint64S (String, (CHAR8 **)NULL, &Result))) {
return MAX_UINT64;
Status = AsciiStrHexToUint64S (String, (CHAR8 **)NULL, &Result);
if (Status == RETURN_INVALID_PARAMETER) {
Result = 0;
// MU_CHANGE End - CodeQL Change
}

return Result;
Expand Down
9 changes: 5 additions & 4 deletions MdePkg/Library/BasePeCoffLib/BasePeCoff.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ PeCoffLoaderGetPeHeader (
UINTN Size;
UINTN ReadSize;
UINT32 SectionHeaderOffset;
UINT32 Index;
UINTN Index; // MU_CHANGE - CodeQL Change - comparison-with-wider-type
UINT32 HeaderWithoutDataDir;
CHAR8 BufferData;
UINTN NumberOfSections;
Expand Down Expand Up @@ -1421,7 +1421,8 @@ PeCoffLoaderLoadImage (
return RETURN_LOAD_ERROR;
}

if (Section->SizeOfRawData > 0) {
// MU_CHANGE - CodeQL change - Assume Base is non-NULL if Size is non-zero
if ((Section->SizeOfRawData > 0) && (Base != NULL)) {
Status = ImageContext->ImageRead (
ImageContext->Handle,
Section->PointerToRawData - TeStrippedOffset,
Expand All @@ -1437,8 +1438,8 @@ PeCoffLoaderLoadImage (
//
// If raw size is less then virtual size, zero fill the remaining
//

if (Size < Section->Misc.VirtualSize) {
// MU_CHANGE - CodeQL change - Assume Base is non-NULL if Size is non-zero
if ((Size < Section->Misc.VirtualSize) && (Base != NULL)) {
ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);
}

Expand Down
9 changes: 8 additions & 1 deletion MdePkg/Library/PeiServicesLib/PeiServicesLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,14 @@ InternalPeiServicesInstallFvInfoPpi (
}

FvInfoPpiDescriptor = AllocatePool (sizeof (EFI_PEI_PPI_DESCRIPTOR));
ASSERT (FvInfoPpiDescriptor != NULL);
// MU_CHANGE Start - CodeQL Change - unguardednullreturndereference
if (FvInfoPpiDescriptor == NULL) {
ASSERT (FvInfoPpiDescriptor != NULL);
// Need to return here, FV may not be published, but we are out of resources anyway...
return;
}

// MU_CHANGE End - CodeQL Change - unguardednullreturndereference

FvInfoPpiDescriptor->Guid = PpiGuid;
FvInfoPpiDescriptor->Flags = EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST;
Expand Down
Loading

0 comments on commit 413351c

Please sign in to comment.