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-23 11:13:38 -07:00 committed by mergify[bot]
parent 72d0846c4c
commit af24f366a4
6 changed files with 27 additions and 27 deletions

View file

@ -539,7 +539,7 @@ UpdateTerminalContent (
IN BMM_FAKE_NV_DATA *BmmData
)
{
UINT16 Index;
UINTN Index;
BM_TERMINAL_CONTEXT *NewTerminalContext;
BM_MENU_ENTRY *NewMenuEntry;
@ -581,7 +581,7 @@ UpdateConsoleContent (
IN BMM_FAKE_NV_DATA *BmmData
)
{
UINT16 Index;
UINTN Index;
BM_CONSOLE_CONTEXT *NewConsoleContext;
BM_TERMINAL_CONTEXT *NewTerminalContext;
BM_MENU_ENTRY *NewMenuEntry;
@ -784,7 +784,7 @@ BootMaintRouteConfig (
BMM_FAKE_NV_DATA *OldBmmData;
BM_MENU_ENTRY *NewMenuEntry;
BM_LOAD_CONTEXT *NewLoadContext;
UINT16 Index;
UINTN Index;
BOOLEAN TerminalAttChange;
BMM_CALLBACK_DATA *Private;
UINTN Offset;
@ -1353,7 +1353,7 @@ DiscardChangeHandler (
IN BMM_FAKE_NV_DATA *CurrentFakeNVMap
)
{
UINT16 Index;
UINTN Index;
switch (Private->BmmPreviousPageId) {
case FORM_BOOT_CHG_ID:
@ -1411,7 +1411,7 @@ CleanUselessBeforeSubmit (
IN BMM_CALLBACK_DATA *Private
)
{
UINT16 Index;
UINTN Index;
if (Private->BmmPreviousPageId != FORM_BOOT_DEL_ID) {
for (Index = 0; Index < BootOptionMenu.MenuNumber; Index++) {
@ -1502,7 +1502,7 @@ InitializeBmmConfig (
{
BM_MENU_ENTRY *NewMenuEntry;
BM_LOAD_CONTEXT *NewLoadContext;
UINT16 Index;
UINTN Index;
ASSERT (CallbackData != NULL);
@ -1515,7 +1515,7 @@ InitializeBmmConfig (
NewLoadContext = (BM_LOAD_CONTEXT *)NewMenuEntry->VariableContext;
if (NewLoadContext->IsBootNext) {
CallbackData->BmmFakeNvData.BootNext = Index;
CallbackData->BmmFakeNvData.BootNext = (UINT32)Index;
break;
}
}

View file

@ -36,7 +36,7 @@ BmmCreateBootNextMenu (
{
BM_MENU_ENTRY *NewMenuEntry;
BM_LOAD_CONTEXT *NewLoadContext;
UINT16 Index;
UINTN Index;
VOID *OptionsOpCodeHandle;
UINT32 BootNextIndex;
@ -61,7 +61,7 @@ BmmCreateBootNextMenu (
EFI_IFR_TYPE_NUM_SIZE_32,
Index
);
BootNextIndex = Index;
BootNextIndex = (UINT32)Index;
} else {
HiiCreateOneOfOptionOpCode (
OptionsOpCodeHandle,

View file

@ -813,7 +813,7 @@ GetBootOrder (
)
{
BMM_FAKE_NV_DATA *BmmConfig;
UINT16 Index;
UINTN Index;
UINT16 OptionOrderIndex;
UINTN DeviceType;
BM_MENU_ENTRY *NewMenuEntry;
@ -860,8 +860,8 @@ GetDriverOrder (
)
{
BMM_FAKE_NV_DATA *BmmConfig;
UINT16 Index;
UINT16 OptionOrderIndex;
UINTN Index;
UINTN OptionOrderIndex;
UINTN DeviceType;
BM_MENU_ENTRY *NewMenuEntry;
BM_LOAD_CONTEXT *NewLoadContext;

View file

@ -1019,7 +1019,7 @@ GetConsoleInCheck (
IN BMM_CALLBACK_DATA *CallbackData
)
{
UINT16 Index;
UINTN Index;
BM_MENU_ENTRY *NewMenuEntry;
UINT8 *ConInCheck;
BM_CONSOLE_CONTEXT *NewConsoleContext;
@ -1057,7 +1057,7 @@ GetConsoleOutCheck (
IN BMM_CALLBACK_DATA *CallbackData
)
{
UINT16 Index;
UINTN Index;
BM_MENU_ENTRY *NewMenuEntry;
UINT8 *ConOutCheck;
BM_CONSOLE_CONTEXT *NewConsoleContext;
@ -1094,7 +1094,7 @@ GetConsoleErrCheck (
IN BMM_CALLBACK_DATA *CallbackData
)
{
UINT16 Index;
UINTN Index;
BM_MENU_ENTRY *NewMenuEntry;
UINT8 *ConErrCheck;
BM_CONSOLE_CONTEXT *NewConsoleContext;
@ -1134,7 +1134,7 @@ GetTerminalAttribute (
BMM_FAKE_NV_DATA *CurrentFakeNVMap;
BM_MENU_ENTRY *NewMenuEntry;
BM_TERMINAL_CONTEXT *NewTerminalContext;
UINT16 TerminalIndex;
UINTN TerminalIndex;
UINT8 AttributeIndex;
ASSERT (CallbackData != NULL);

View file

@ -194,7 +194,7 @@ UpdateConCOMPage (
)
{
BM_MENU_ENTRY *NewMenuEntry;
UINT16 Index;
UINTN Index;
CallbackData->BmmAskSaveOrNot = TRUE;
@ -230,7 +230,7 @@ UpdateBootDelPage (
{
BM_MENU_ENTRY *NewMenuEntry;
BM_LOAD_CONTEXT *NewLoadContext;
UINT16 Index;
UINTN Index;
CallbackData->BmmAskSaveOrNot = TRUE;
@ -285,7 +285,7 @@ UpdateDrvAddHandlePage (
)
{
BM_MENU_ENTRY *NewMenuEntry;
UINT16 Index;
UINTN Index;
CallbackData->BmmAskSaveOrNot = FALSE;
@ -321,7 +321,7 @@ UpdateDrvDelPage (
{
BM_MENU_ENTRY *NewMenuEntry;
BM_LOAD_CONTEXT *NewLoadContext;
UINT16 Index;
UINTN Index;
CallbackData->BmmAskSaveOrNot = TRUE;
@ -451,8 +451,8 @@ UpdateConsolePage (
BM_MENU_ENTRY *NewMenuEntry;
BM_CONSOLE_CONTEXT *NewConsoleContext;
BM_TERMINAL_CONTEXT *NewTerminalContext;
UINT16 Index;
UINT16 Index2;
UINTN Index;
UINTN Index2;
UINT8 CheckFlags;
UINT8 *ConsoleCheck;
EFI_QUESTION_ID QuestionIdBase;
@ -571,7 +571,7 @@ UpdateOrderPage (
)
{
BM_MENU_ENTRY *NewMenuEntry;
UINT16 Index;
UINTN Index;
UINT16 OptionIndex;
VOID *OptionsOpCodeHandle;
BOOLEAN BootOptionFound;

View file

@ -537,7 +537,7 @@ Var_UpdateBootNext (
BM_MENU_ENTRY *NewMenuEntry;
BM_LOAD_CONTEXT *NewLoadContext;
BMM_FAKE_NV_DATA *CurrentFakeNVMap;
UINT16 Index;
UINTN Index;
EFI_STATUS Status;
Status = EFI_SUCCESS;
@ -592,8 +592,8 @@ Var_UpdateBootOrder (
)
{
EFI_STATUS Status;
UINT16 Index;
UINT16 OrderIndex;
UINTN Index;
UINTN OrderIndex;
UINT16 *BootOrder;
UINTN BootOrderSize;
UINT16 OptionNumber;
@ -654,7 +654,7 @@ Var_UpdateDriverOrder (
)
{
EFI_STATUS Status;
UINT16 Index;
UINTN Index;
UINT16 *DriverOrderList;
UINT16 *NewDriverOrderList;
UINTN DriverOrderListSize;