MdePkg/UefiDevicePathLib: Fix the potential memory leak issue

The function DevPathToTextUsbWWID() allocates NewStr when the input
SerialNumber lacks a null terminator. However, this allocated memory
is never freed after use, resulting in a memory leak.

This patch adds the missing FreePool() for NewStr before the function
returns, ensuring that the allocated buffer is properly freed and
eliminating the potential leak.

Signed-off-by: Qihang Gao <gaoqihang@loongson.cn>
This commit is contained in:
Qihang Gao 2026-06-25 17:07:51 +08:00 committed by mergify[bot]
parent c70637de12
commit fa461abf8a

View file

@ -996,6 +996,7 @@ DevPathToTextUsbWWID (
UINT16 Length;
UsbWWId = DevPath;
NewStr = NULL;
SerialNumberStr = (CHAR16 *)((UINT8 *)UsbWWId + sizeof (USB_WWID_DEVICE_PATH));
Length = (UINT16)((DevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *)UsbWWId) - sizeof (USB_WWID_DEVICE_PATH)) / sizeof (CHAR16));
@ -1018,6 +1019,10 @@ DevPathToTextUsbWWID (
UsbWWId->InterfaceNumber,
SerialNumberStr
);
if (NewStr != NULL) {
FreePool (NewStr);
}
}
/**