From bf0dc7d78701c93a2edeeadcb0daca58f4af91a6 Mon Sep 17 00:00:00 2001 From: Aaron Pop Date: Wed, 22 Oct 2025 11:33:02 -0700 Subject: [PATCH] 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 --- MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c | 2 +- MdeModulePkg/Bus/Pci/IdeBusPei/AtapiPeim.c | 2 +- MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressPassthru.c | 8 ++++---- .../Bus/Pci/NvmExpressPei/NvmExpressPeiPassThru.c | 6 +++--- MdeModulePkg/Bus/Pci/PciSioSerialDxe/SerialIo.c | 2 +- MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHci.c | 2 +- MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHci.c | 2 +- MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c | 2 +- MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c | 2 +- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c index 19d7b4930c..f475cadcb5 100644 --- a/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c +++ b/MdeModulePkg/Bus/Ata/AtaAtapiPassThru/IdeMode.c @@ -943,7 +943,7 @@ AtaPioDataInOut ( IN ATA_NONBLOCK_TASK *Task ) { - UINTN WordCount; + UINT64 WordCount; UINTN Increment; UINT16 *Buffer16; EFI_STATUS Status; diff --git a/MdeModulePkg/Bus/Pci/IdeBusPei/AtapiPeim.c b/MdeModulePkg/Bus/Pci/IdeBusPei/AtapiPeim.c index 7b390995b3..950ca0f098 100644 --- a/MdeModulePkg/Bus/Pci/IdeBusPei/AtapiPeim.c +++ b/MdeModulePkg/Bus/Pci/IdeBusPei/AtapiPeim.c @@ -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; diff --git a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressPassthru.c b/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressPassthru.c index f818e48fc1..9dde95b5ea 100644 --- a/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressPassthru.c +++ b/MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressPassthru.c @@ -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)) { diff --git a/MdeModulePkg/Bus/Pci/NvmExpressPei/NvmExpressPeiPassThru.c b/MdeModulePkg/Bus/Pci/NvmExpressPei/NvmExpressPeiPassThru.c index ac9328047f..b329dbe979 100644 --- a/MdeModulePkg/Bus/Pci/NvmExpressPei/NvmExpressPeiPassThru.c +++ b/MdeModulePkg/Bus/Pci/NvmExpressPei/NvmExpressPeiPassThru.c @@ -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; } diff --git a/MdeModulePkg/Bus/Pci/PciSioSerialDxe/SerialIo.c b/MdeModulePkg/Bus/Pci/PciSioSerialDxe/SerialIo.c index dcdaa6cf9b..fe8cb834b3 100644 --- a/MdeModulePkg/Bus/Pci/PciSioSerialDxe/SerialIo.c +++ b/MdeModulePkg/Bus/Pci/PciSioSerialDxe/SerialIo.c @@ -1234,7 +1234,7 @@ SerialRead ( ) { SERIAL_DEV *SerialDevice; - UINT32 Index; + UINTN Index; UINT8 *CharBuffer; UINTN Elapsed; EFI_STATUS Status; diff --git a/MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHci.c b/MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHci.c index 9e8a7f4e43..4b95028e4a 100644 --- a/MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHci.c +++ b/MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHci.c @@ -1476,7 +1476,7 @@ BuildAdmaDescTable ( EFI_PHYSICAL_ADDRESS Data; UINT64 DataLen; UINT64 Entries; - UINT32 Index; + UINT64 Index; UINT64 Remaining; UINT64 Address; UINTN TableSize; diff --git a/MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHci.c b/MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHci.c index bafd71e9b5..89df43c9dd 100644 --- a/MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHci.c +++ b/MdeModulePkg/Bus/Sd/EmmcBlockIoPei/EmmcHci.c @@ -934,7 +934,7 @@ BuildAdmaDescTable ( EFI_PHYSICAL_ADDRESS Data; UINT64 DataLen; UINT64 Entries; - UINT32 Index; + UINT64 Index; UINT64 Remaining; UINT32 Address; diff --git a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c b/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c index 805037e942..305eb8193d 100644 --- a/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c +++ b/MdeModulePkg/Bus/Ufs/UfsBlockIoPei/UfsHci.c @@ -317,7 +317,7 @@ UfsInitUtpPrdt ( IN UINT32 BufferSize ) { - UINT32 PrdtIndex; + UINTN PrdtIndex; UINT32 RemainingLen; UINT8 *Remaining; UINTN PrdtNumber; diff --git a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c index a67994000c..6b433f18e9 100644 --- a/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c +++ b/MdeModulePkg/Bus/Ufs/UfsPassThruDxe/UfsPassThruHci.c @@ -389,7 +389,7 @@ UfsInitUtpPrdt ( IN UINT32 BufferSize ) { - UINT32 PrdtIndex; + UINTN PrdtIndex; UINT32 RemainingLen; UINT8 *Remaining; UINTN PrdtNumber;