MdeModulePkg: Fix missing NULL tests

https://github.com/github/codeql/blob/codeql-cli-2.7.3/cpp/ql/src/Critical/MissingNullTest.qhelp

For items which allocate memory, or get a pointer from another
structure, it is important to validate that the pointers
are not null before they are dereferenced.

Signed-off-by: Aaron Pop <aaronpop@microsoft.com>
This commit is contained in:
Aaron Pop 2025-10-23 12:57:49 -07:00 committed by mergify[bot]
parent af24f366a4
commit 1cc0af9d6d
6 changed files with 55 additions and 11 deletions

View file

@ -1451,10 +1451,17 @@ CustomizeMenus (
// Allocate space for creation of UpdateData Buffer
//
StartOpCodeHandle = HiiAllocateOpCodeHandle ();
ASSERT (StartOpCodeHandle != NULL);
if (StartOpCodeHandle == NULL) {
ASSERT (StartOpCodeHandle != NULL);
return;
}
EndOpCodeHandle = HiiAllocateOpCodeHandle ();
ASSERT (EndOpCodeHandle != NULL);
if (EndOpCodeHandle == NULL) {
ASSERT (EndOpCodeHandle != NULL);
goto Exit;
}
//
// Create Hii Extend Label OpCode as the start opcode
//
@ -1485,6 +1492,7 @@ CustomizeMenus (
);
HiiFreeOpCodeHandle (StartOpCodeHandle);
Exit:
HiiFreeOpCodeHandle (EndOpCodeHandle);
}

View file

@ -378,10 +378,18 @@ BmmListThirdPartyDrivers (
}
HiiHandles = HiiGetHiiHandles (NULL);
ASSERT (HiiHandles != NULL);
if (HiiHandles == NULL) {
ASSERT (HiiHandles != NULL);
return EFI_OUT_OF_RESOURCES;
}
gHiiDriverList = AllocateZeroPool (UI_HII_DRIVER_LIST_SIZE * sizeof (UI_HII_DRIVER_INSTANCE));
ASSERT (gHiiDriverList != NULL);
if (gHiiDriverList == NULL) {
ASSERT (gHiiDriverList != NULL);
FreePool (HiiHandles);
return EFI_OUT_OF_RESOURCES;
}
DriverListPtr = gHiiDriverList;
CurrentSize = UI_HII_DRIVER_LIST_SIZE;

View file

@ -340,6 +340,10 @@ BOpt_GetBootOptions (
BootOption = EfiBootManagerGetLoadOptions (&BootOptionCount, LoadOptionTypeBoot);
for (Index = 0; Index < BootOrderListSize / sizeof (UINT16); Index++) {
if (BootOption == NULL) {
continue;
}
//
// Don't display the hidden/inactive boot option
//
@ -363,7 +367,10 @@ BOpt_GetBootOptions (
}
NewMenuEntry = BOpt_CreateMenuEntry (BM_LOAD_CONTEXT_SELECT);
ASSERT (NULL != NewMenuEntry);
if (NewMenuEntry == NULL) {
ASSERT (NULL != NewMenuEntry);
return EFI_OUT_OF_RESOURCES;
}
NewLoadContext = (BM_LOAD_CONTEXT *)NewMenuEntry->VariableContext;

View file

@ -803,6 +803,10 @@ GetConsoleMenu (
Index2 = 0;
for (Index = 0; Index < AllCount; Index++) {
DevicePathInst = GetNextDevicePathInstance (&MultiDevicePath, &Size);
if (DevicePathInst == NULL) {
ASSERT (DevicePathInst != NULL);
continue;
}
NewMenuEntry = BOpt_CreateMenuEntry (BM_CONSOLE_CONTEXT_SELECT);
if (NULL == NewMenuEntry) {
@ -813,7 +817,11 @@ GetConsoleMenu (
NewMenuEntry->OptionNumber = Index2;
NewConsoleContext->DevicePath = DuplicateDevicePath (DevicePathInst);
ASSERT (NewConsoleContext->DevicePath != NULL);
if (NewConsoleContext->DevicePath == NULL) {
ASSERT (NewConsoleContext->DevicePath != NULL);
return EFI_OUT_OF_RESOURCES;
}
NewMenuEntry->DisplayString = EfiLibStrFromDatahub (NewConsoleContext->DevicePath);
if (NULL == NewMenuEntry->DisplayString) {
NewMenuEntry->DisplayString = UiDevicePathToStr (NewConsoleContext->DevicePath);

View file

@ -486,7 +486,10 @@ UpdateConsolePage (
break;
}
ASSERT (ConsoleCheck != NULL);
if (ConsoleCheck == NULL) {
ASSERT (ConsoleCheck != NULL);
return;
}
for (Index = 0; ((Index < ConsoleMenu->MenuNumber) && \
(Index < MAX_MENU_NUMBER)); Index++)
@ -619,10 +622,16 @@ UpdateOrderPage (
break;
}
ASSERT (OptionOrder != NULL);
if (OptionOrder == NULL ) {
ASSERT (OptionOrder != NULL);
return;
}
OptionsOpCodeHandle = HiiAllocateOpCodeHandle ();
ASSERT (OptionsOpCodeHandle != NULL);
if (OptionsOpCodeHandle == NULL) {
ASSERT (OptionsOpCodeHandle != NULL);
return;
}
NewMenuEntry = NULL;
for (OptionIndex = 0; (OptionIndex < MAX_MENU_NUMBER && OptionOrder[OptionIndex] != 0); OptionIndex++) {
@ -635,7 +644,7 @@ UpdateOrderPage (
}
}
if (BootOptionFound) {
if (BootOptionFound && (NewMenuEntry != NULL)) {
HiiCreateOneOfOptionOpCode (
OptionsOpCodeHandle,
NewMenuEntry->DisplayStringToken,

View file

@ -197,7 +197,11 @@ Var_UpdateConsoleOption (
NewTerminalContext->DevicePath,
(EFI_DEVICE_PATH_PROTOCOL *)&Vendor
);
ASSERT (TerminalDevicePath != NULL);
if (TerminalDevicePath == NULL) {
ASSERT (TerminalDevicePath != NULL);
return EFI_OUT_OF_RESOURCES;
}
ChangeTerminalDevicePath (TerminalDevicePath, TRUE);
ConDevicePath = AppendDevicePathInstance (
ConDevicePath,