ShellPkg: Refactor string manipulation in cp 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: Eric Dong <eric.dong@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15771 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Jaben Carsey 2014-08-07 20:02:40 +00:00 committed by jcarsey
parent 6e1e540554
commit e1044f8074
1 changed files with 20 additions and 20 deletions

View File

@ -297,7 +297,7 @@ ValidateAndCopyFiles(
SHELL_STATUS ShellStatus; SHELL_STATUS ShellStatus;
CHAR16 *DestPath; CHAR16 *DestPath;
VOID *Response; VOID *Response;
UINTN PathLen; UINTN PathSize;
CONST CHAR16 *Cwd; CONST CHAR16 *Cwd;
UINTN NewSize; UINTN NewSize;
@ -309,7 +309,7 @@ ValidateAndCopyFiles(
DestPath = NULL; DestPath = NULL;
ShellStatus = SHELL_SUCCESS; ShellStatus = SHELL_SUCCESS;
PathLen = 0; PathSize = 0;
Cwd = ShellGetCurrentDir(NULL); Cwd = ShellGetCurrentDir(NULL);
ASSERT(FileList != NULL); ASSERT(FileList != NULL);
@ -339,8 +339,8 @@ ValidateAndCopyFiles(
NewSize = StrSize(DestDir); NewSize = StrSize(DestDir);
NewSize += StrSize(Node->FullName); NewSize += StrSize(Node->FullName);
NewSize += (Cwd == NULL)? 0 : StrSize(Cwd); NewSize += (Cwd == NULL)? 0 : StrSize(Cwd);
if (NewSize > PathLen) { if (NewSize > PathSize) {
PathLen = NewSize; PathSize = NewSize;
} }
// //
@ -365,7 +365,7 @@ ValidateAndCopyFiles(
HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_CP_OUTPUT), NULL); HiiOutput = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_CP_OUTPUT), NULL);
HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL); HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);
DestPath = AllocateZeroPool(PathLen); DestPath = AllocateZeroPool(PathSize);
if (DestPath == NULL || HiiOutput == NULL || HiiResultOk == NULL) { if (DestPath == NULL || HiiOutput == NULL || HiiResultOk == NULL) {
SHELL_FREE_NON_NULL(DestPath); SHELL_FREE_NON_NULL(DestPath);
@ -402,19 +402,19 @@ ValidateAndCopyFiles(
// simple copy of a single file // simple copy of a single file
// //
if (Cwd != NULL) { if (Cwd != NULL) {
StrCpy(DestPath, Cwd); StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16)-1);
} else { } else {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir); ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir);
return (SHELL_INVALID_PARAMETER); return (SHELL_INVALID_PARAMETER);
} }
if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') { if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {
StrCat(DestPath, L"\\"); StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') { } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {
((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL; ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;
} }
StrCat(DestPath, DestDir); StrnCat(DestPath, DestDir, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} else { } else {
StrCpy(DestPath, DestDir); StrnCpy(DestPath, DestDir, PathSize/sizeof(CHAR16) -1);
} }
} else { } else {
// //
@ -429,42 +429,42 @@ ValidateAndCopyFiles(
// Copy to the root of CWD // Copy to the root of CWD
// //
if (Cwd != NULL) { if (Cwd != NULL) {
StrCpy(DestPath, Cwd); StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);
} else { } else {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir); ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir);
return (SHELL_INVALID_PARAMETER); return (SHELL_INVALID_PARAMETER);
} }
while (PathRemoveLastItem(DestPath)); while (PathRemoveLastItem(DestPath));
StrCat(DestPath, DestDir+1); StrnCat(DestPath, DestDir+1, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
StrCat(DestPath, Node->FileName); StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} else if (StrStr(DestDir, L":") == NULL) { } else if (StrStr(DestDir, L":") == NULL) {
if (Cwd != NULL) { if (Cwd != NULL) {
StrCpy(DestPath, Cwd); StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);
} else { } else {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir); ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, DestDir);
return (SHELL_INVALID_PARAMETER); return (SHELL_INVALID_PARAMETER);
} }
if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') { if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {
StrCat(DestPath, L"\\"); StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') { } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {
((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL; ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;
} }
StrCat(DestPath, DestDir); StrnCat(DestPath, DestDir, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') { if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {
StrCat(DestPath, L"\\"); StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') { } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {
((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL; ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;
} }
StrCat(DestPath, Node->FileName); StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} else { } else {
StrCpy(DestPath, DestDir); StrnCpy(DestPath, DestDir, PathSize/sizeof(CHAR16) -1);
if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') { if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {
StrCat(DestPath, L"\\"); StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') { } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {
((CHAR16*)DestDir)[StrLen(DestDir)-1] = CHAR_NULL; ((CHAR16*)DestDir)[StrLen(DestDir)-1] = CHAR_NULL;
} }
StrCat(DestPath, Node->FileName); StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);
} }
} }