]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/DxeServicesLib/DxeServicesLib.c
Make sure gBS FreePool() is used to free the buffer always allocated by gBS AllocateP...
[mirror_edk2.git] / MdePkg / Library / DxeServicesLib / DxeServicesLib.c
index d8e6b5cc4e404ccd86fa663d04fdeb9e860d0353..9cf845d6fc1bac276ea94401f66b2cf66ec8039b 100644 (file)
@@ -122,6 +122,7 @@ InternalGetSectionFromFv (
   EFI_STATUS                    Status;\r
   EFI_FIRMWARE_VOLUME2_PROTOCOL *Fv;\r
   UINT32                        AuthenticationStatus;\r
+  VOID*                         TempBuffer;\r
 \r
   ASSERT (NameGuid != NULL);\r
   ASSERT (Buffer != NULL);\r
@@ -170,6 +171,17 @@ InternalGetSectionFromFv (
                     );\r
   }\r
 \r
+  if (!EFI_ERROR (Status)) {\r
+    //\r
+    // The found buffer by FV protocol is allocated by gBS AllocatePool() service. \r
+    // Copy the found buffer to the allocated buffer by AllocatePool().\r
+    // So, the returned buffer can be freed by FreePool().\r
+    //\r
+    TempBuffer = AllocateCopyPool (*Size, *Buffer);\r
+    gBS->FreePool (*Buffer);\r
+    *Buffer = TempBuffer;\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
@@ -312,7 +324,11 @@ GetSectionFromAnyFvByFileType  (
 \r
 Done:\r
   if (HandleBuffer != NULL) {  \r
-    FreePool(HandleBuffer);\r
+    //\r
+    // HandleBuffer is allocated by gBS AllocatePool() service. \r
+    // So, gBS FreePool() service is used to free HandleBuffer.\r
+    //\r
+    gBS->FreePool (HandleBuffer);\r
   }\r
 \r
   return Status;\r
@@ -435,8 +451,12 @@ GetSectionFromAnyFv  (
 \r
 Done:\r
   \r
-  if (HandleBuffer != NULL) {  \r
-    FreePool(HandleBuffer);\r
+  if (HandleBuffer != NULL) {\r
+    //\r
+    // HandleBuffer is allocated by gBS AllocatePool() service. \r
+    // So, gBS FreePool() service is used to free HandleBuffer.\r
+    //\r
+    gBS->FreePool (HandleBuffer);\r
   }\r
   return Status;\r
   \r
@@ -615,6 +635,7 @@ GetFileBufferByFilePath (
   EFI_FIRMWARE_VOLUME2_PROTOCOL     *FwVol;\r
   EFI_SECTION_TYPE                  SectionType;\r
   UINT8                             *ImageBuffer;\r
+  UINT8                             *TempBuffer;\r
   UINTN                             ImageBufferSize;\r
   EFI_FV_FILETYPE                   Type;\r
   EFI_FV_FILE_ATTRIBUTES            Attrib;\r
@@ -642,6 +663,7 @@ GetFileBufferByFilePath (
   FileInfo            = NULL;\r
   FileHandle          = NULL;\r
   ImageBuffer         = NULL;\r
+  TempBuffer          = NULL;\r
   ImageBufferSize     = 0;\r
   *AuthenticationStatus = 0;\r
   \r
@@ -702,6 +724,16 @@ GetFileBufferByFilePath (
                             AuthenticationStatus\r
                             );\r
         }\r
+        if (!EFI_ERROR (Status)) {\r
+          //\r
+          // The found buffer by FV protocol is allocated by gBS AllocatePool() service. \r
+          // Copy the found buffer to the allocated buffer by AllocatePool().\r
+          // Then, this returned buffer can be freed by FreePool().\r
+          //\r
+          TempBuffer = AllocateCopyPool (ImageBufferSize, ImageBuffer);\r
+          gBS->FreePool (ImageBuffer);\r
+          ImageBuffer = TempBuffer;\r
+        }\r
       }\r
     }\r
     goto Finish;\r