From 98a601b177b5de6dcb7c5007cd292d87372e0f8c Mon Sep 17 00:00:00 2001 From: qhuang8 Date: Wed, 11 Mar 2009 13:59:22 +0000 Subject: [PATCH] Add comments and refine code to avoid addition overflow. git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@7863 6f19259b-4bc3-4df7-8a09-765794883524 --- MdeModulePkg/Core/Pei/Memory/MemoryServices.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c index 3e424ad1a5..22e04cd46a 100644 --- a/MdeModulePkg/Core/Pei/Memory/MemoryServices.c +++ b/MdeModulePkg/Core/Pei/Memory/MemoryServices.c @@ -161,7 +161,11 @@ PeiAllocatePages ( // Verify that there is sufficient memory to satisfy the allocation // RemainingPages = EFI_SIZE_TO_PAGES ((UINTN) (*FreeMemoryTop - *FreeMemoryBottom)); - if ((INTN) (RemainingPages - EFI_SIZE_TO_PAGES (sizeof (EFI_HOB_MEMORY_ALLOCATION))) < Pages) { + // + // For page allocation, the overhead sizeof (EFI_HOB_MEMORY_ALLOCATION) needs one extra page. + // So the number of remaining pages needs to be greater than that of the request pages. + // + if (RemainingPages <= Pages) { DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%lx Pages is available.\n", (UINT64) Pages)); DEBUG ((EFI_D_ERROR, "There is only left 0x%lx pages memory resource to be allocated.\n", (UINT64) RemainingPages)); return EFI_OUT_OF_RESOURCES;