MdeModulePkg: Fix comparison with wider widths

https://codeql.github.com/codeql-query-help/cpp/cpp-comparison-with-wider-type

If the narrow type (smaller range) is compared against a wide type
(larger range), the narrow value may overflow before reaching the wide
value. This can cause unexpected behavior, such as:

Infinite loops (loop condition never becomes false).
Incorrect logic (comparison results are misleading).

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
This commit is contained in:
Aaron Pop 2025-10-22 11:33:02 -07:00 committed by mergify[bot]
parent fa41c179db
commit bf0dc7d787
9 changed files with 14 additions and 14 deletions

View file

@ -943,7 +943,7 @@ AtaPioDataInOut (
IN ATA_NONBLOCK_TASK *Task
)
{
UINTN WordCount;
UINT64 WordCount;
UINTN Increment;
UINT16 *Buffer16;
EFI_STATUS Status;

View file

@ -517,7 +517,7 @@ AtapiEnumerateDevices (
IN ATAPI_BLK_IO_DEV *AtapiBlkIoDev
)
{
UINT8 Index1;
UINT32 Index1;
UINT8 Index2;
UINTN DevicePosition;
EFI_PEI_BLOCK_IO_MEDIA MediaInfo;

View file

@ -219,10 +219,10 @@ NvmeCreatePrpList (
OUT VOID **Mapping
)
{
UINTN PrpEntryNo;
UINT64 PrpEntryNo;
UINT64 PrpListBase;
UINTN PrpListIndex;
UINTN PrpEntryIndex;
UINT64 PrpListIndex;
UINT64 PrpEntryIndex;
UINT64 Remainder;
EFI_PHYSICAL_ADDRESS PrpListPhyAddr;
UINTN Bytes;
@ -236,7 +236,7 @@ NvmeCreatePrpList (
//
// Calculate total PrpList number.
//
*PrpListNo = (UINTN)DivU64x64Remainder ((UINT64)Pages, (UINT64)PrpEntryNo - 1, &Remainder);
*PrpListNo = (UINTN)DivU64x64Remainder ((UINT64)Pages, PrpEntryNo - 1, &Remainder);
if (*PrpListNo == 0) {
*PrpListNo = 1;
} else if ((Remainder != 0) && (Remainder != 1)) {

View file

@ -27,12 +27,12 @@ NvmeCreatePrpList (
IN UINTN Pages
)
{
UINTN PrpEntryNo;
UINT64 PrpEntryNo;
UINTN PrpListNo;
UINT64 PrpListBase;
VOID *PrpListHost;
UINTN PrpListIndex;
UINTN PrpEntryIndex;
UINT64 PrpEntryIndex;
UINT64 Remainder;
EFI_PHYSICAL_ADDRESS PrpListPhyAddr;
UINTN Bytes;
@ -47,7 +47,7 @@ NvmeCreatePrpList (
//
// Calculate total PrpList number.
//
PrpListNo = (UINTN)DivU64x64Remainder ((UINT64)Pages, (UINT64)PrpEntryNo, &Remainder);
PrpListNo = (UINTN)DivU64x64Remainder ((UINT64)Pages, PrpEntryNo, &Remainder);
if (Remainder != 0) {
PrpListNo += 1;
}

View file

@ -1234,7 +1234,7 @@ SerialRead (
)
{
SERIAL_DEV *SerialDevice;
UINT32 Index;
UINTN Index;
UINT8 *CharBuffer;
UINTN Elapsed;
EFI_STATUS Status;

View file

@ -1476,7 +1476,7 @@ BuildAdmaDescTable (
EFI_PHYSICAL_ADDRESS Data;
UINT64 DataLen;
UINT64 Entries;
UINT32 Index;
UINT64 Index;
UINT64 Remaining;
UINT64 Address;
UINTN TableSize;

View file

@ -934,7 +934,7 @@ BuildAdmaDescTable (
EFI_PHYSICAL_ADDRESS Data;
UINT64 DataLen;
UINT64 Entries;
UINT32 Index;
UINT64 Index;
UINT64 Remaining;
UINT32 Address;

View file

@ -317,7 +317,7 @@ UfsInitUtpPrdt (
IN UINT32 BufferSize
)
{
UINT32 PrdtIndex;
UINTN PrdtIndex;
UINT32 RemainingLen;
UINT8 *Remaining;
UINTN PrdtNumber;

View file

@ -389,7 +389,7 @@ UfsInitUtpPrdt (
IN UINT32 BufferSize
)
{
UINT32 PrdtIndex;
UINTN PrdtIndex;
UINT32 RemainingLen;
UINT8 *Remaining;
UINTN PrdtNumber;