BaseTools/EfiRom: Fix potential memory leak

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
This commit is contained in:
Hao Wu 2016-10-11 13:56:20 +08:00
parent aee346514d
commit fb4ea38c46
1 changed files with 51 additions and 19 deletions

View File

@ -883,9 +883,11 @@ Returns:
UINT32 ClassCode; UINT32 ClassCode;
UINT32 CodeRevision; UINT32 CodeRevision;
EFI_STATUS Status; EFI_STATUS Status;
INTN ReturnStatus;
BOOLEAN EfiRomFlag; BOOLEAN EfiRomFlag;
UINT64 TempValue; UINT64 TempValue;
ReturnStatus = 0;
FileFlags = 0; FileFlags = 0;
EfiRomFlag = FALSE; EfiRomFlag = FALSE;
@ -940,11 +942,13 @@ Returns:
Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue); Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
if (TempValue >= 0x10000) { if (TempValue >= 0x10000) {
Error (NULL, 0, 2000, "Invalid option value", "Vendor Id %s out of range!", Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "Vendor Id %s out of range!", Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
Options->VendId = (UINT16) TempValue; Options->VendId = (UINT16) TempValue;
Options->VendIdValid = 1; Options->VendIdValid = 1;
@ -959,11 +963,13 @@ Returns:
Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue); Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
if (TempValue >= 0x10000) { if (TempValue >= 0x10000) {
Error (NULL, 0, 2000, "Invalid option value", "Device Id %s out of range!", Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "Device Id %s out of range!", Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
Options->DevId = (UINT16) TempValue; Options->DevId = (UINT16) TempValue;
Options->DevIdValid = 1; Options->DevIdValid = 1;
@ -977,11 +983,13 @@ Returns:
// //
if (Argv[1] == NULL || Argv[1][0] == '-') { if (Argv[1] == NULL || Argv[1][0] == '-') {
Error (NULL, 0, 2000, "Invalid parameter", "Missing output file name with %s option!", Argv[0]); Error (NULL, 0, 2000, "Invalid parameter", "Missing output file name with %s option!", Argv[0]);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
if (strlen (Argv[1]) > MAX_PATH - 1) { if (strlen (Argv[1]) > MAX_PATH - 1) {
Error (NULL, 0, 2000, "Invalid parameter", "Output file name %s is too long!", Argv[1]); Error (NULL, 0, 2000, "Invalid parameter", "Output file name %s is too long!", Argv[1]);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
strncpy (Options->OutFileName, Argv[1], MAX_PATH - 1); strncpy (Options->OutFileName, Argv[1], MAX_PATH - 1);
Options->OutFileName[MAX_PATH - 1] = 0; Options->OutFileName[MAX_PATH - 1] = 0;
@ -993,7 +1001,8 @@ Returns:
// Help option // Help option
// //
Usage (); Usage ();
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} else if (stricmp (Argv[0], "-b") == 0) { } else if (stricmp (Argv[0], "-b") == 0) {
// //
// Specify binary files with -b // Specify binary files with -b
@ -1021,11 +1030,13 @@ Returns:
Status = AsciiStringToUint64(Argv[1], FALSE, &DebugLevel); Status = AsciiStringToUint64(Argv[1], FALSE, &DebugLevel);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
if (DebugLevel > 9) { if (DebugLevel > 9) {
Error (NULL, 0, 2000, "Invalid option value", "Debug Level range is 0-9, current input level is %d", Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "Debug Level range is 0-9, current input level is %d", Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
if (DebugLevel>=5 && DebugLevel<=9) { if (DebugLevel>=5 && DebugLevel<=9) {
Options->Debug = TRUE; Options->Debug = TRUE;
@ -1055,12 +1066,14 @@ Returns:
Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue); Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
ClassCode = (UINT32) TempValue; ClassCode = (UINT32) TempValue;
if (ClassCode & 0xFF000000) { if (ClassCode & 0xFF000000) {
Error (NULL, 0, 2000, "Invalid parameter", "Class code %s out of range!", Argv[1]); Error (NULL, 0, 2000, "Invalid parameter", "Class code %s out of range!", Argv[1]);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
if (FileList != NULL && FileList->ClassCode == 0) { if (FileList != NULL && FileList->ClassCode == 0) {
FileList->ClassCode = ClassCode; FileList->ClassCode = ClassCode;
@ -1076,12 +1089,14 @@ Returns:
Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue); Status = AsciiStringToUint64(Argv[1], FALSE, &TempValue);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]); Error (NULL, 0, 2000, "Invalid option value", "%s = %s", Argv[0], Argv[1]);
return 1; ReturnStatus = 1;
goto Done;
} }
CodeRevision = (UINT32) TempValue; CodeRevision = (UINT32) TempValue;
if (CodeRevision & 0xFFFF0000) { if (CodeRevision & 0xFFFF0000) {
Error (NULL, 0, 2000, "Invalid parameter", "Code revision %s out of range!", Argv[1]); Error (NULL, 0, 2000, "Invalid parameter", "Code revision %s out of range!", Argv[1]);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
if (FileList != NULL && FileList->CodeRevision == 0) { if (FileList != NULL && FileList->CodeRevision == 0) {
FileList->CodeRevision = (UINT16) CodeRevision; FileList->CodeRevision = (UINT16) CodeRevision;
@ -1095,7 +1110,8 @@ Returns:
mOptions.Pci23 = 1; mOptions.Pci23 = 1;
} else { } else {
Error (NULL, 0, 2000, "Invalid parameter", "Invalid option specified: %s", Argv[0]); Error (NULL, 0, 2000, "Invalid parameter", "Invalid option specified: %s", Argv[0]);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
} else { } else {
// //
@ -1104,7 +1120,8 @@ Returns:
// //
if ((FileFlags & (FILE_FLAG_BINARY | FILE_FLAG_EFI)) == 0) { if ((FileFlags & (FILE_FLAG_BINARY | FILE_FLAG_EFI)) == 0) {
Error (NULL, 0, 2000, "Invalid parameter", "Missing -e or -b with input file %s!", Argv[0]); Error (NULL, 0, 2000, "Invalid parameter", "Missing -e or -b with input file %s!", Argv[0]);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
// //
// Check Efi Option RomImage // Check Efi Option RomImage
@ -1118,7 +1135,8 @@ Returns:
FileList = (FILE_LIST *) malloc (sizeof (FILE_LIST)); FileList = (FILE_LIST *) malloc (sizeof (FILE_LIST));
if (FileList == NULL) { if (FileList == NULL) {
Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!", NULL); Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!", NULL);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
// //
@ -1156,6 +1174,9 @@ Returns:
// //
if (Options->FileList == NULL) { if (Options->FileList == NULL) {
Error (NULL, 0, 2000, "Invalid parameter", "Missing input file name!"); Error (NULL, 0, 2000, "Invalid parameter", "Missing input file name!");
//
// No memory allocation, return directly.
//
return STATUS_ERROR; return STATUS_ERROR;
} }
@ -1165,16 +1186,27 @@ Returns:
if (EfiRomFlag) { if (EfiRomFlag) {
if (!Options->VendIdValid) { if (!Options->VendIdValid) {
Error (NULL, 0, 2000, "Missing Vendor ID in command line", NULL); Error (NULL, 0, 2000, "Missing Vendor ID in command line", NULL);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
if (!Options->DevIdValid) { if (!Options->DevIdValid) {
Error (NULL, 0, 2000, "Missing Device ID in command line", NULL); Error (NULL, 0, 2000, "Missing Device ID in command line", NULL);
return STATUS_ERROR; ReturnStatus = STATUS_ERROR;
goto Done;
} }
} }
return 0; Done:
if (ReturnStatus != 0) {
while (Options->FileList != NULL) {
FileList = Options->FileList->Next;
free (Options->FileList);
Options->FileList = FileList;
}
}
return ReturnStatus;
} }
static static