ShellPkg: Corrected CatSPrint usage to prevent memory leaks.

CatSPrint allocates return buffer for the caller. The caller doesn't have to allocate one, and has to free the used buffers.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Cecil Sheng <cecil.sheng@hpe.com>
Reviewed-by: Qiu Shumin <shumin.qiu@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18902 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Cecil Sheng 2015-11-19 08:37:03 +00:00 committed by shenshushi
parent 55df704dd2
commit 1a6f74d988
1 changed files with 120 additions and 108 deletions

View File

@ -1,6 +1,7 @@
/** @file
Provides interface to advanced shell functionality for parsing both handle and protocol database.
(C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
(C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
@ -180,10 +181,7 @@ LoadedImageProtocolDumpInformation(
HandleParsingHiiInit();
Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_LI_DUMP_MAIN), NULL);
RetVal = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
if (Temp == NULL || RetVal == NULL) {
SHELL_FREE_NON_NULL(Temp);
SHELL_FREE_NON_NULL(RetVal);
if (Temp == NULL) {
return NULL;
}
@ -198,27 +196,28 @@ LoadedImageProtocolDumpInformation(
if (EFI_ERROR (Status)) {
SHELL_FREE_NON_NULL (Temp);
SHELL_FREE_NON_NULL (RetVal);
return NULL;
}
DataType = ConvertMemoryType(LoadedImage->ImageDataType);
CodeType = ConvertMemoryType(LoadedImage->ImageCodeType);
RetVal = CatSPrint(RetVal,
Temp,
LoadedImage->Revision,
LoadedImage->ParentHandle,
LoadedImage->SystemTable,
LoadedImage->DeviceHandle,
LoadedImage->FilePath,
LoadedImage->LoadOptionsSize,
LoadedImage->LoadOptions,
LoadedImage->ImageBase,
LoadedImage->ImageSize,
CodeType,
DataType,
LoadedImage->Unload);
RetVal = CatSPrint(
NULL,
Temp,
LoadedImage->Revision,
LoadedImage->ParentHandle,
LoadedImage->SystemTable,
LoadedImage->DeviceHandle,
LoadedImage->FilePath,
LoadedImage->LoadOptionsSize,
LoadedImage->LoadOptions,
LoadedImage->ImageBase,
LoadedImage->ImageSize,
CodeType,
DataType,
LoadedImage->Unload
);
SHELL_FREE_NON_NULL(Temp);
@ -258,10 +257,7 @@ GraphicsOutputProtocolDumpInformation(
HandleParsingHiiInit();
Temp = HiiGetString(mHandleParsingHiiHandle, STRING_TOKEN(STR_GOP_DUMP_MAIN), NULL);
RetVal = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
if (Temp == NULL || RetVal == NULL) {
SHELL_FREE_NON_NULL(Temp);
SHELL_FREE_NON_NULL(RetVal);
if (Temp == NULL) {
return NULL;
}
@ -276,28 +272,28 @@ GraphicsOutputProtocolDumpInformation(
if (EFI_ERROR (Status)) {
SHELL_FREE_NON_NULL (Temp);
SHELL_FREE_NON_NULL (RetVal);
return NULL;
}
Fmt = ConvertPixelFormat(GraphicsOutput->Mode->Info->PixelFormat);
RetVal = CatSPrint(RetVal,
Temp,
GraphicsOutput->Mode->MaxMode,
GraphicsOutput->Mode->Mode,
GraphicsOutput->Mode->FrameBufferBase,
(UINT64)GraphicsOutput->Mode->FrameBufferSize,
(UINT64)GraphicsOutput->Mode->SizeOfInfo,
GraphicsOutput->Mode->Info->Version,
GraphicsOutput->Mode->Info->HorizontalResolution,
GraphicsOutput->Mode->Info->VerticalResolution,
Fmt,
GraphicsOutput->Mode->Info->PixelsPerScanLine,
GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.RedMask,
GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.GreenMask,
GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.BlueMask
);
RetVal = CatSPrint(
NULL,
Temp,
GraphicsOutput->Mode->MaxMode,
GraphicsOutput->Mode->Mode,
GraphicsOutput->Mode->FrameBufferBase,
(UINT64)GraphicsOutput->Mode->FrameBufferSize,
(UINT64)GraphicsOutput->Mode->SizeOfInfo,
GraphicsOutput->Mode->Info->Version,
GraphicsOutput->Mode->Info->HorizontalResolution,
GraphicsOutput->Mode->Info->VerticalResolution,
Fmt,
GraphicsOutput->Mode->Info->PixelsPerScanLine,
GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.RedMask,
GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.GreenMask,
GraphicsOutput->Mode->Info->PixelFormat!=PixelBitMask?0:GraphicsOutput->Mode->Info->PixelInformation.BlueMask
);
SHELL_FREE_NON_NULL(Temp);
SHELL_FREE_NON_NULL(Fmt);
@ -618,6 +614,7 @@ AdapterInformationDumpInformation (
CHAR16 *GuidStr;
CHAR16 *TempStr;
CHAR16 *RetVal;
CHAR16 *TempRetVal;
VOID *InformationBlock;
UINTN InformationBlockSize;
@ -628,13 +625,6 @@ AdapterInformationDumpInformation (
InfoTypesBuffer = NULL;
InformationBlock = NULL;
//
// Allocate print buffer to store data
//
RetVal = AllocateZeroPool (PcdGet16(PcdShellPrintBufferSize));
if (RetVal == NULL) {
return NULL;
}
Status = gBS->OpenProtocol (
(EFI_HANDLE) (TheHandle),
@ -646,7 +636,6 @@ AdapterInformationDumpInformation (
);
if (EFI_ERROR (Status)) {
SHELL_FREE_NON_NULL (RetVal);
return NULL;
}
@ -658,10 +647,11 @@ AdapterInformationDumpInformation (
&InfoTypesBuffer,
&InfoTypesBufferCount
);
RetVal = NULL;
if (EFI_ERROR (Status)) {
TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_GET_SUPP_TYPES_FAILED), NULL);
if (TempStr != NULL) {
RetVal = CatSPrint (RetVal, TempStr, Status);
RetVal = CatSPrint (NULL, TempStr, Status);
} else {
goto ERROR_EXIT;
}
@ -670,7 +660,7 @@ AdapterInformationDumpInformation (
if (TempStr == NULL) {
goto ERROR_EXIT;
}
RetVal = CatSPrint (RetVal, TempStr);
RetVal = CatSPrint (NULL, TempStr);
SHELL_FREE_NON_NULL (TempStr);
for (GuidIndex = 0; GuidIndex < InfoTypesBufferCount; GuidIndex++) {
@ -678,7 +668,9 @@ AdapterInformationDumpInformation (
if (TempStr == NULL) {
goto ERROR_EXIT;
}
RetVal = CatSPrint (RetVal, TempStr, (GuidIndex + 1), InfoTypesBuffer[GuidIndex]);
TempRetVal = CatSPrint (RetVal, TempStr, (GuidIndex + 1), InfoTypesBuffer[GuidIndex]);
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
SHELL_FREE_NON_NULL (TempStr);
TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_GUID_STRING), NULL);
@ -687,18 +679,26 @@ AdapterInformationDumpInformation (
}
if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoMediaStateGuid)) {
RetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoMediaStateGuid");
TempRetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoMediaStateGuid");
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
} else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoNetworkBootGuid)) {
RetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoNetworkBootGuid");
TempRetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoNetworkBootGuid");
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
} else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoSanMacAddressGuid)) {
RetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoSanMacAddressGuid");
TempRetVal = CatSPrint (RetVal, TempStr, L"gEfiAdapterInfoSanMacAddressGuid");
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
} else {
GuidStr = GetStringNameFromGuid (&InfoTypesBuffer[GuidIndex], NULL);
if (GuidStr != NULL) {
if (StrCmp(GuidStr, L"UnknownDevice") == 0) {
RetVal = CatSPrint (RetVal, TempStr, L"UnknownInfoType");
TempRetVal = CatSPrint (RetVal, TempStr, L"UnknownInfoType");
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
SHELL_FREE_NON_NULL (TempStr);
SHELL_FREE_NON_NULL(GuidStr);
@ -707,7 +707,9 @@ AdapterInformationDumpInformation (
//
continue;
} else {
RetVal = CatSPrint (RetVal, TempStr, GuidStr);
TempRetVal = CatSPrint (RetVal, TempStr, GuidStr);
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
SHELL_FREE_NON_NULL(GuidStr);
}
}
@ -727,57 +729,67 @@ AdapterInformationDumpInformation (
if (TempStr == NULL) {
goto ERROR_EXIT;
}
RetVal = CatSPrint (RetVal, TempStr, Status);
TempRetVal = CatSPrint (RetVal, TempStr, Status);
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
} else {
if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoMediaStateGuid)) {
TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_MEDIA_STATE), NULL);
if (TempStr == NULL) {
goto ERROR_EXIT;
}
RetVal = CatSPrint (
RetVal,
TempStr,
((EFI_ADAPTER_INFO_MEDIA_STATE *)InformationBlock)->MediaState,
((EFI_ADAPTER_INFO_MEDIA_STATE *)InformationBlock)->MediaState
);
TempRetVal = CatSPrint (
RetVal,
TempStr,
((EFI_ADAPTER_INFO_MEDIA_STATE *)InformationBlock)->MediaState,
((EFI_ADAPTER_INFO_MEDIA_STATE *)InformationBlock)->MediaState
);
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
} else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoNetworkBootGuid)) {
TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_NETWORK_BOOT_INFO), NULL);
if (TempStr == NULL) {
goto ERROR_EXIT;
}
RetVal = CatSPrint (
RetVal,
TempStr,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv4BootCapablity,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv6BootCapablity,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->FCoeBootCapablity,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->OffloadCapability,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiMpioCapability,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv4Boot,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv6Boot,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->FCoeBoot
);
TempRetVal = CatSPrint (
RetVal,
TempStr,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv4BootCapablity,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv6BootCapablity,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->FCoeBootCapablity,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->OffloadCapability,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiMpioCapability,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv4Boot,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->iScsiIpv6Boot,
((EFI_ADAPTER_INFO_NETWORK_BOOT *)InformationBlock)->FCoeBoot
);
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
} else if (CompareGuid (&InfoTypesBuffer[GuidIndex], &gEfiAdapterInfoSanMacAddressGuid) == TRUE) {
TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_SAN_MAC_ADDRESS_INFO), NULL);
if (TempStr == NULL) {
goto ERROR_EXIT;
}
RetVal = CatSPrint (
RetVal,
TempStr,
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[0],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[1],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[2],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[3],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[4],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[5]
);
TempRetVal = CatSPrint (
RetVal,
TempStr,
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[0],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[1],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[2],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[3],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[4],
((EFI_ADAPTER_INFO_SAN_MAC_ADDRESS *)InformationBlock)->SanMacAddress.Addr[5]
);
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
} else {
TempStr = HiiGetString (mHandleParsingHiiHandle, STRING_TOKEN(STR_UNKNOWN_INFO_TYPE), NULL);
if (TempStr == NULL) {
goto ERROR_EXIT;
}
RetVal = CatSPrint (RetVal, TempStr, &InfoTypesBuffer[GuidIndex]);
TempRetVal = CatSPrint (RetVal, TempStr, &InfoTypesBuffer[GuidIndex]);
SHELL_FREE_NON_NULL (RetVal);
RetVal = TempRetVal;
}
}
SHELL_FREE_NON_NULL (TempStr);