MdeModulePkg: DxeCore: Adding check for underflow before subtraction

During DXE core memory service initialization, the system would check
available resource descriptor hobs against the memory top from PHIT hob.
However, it is possible that a given resource descriptor hob will not be
larger than the cover the memory top, causing the Length calculation to
underflow.

This change adds a check for potential underflow before performing the
subtraction.

Signed-off-by: Kun Qin <kun.qin@microsoft.com>
This commit is contained in:
Kun Qin 2025-10-30 11:42:53 -07:00 committed by mergify[bot]
parent 5be1bccd9c
commit feeb137d43

View file

@ -2430,8 +2430,14 @@ CoreInitializeMemoryServices (
//
Attributes = PhitResourceHob->ResourceAttribute;
BaseAddress = PageAlignAddress (PhitHob->EfiMemoryTop);
Length = PageAlignLength (ResourceHobMemoryTop - BaseAddress);
FindLargestFreeRegion (&BaseAddress, &Length, (EFI_HOB_MEMORY_ALLOCATION *)GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION));
if (BaseAddress > ResourceHobMemoryTop) {
Length = 0;
} else {
Length = PageAlignLength (ResourceHobMemoryTop - BaseAddress);
FindLargestFreeRegion (&BaseAddress, &Length, (EFI_HOB_MEMORY_ALLOCATION *)GetFirstHob (EFI_HOB_TYPE_MEMORY_ALLOCATION));
}
if (Length < MinimalMemorySizeNeeded) {
//
// If that range is not large enough to intialize the DXE Core, then