audk/MdeModulePkg/Core/Dxe
Nate DeSimone beafabdae4 MdeModulePkg: Fix memory leak in LocateHandleBuffer()
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4543
REF: https://uefi.org/specs/UEFI/2.10/07_Services_Boot_Services.html#efi-boot-services-locatehandlebuffer

CoreLocateHandleBuffer() can in certain cases, return an
error and not free an allocated buffer. This scenario
occurs if the first call to InternalCoreLocateHandle()
returns success and the second call returns an error.

On a successful return, LocateHandleBuffer() passes
ownership of the buffer to the caller. However, the UEFI
specification is not explicit about what the expected
ownership of this buffer is in the case of an error.
However, it is heavily implied by the code example given
in section 7.3.15 of v2.10 of the UEFI specificaton that
if LocateHandleBuffer() returns a non-successful status
code then the ownership of the buffer does NOT transfer
to the caller. This code example explicitly refrains from
calling FreePool() if LocateHandleBuffer() returns an
error.

From a practical standpoint, it is logical to assume that
a non-successful status code indicates that no buffer of
handles was ever allocated. Indeed, in most error cases,
LocateHandleBuffer() does not go far enough to get to the
point where a buffer is allocated. Therefore, all existing
users of this API must already be coded to support the case
of a non-successful status code resulting in an invalid
handle buffer being returned. Therefore, this change will
not cause any backwards compatibility issues with existing
code.

In conclusion, this boils down to a fix for a memory leak
that also brings the behavior of our LocateHandleBuffer()
implementation into alignment with the original intentions
of the UEFI specification authors.

Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Signed-off-by: Nate DeSimone <nathaniel.l.desimone@intel.com>
2023-08-31 19:35:19 +00:00
..
Dispatcher MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
DxeMain MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
Event MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
FwVol MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
FwVolBlock MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
Gcd MdeModulePkg: Update Dxe to handle unaccepted memory type 2022-11-01 10:08:10 +00:00
Hand MdeModulePkg: Fix memory leak in LocateHandleBuffer() 2023-08-31 19:35:19 +00:00
Image MdeModulePkg: Enable forward edge CFI in mem attributes table 2023-04-07 13:18:38 +00:00
Library MdeModulePkg: Apply uncrustify changes 2021-12-07 17:24:28 +00:00
Mem MdeModulePkg: HeapGuard: Don't Assume Pool Head Allocated In First Page 2023-08-19 03:18:50 +00:00
Misc MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
SectionExtraction MdeModulePkg: Update code to be more C11 compliant by using __func__ 2023-04-10 14:19:57 +00:00
DxeCore.uni MdeModulePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:08 -07:00
DxeCoreExtra.uni MdeModulePkg: Replace BSD License with BSD+Patent License 2019-04-09 10:58:08 -07:00
DxeMain.h MdeModulePkg: Enable forward edge CFI in mem attributes table 2023-04-07 13:18:38 +00:00
DxeMain.inf MdeModulePkg: Notify BeforeExitBootServices in CoreExitBootServices 2023-01-12 16:03:30 +00:00