From af24f366a42503b3b5ebbb0bcb9742a6785b6833 Mon Sep 17 00:00:00 2001 From: Aaron Pop Date: Thu, 23 Oct 2025 11:13:38 -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 --- .../BootMaintenanceManagerUiLib/BootMaintenance.c | 14 +++++++------- .../BootMaintenanceManagerCustomizedUiSupport.c | 4 ++-- .../BootMaintenanceManagerUiLib/BootOption.c | 6 +++--- .../BootMaintenanceManagerUiLib/ConsoleOption.c | 8 ++++---- .../BootMaintenanceManagerUiLib/UpdatePage.c | 14 +++++++------- .../Library/BootMaintenanceManagerUiLib/Variable.c | 8 ++++---- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c index 19751642a3..773c978c9f 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenance.c @@ -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; } } diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c index e16936041a..bbc86dea18 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootMaintenanceManagerCustomizedUiSupport.c @@ -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, diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c index a47106d43b..73c985c169 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/BootOption.c @@ -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; diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c index 7c02a78166..cc84f46592 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/ConsoleOption.c @@ -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); diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c index b1d1e2ee44..84becdfd84 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/UpdatePage.c @@ -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; diff --git a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c index 82a0ed66a7..25158f99d6 100644 --- a/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c +++ b/MdeModulePkg/Library/BootMaintenanceManagerUiLib/Variable.c @@ -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;