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

View File

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