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 <[email protected]>
Co-authored-by: kenlautner <[email protected]>

Signed-off-by: Aaron Pop <[email protected]>
  • Loading branch information
makubacki authored and mergify[bot] committed Nov 1, 2024
1 parent a9901a7 commit b7735a0
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 196 deletions.
4 changes: 1 addition & 3 deletions MdePkg/Include/IndustryStandard/PciExpress21.h
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,7 @@ 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))

#define PCI_EXPRESS_EXTENDED_CAPABILITY_DYNAMIC_POWER_ALLOCATION_GET_SUBSTATE_MAX(POWER) (UINT32)(((POWER->DpaCapability)&0x0000000F))
#define PCI_EXPRESS_EXTENDED_CAPABILITY_LATENCE_TOLERANCE_REPORTING_ID 0x0018
#define PCI_EXPRESS_EXTENDED_CAPABILITY_LATENCE_TOLERANCE_REPORTING_VER1 0x1

Expand Down
64 changes: 40 additions & 24 deletions MdePkg/Library/BaseLib/String.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,12 @@ StrDecimalToUintn (
IN CONST CHAR16 *String
)
{
UINTN Result;
UINTN Result;
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;
}

return Result;
Expand Down Expand Up @@ -455,10 +457,12 @@ StrDecimalToUint64 (
IN CONST CHAR16 *String
)
{
UINT64 Result;
UINT64 Result;
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;
}

return Result;
Expand Down Expand Up @@ -505,10 +509,12 @@ StrHexToUintn (
IN CONST CHAR16 *String
)
{
UINTN Result;
UINTN Result;
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;
}

return Result;
Expand Down Expand Up @@ -555,10 +561,12 @@ StrHexToUint64 (
IN CONST CHAR16 *String
)
{
UINT64 Result;
UINT64 Result;
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;
}

return Result;
Expand Down Expand Up @@ -999,10 +1007,12 @@ AsciiStrDecimalToUintn (
IN CONST CHAR8 *String
)
{
UINTN Result;
UINTN Result;
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;
}

return Result;
Expand Down Expand Up @@ -1044,10 +1054,12 @@ AsciiStrDecimalToUint64 (
IN CONST CHAR8 *String
)
{
UINT64 Result;
UINT64 Result;
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;
}

return Result;
Expand Down Expand Up @@ -1093,10 +1105,12 @@ AsciiStrHexToUintn (
IN CONST CHAR8 *String
)
{
UINTN Result;
UINTN Result;
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;
}

return Result;
Expand Down Expand Up @@ -1142,10 +1156,12 @@ AsciiStrHexToUint64 (
IN CONST CHAR8 *String
)
{
UINT64 Result;
UINT64 Result;
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;
}

return Result;
Expand Down
6 changes: 3 additions & 3 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;
UINT32 HeaderWithoutDataDir;
CHAR8 BufferData;
UINTN NumberOfSections;
Expand Down Expand Up @@ -1407,7 +1407,7 @@ PeCoffLoaderLoadImage (
return RETURN_LOAD_ERROR;
}

if (Section->SizeOfRawData > 0) {
if ((Section->SizeOfRawData > 0) && (Base != NULL)) {
Status = ImageContext->ImageRead (
ImageContext->Handle,
Section->PointerToRawData - TeStrippedOffset,
Expand All @@ -1424,7 +1424,7 @@ PeCoffLoaderLoadImage (
// If raw size is less then virtual size, zero fill the remaining
//

if (Size < Section->Misc.VirtualSize) {
if ((Size < Section->Misc.VirtualSize) && (Base != NULL)) {
ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);
}

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

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

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

0 comments on commit b7735a0

Please sign in to comment.