From e75390f02971bcd4d67a9696508050bee4936a01 Mon Sep 17 00:00:00 2001 From: Qiu Shumin Date: Tue, 30 Jun 2015 03:18:31 +0000 Subject: ShellPkg: Use safe string functions to refine code. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Qiu Shumin Reviewed-by: Jaben Carsey Reviewed-by: Star Zeng git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@17730 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Library/UefiShellLib/UefiShellLib.c | 35 ++++++++++++++++++---------- 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'ShellPkg/Library/UefiShellLib/UefiShellLib.c') diff --git a/ShellPkg/Library/UefiShellLib/UefiShellLib.c b/ShellPkg/Library/UefiShellLib/UefiShellLib.c index f82668b60f..5b4c6d3fb7 100644 --- a/ShellPkg/Library/UefiShellLib/UefiShellLib.c +++ b/ShellPkg/Library/UefiShellLib/UefiShellLib.c @@ -1713,8 +1713,8 @@ ShellFindFilePath ( if (TestPath == NULL) { return (NULL); } - StrnCpy(TestPath, Path, Size/sizeof(CHAR16) - 1); - StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); + StrCpyS(TestPath, Size/sizeof(CHAR16), Path); + StrCatS(TestPath, Size/sizeof(CHAR16), FileName); Status = ShellOpenFileByName(TestPath, &Handle, EFI_FILE_MODE_READ, 0); if (!EFI_ERROR(Status)){ if (FileHandleIsDirectory(Handle) != EFI_SUCCESS) { @@ -1746,12 +1746,12 @@ ShellFindFilePath ( *TempChar = CHAR_NULL; } if (TestPath[StrLen(TestPath)-1] != L'\\') { - StrnCat(TestPath, L"\\", Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); + StrCatS(TestPath, Size/sizeof(CHAR16), L"\\"); } if (FileName[0] == L'\\') { FileName++; } - StrnCat(TestPath, FileName, Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); + StrCatS(TestPath, Size/sizeof(CHAR16), FileName); if (StrStr(Walker, L";") != NULL) { Walker = StrStr(Walker, L";") + 1; } else { @@ -1820,9 +1820,9 @@ ShellFindFilePathEx ( return (NULL); } for (ExtensionWalker = FileExtension, TempChar2 = (CHAR16*)FileExtension; TempChar2 != NULL ; ExtensionWalker = TempChar2 + 1){ - StrnCpy(TestPath, FileName, Size/sizeof(CHAR16) - 1); + StrCpyS(TestPath, Size/sizeof(CHAR16), FileName); if (ExtensionWalker != NULL) { - StrnCat(TestPath, ExtensionWalker, Size/sizeof(CHAR16) - 1 - StrLen(TestPath)); + StrCatS(TestPath, Size/sizeof(CHAR16), ExtensionWalker); } TempChar = StrStr(TestPath, L";"); if (TempChar != NULL) { @@ -2109,10 +2109,19 @@ InternalCommandLineParse ( CurrentItemPackage->Value = ReallocatePool(ValueSize, CurrentValueSize, CurrentItemPackage->Value); ASSERT(CurrentItemPackage->Value != NULL); if (ValueSize == 0) { - StrnCpy(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1); + StrCpyS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), + Argv[LoopCounter] + ); } else { - StrnCat(CurrentItemPackage->Value, L" ", CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); - StrnCat(CurrentItemPackage->Value, Argv[LoopCounter], CurrentValueSize/sizeof(CHAR16) - 1 - StrLen(CurrentItemPackage->Value)); + StrCatS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), + L" " + ); + StrCatS( CurrentItemPackage->Value, + CurrentValueSize/sizeof(CHAR16), + Argv[LoopCounter] + ); } ValueSize += StrSize(Argv[LoopCounter]) + sizeof(CHAR16); @@ -2635,14 +2644,14 @@ ShellCopySearchAndReplace( FreePool(Replace); return (EFI_BUFFER_TOO_SMALL); } - StrnCat(NewString, Replace, NewSize/sizeof(CHAR16) - 1 - StrLen(NewString)); + StrCatS(NewString, NewSize/sizeof(CHAR16), Replace); } else { Size = StrSize(NewString); if (Size + sizeof(CHAR16) > NewSize) { FreePool(Replace); return (EFI_BUFFER_TOO_SMALL); } - StrnCat(NewString, SourceString, 1); + StrnCatS(NewString, NewSize/sizeof(CHAR16), SourceString, 1); SourceString++; } } @@ -3254,7 +3263,9 @@ StrnCatGrow ( if (*Destination == NULL) { return (NULL); } - return StrnCat(*Destination, Source, Count); + + StrCatS(*Destination, Count + 1, Source); + return *Destination; } /** -- cgit v1.2.3