StandaloneMmPkg/Core: Fix memory leak in MmiHandlerRegister

In MmiHandlerRegister(), the MmiHandler structure is currently
allocated before looking up the target MmiEntry. If the lookup
fails, the function returns an error but the allocated MmiHandler
is never freed, causing a memory leak.

This patch moves the allocation of MmiHandler and its initialization
to after the MmiEntry lookup and validation. This ensures that memory
is only allocated when the operation can succeed, eliminating the need
for a FreePool() on the error path and simplifying the error handling
logic.

Signed-off-by: Qihang Gao <gaoqihang@loongson.cn>
Suggested-by: Ray Ni <ray.ni@intel.com>
This commit is contained in:
Qihang Gao 2026-07-24 10:55:15 +08:00 committed by mergify[bot]
parent f246d215ea
commit 8d2bbdfb14

View file

@ -367,15 +367,6 @@ MmiHandlerRegister (
return EFI_INVALID_PARAMETER;
}
MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
if (MmiHandler == NULL) {
return EFI_OUT_OF_RESOURCES;
}
MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
MmiHandler->Handler = Handler;
MmiHandler->ToRemove = FALSE;
if (HandlerType == NULL) {
//
// This is root MMI handler
@ -394,7 +385,15 @@ MmiHandlerRegister (
List = &MmiEntry->MmiHandlers;
}
MmiHandler->MmiEntry = MmiEntry;
MmiHandler = AllocateZeroPool (sizeof (MMI_HANDLER));
if (MmiHandler == NULL) {
return EFI_OUT_OF_RESOURCES;
}
MmiHandler->Signature = MMI_HANDLER_SIGNATURE;
MmiHandler->Handler = Handler;
MmiHandler->ToRemove = FALSE;
MmiHandler->MmiEntry = MmiEntry;
InsertTailList (List, &MmiHandler->Link);
*DispatchHandle = (EFI_HANDLE)MmiHandler;