mirror of https://github.com/acidanthera/audk.git
ShellPkg: Refactor string manipulation in UefiShellLib command
This patch replaces StrCpy with StrnCpy or refactors out the usage of StrCpy through some other means. This patch replaces StrCat with StrnCat or refactors out the usage of StrCat through some other means. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jaben Carsey <jaben.carsey@intel.com> Reviewed-by: Erik Bjorge <erik.c.bjorge@intel.com> git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15839 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
parent
8af89dae12
commit
98c16be588
|
@ -1430,26 +1430,30 @@ InternalShellConvertFileListType (
|
||||||
//
|
//
|
||||||
// allocate new space to copy strings and structure
|
// allocate new space to copy strings and structure
|
||||||
//
|
//
|
||||||
NewInfo->FullName = AllocateZeroPool(StrSize(OldInfo->FullName));
|
NewInfo->FullName = AllocateCopyPool(StrSize(OldInfo->FullName), OldInfo->FullName);
|
||||||
NewInfo->FileName = AllocateZeroPool(StrSize(OldInfo->FileName));
|
NewInfo->FileName = AllocateCopyPool(StrSize(OldInfo->FileName), OldInfo->FileName);
|
||||||
NewInfo->Info = AllocateZeroPool((UINTN)OldInfo->Info->Size);
|
NewInfo->Info = AllocateCopyPool((UINTN)OldInfo->Info->Size, OldInfo->Info);
|
||||||
|
|
||||||
//
|
//
|
||||||
// make sure all the memory allocations were sucessful
|
// make sure all the memory allocations were sucessful
|
||||||
//
|
//
|
||||||
if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
|
if (NULL == NewInfo->FullName || NewInfo->FileName == NULL || NewInfo->Info == NULL) {
|
||||||
|
//
|
||||||
|
// Free the partially allocated new node
|
||||||
|
//
|
||||||
|
SHELL_FREE_NON_NULL(NewInfo->FullName);
|
||||||
|
SHELL_FREE_NON_NULL(NewInfo->FileName);
|
||||||
|
SHELL_FREE_NON_NULL(NewInfo->Info);
|
||||||
|
SHELL_FREE_NON_NULL(NewInfo);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Free the previously converted stuff
|
||||||
|
//
|
||||||
ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
|
ShellCloseFileMetaArg((EFI_SHELL_FILE_INFO**)(&ListHead));
|
||||||
ListHead = NULL;
|
ListHead = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
// Copt the strings and structure
|
|
||||||
//
|
|
||||||
StrCpy(NewInfo->FullName, OldInfo->FullName);
|
|
||||||
StrCpy(NewInfo->FileName, OldInfo->FileName);
|
|
||||||
gBS->CopyMem (NewInfo->Info, OldInfo->Info, (UINTN)OldInfo->Info->Size);
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// add that to the list
|
// add that to the list
|
||||||
//
|
//
|
||||||
|
@ -1671,8 +1675,8 @@ ShellFindFilePath (
|
||||||
if (TestPath == NULL) {
|
if (TestPath == NULL) {
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
StrCpy(TestPath, Path);
|
StrnCpy(TestPath, Path, Size/sizeof(CHAR16) - 1);
|
||||||
StrCat(TestPath, FileName);
|
StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
|
||||||
Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
|
Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0);
|
||||||
if (!EFI_ERROR(Status)){
|
if (!EFI_ERROR(Status)){
|
||||||
if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
|
if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) {
|
||||||
|
@ -1704,12 +1708,12 @@ ShellFindFilePath (
|
||||||
*TempChar = CHAR_NULL;
|
*TempChar = CHAR_NULL;
|
||||||
}
|
}
|
||||||
if (TestPath[StrLen(TestPath)-1] != L'\\') {
|
if (TestPath[StrLen(TestPath)-1] != L'\\') {
|
||||||
StrCat(TestPath, L"\\");
|
StrnCat(TestPath, L"\\", Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
|
||||||
}
|
}
|
||||||
if (FileName[0] == L'\\') {
|
if (FileName[0] == L'\\') {
|
||||||
FileName++;
|
FileName++;
|
||||||
}
|
}
|
||||||
StrCat(TestPath, FileName);
|
StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
|
||||||
if (StrStr(Walker, L";") != NULL) {
|
if (StrStr(Walker, L";") != NULL) {
|
||||||
Walker = StrStr(Walker, L";") + 1;
|
Walker = StrStr(Walker, L";") + 1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -1778,9 +1782,9 @@ ShellFindFilePathEx (
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
|
for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){
|
||||||
StrCpy(TestPath, FileName);
|
StrnCpy(TestPath, FileName, Size/sizeof(CHAR16) - 1);
|
||||||
if (ExtensionWalker != NULL) {
|
if (ExtensionWalker != NULL) {
|
||||||
StrCat(TestPath, ExtensionWalker);
|
StrnCat(TestPath, ExtensionWalker, Size/sizeof(CHAR16) - 1 - StrLen(TestPath));
|
||||||
}
|
}
|
||||||
TempChar = StrStr(TestPath, L";");
|
TempChar = StrStr(TestPath, L";");
|
||||||
if (TempChar != NULL) {
|
if (TempChar != NULL) {
|
||||||
|
@ -1963,6 +1967,7 @@ InternalCommandLineParse (
|
||||||
UINTN ValueSize;
|
UINTN ValueSize;
|
||||||
UINTN Count;
|
UINTN Count;
|
||||||
CONST CHAR16 *TempPointer;
|
CONST CHAR16 *TempPointer;
|
||||||
|
UINTN CurrentValueSize;
|
||||||
|
|
||||||
CurrentItemPackage = NULL;
|
CurrentItemPackage = NULL;
|
||||||
GetItemValue = 0;
|
GetItemValue = 0;
|
||||||
|
@ -2018,13 +2023,12 @@ InternalCommandLineParse (
|
||||||
*CheckPackage = NULL;
|
*CheckPackage = NULL;
|
||||||
return (EFI_OUT_OF_RESOURCES);
|
return (EFI_OUT_OF_RESOURCES);
|
||||||
}
|
}
|
||||||
CurrentItemPackage->Name = AllocateZeroPool(StrSize(Argv[LoopCounter]));
|
CurrentItemPackage->Name = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
|
||||||
if (CurrentItemPackage->Name == NULL) {
|
if (CurrentItemPackage->Name == NULL) {
|
||||||
ShellCommandLineFreeVarList(*CheckPackage);
|
ShellCommandLineFreeVarList(*CheckPackage);
|
||||||
*CheckPackage = NULL;
|
*CheckPackage = NULL;
|
||||||
return (EFI_OUT_OF_RESOURCES);
|
return (EFI_OUT_OF_RESOURCES);
|
||||||
}
|
}
|
||||||
StrCpy(CurrentItemPackage->Name, Argv[LoopCounter]);
|
|
||||||
CurrentItemPackage->Type = CurrentItemType;
|
CurrentItemPackage->Type = CurrentItemType;
|
||||||
CurrentItemPackage->OriginalPosition = (UINTN)(-1);
|
CurrentItemPackage->OriginalPosition = (UINTN)(-1);
|
||||||
CurrentItemPackage->Value = NULL;
|
CurrentItemPackage->Value = NULL;
|
||||||
|
@ -2062,30 +2066,32 @@ InternalCommandLineParse (
|
||||||
// get the item VALUE for a previous flag
|
// get the item VALUE for a previous flag
|
||||||
//
|
//
|
||||||
if (StrStr(Argv[LoopCounter], L" ") == NULL) {
|
if (StrStr(Argv[LoopCounter], L" ") == NULL) {
|
||||||
CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16), CurrentItemPackage->Value);
|
CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
|
||||||
|
CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
|
||||||
ASSERT(CurrentItemPackage->Value != NULL);
|
ASSERT(CurrentItemPackage->Value != NULL);
|
||||||
if (ValueSize == 0) {
|
if (ValueSize == 0) {
|
||||||
StrCpy(CurrentItemPackage->Value, Argv[LoopCounter]);
|
StrnCpy(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1);
|
||||||
} else {
|
} else {
|
||||||
StrCat(CurrentItemPackage->Value, L" ");
|
StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
|
StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
}
|
}
|
||||||
ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
|
ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
|
||||||
} else {
|
} else {
|
||||||
//
|
//
|
||||||
// the parameter has spaces. must be quoted.
|
// the parameter has spaces. must be quoted.
|
||||||
//
|
//
|
||||||
CurrentItemPackage->Value = ReallocatePool(ValueSize, ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16), CurrentItemPackage->Value);
|
CurrentValueSize = ValueSize + StrSize(Argv[LoopCounter]) + sizeof(CHAR16) + sizeof(CHAR16) + sizeof(CHAR16);
|
||||||
|
CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value);
|
||||||
ASSERT(CurrentItemPackage->Value != NULL);
|
ASSERT(CurrentItemPackage->Value != NULL);
|
||||||
if (ValueSize == 0) {
|
if (ValueSize == 0) {
|
||||||
StrCpy(CurrentItemPackage->Value, L"\"");
|
StrnCpy(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1);
|
||||||
StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
|
StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
StrCat(CurrentItemPackage->Value, L"\"");
|
StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
} else {
|
} else {
|
||||||
StrCat(CurrentItemPackage->Value, L" ");
|
StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
StrCat(CurrentItemPackage->Value, L"\"");
|
StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
StrCat(CurrentItemPackage->Value, Argv[LoopCounter]);
|
StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
StrCat(CurrentItemPackage->Value, L"\"");
|
StrnCat(CurrentItemPackage->Value, L"\"", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value));
|
||||||
}
|
}
|
||||||
ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
|
ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16);
|
||||||
}
|
}
|
||||||
|
@ -2113,13 +2119,12 @@ InternalCommandLineParse (
|
||||||
}
|
}
|
||||||
CurrentItemPackage->Name = NULL;
|
CurrentItemPackage->Name = NULL;
|
||||||
CurrentItemPackage->Type = TypePosition;
|
CurrentItemPackage->Type = TypePosition;
|
||||||
CurrentItemPackage->Value = AllocateZeroPool(StrSize(TempPointer));
|
CurrentItemPackage->Value = AllocateCopyPool(StrSize(TempPointer), TempPointer);
|
||||||
if (CurrentItemPackage->Value == NULL) {
|
if (CurrentItemPackage->Value == NULL) {
|
||||||
ShellCommandLineFreeVarList(*CheckPackage);
|
ShellCommandLineFreeVarList(*CheckPackage);
|
||||||
*CheckPackage = NULL;
|
*CheckPackage = NULL;
|
||||||
return (EFI_OUT_OF_RESOURCES);
|
return (EFI_OUT_OF_RESOURCES);
|
||||||
}
|
}
|
||||||
StrCpy(CurrentItemPackage->Value, TempPointer);
|
|
||||||
CurrentItemPackage->OriginalPosition = Count++;
|
CurrentItemPackage->OriginalPosition = Count++;
|
||||||
InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
|
InsertHeadList(*CheckPackage, &CurrentItemPackage->Link);
|
||||||
} else {
|
} else {
|
||||||
|
@ -2127,10 +2132,7 @@ InternalCommandLineParse (
|
||||||
// this was a non-recognised flag... error!
|
// this was a non-recognised flag... error!
|
||||||
//
|
//
|
||||||
if (ProblemParam != NULL) {
|
if (ProblemParam != NULL) {
|
||||||
*ProblemParam = AllocateZeroPool(StrSize(Argv[LoopCounter]));
|
*ProblemParam = AllocateCopyPool(StrSize(Argv[LoopCounter]), Argv[LoopCounter]);
|
||||||
if (*ProblemParam != NULL) {
|
|
||||||
StrCpy(*ProblemParam, Argv[LoopCounter]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ShellCommandLineFreeVarList(*CheckPackage);
|
ShellCommandLineFreeVarList(*CheckPackage);
|
||||||
*CheckPackage = NULL;
|
*CheckPackage = NULL;
|
||||||
|
@ -2597,7 +2599,7 @@ ShellCopySearchAndReplace(
|
||||||
if (Replace == NULL) {
|
if (Replace == NULL) {
|
||||||
return (EFI_OUT_OF_RESOURCES);
|
return (EFI_OUT_OF_RESOURCES);
|
||||||
}
|
}
|
||||||
NewString = SetMem16(NewString, NewSize, CHAR_NULL);
|
NewString = ZeroMem(NewString, NewSize);
|
||||||
while (*SourceString != CHAR_NULL) {
|
while (*SourceString != CHAR_NULL) {
|
||||||
//
|
//
|
||||||
// if we find the FindTarget and either Skip == FALSE or Skip and we
|
// if we find the FindTarget and either Skip == FALSE or Skip and we
|
||||||
|
@ -2612,7 +2614,7 @@ ShellCopySearchAndReplace(
|
||||||
FreePool(Replace);
|
FreePool(Replace);
|
||||||
return (EFI_BUFFER_TOO_SMALL);
|
return (EFI_BUFFER_TOO_SMALL);
|
||||||
}
|
}
|
||||||
StrCat(NewString, Replace);
|
StrnCat(NewString, Replace, NewSize/sizeof(CHAR16) - 1 - StrLen(NewString));
|
||||||
} else {
|
} else {
|
||||||
Size = StrSize(NewString);
|
Size = StrSize(NewString);
|
||||||
if (Size + sizeof(CHAR16) > NewSize) {
|
if (Size + sizeof(CHAR16) > NewSize) {
|
||||||
|
|
Loading…
Reference in New Issue