ShellPkg: Refactor string manipulation

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@16038 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
Jaben Carsey
2014-09-02 20:17:38 +00:00
committed by jcarsey
parent 8ac6e336ff
commit 7f79b01e8e
7 changed files with 115 additions and 98 deletions

View File

@ -1,7 +1,7 @@
/** @file
Provides interface to shell MAN file parser.
Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
@ -39,15 +39,12 @@ GetManFileName(
// Fix the file name
//
if (StrnCmp(ManFileName+StrLen(ManFileName)-4, L".man", 4)==0) {
Buffer = AllocateZeroPool(StrSize(ManFileName));
if (Buffer != NULL) {
StrCpy(Buffer, ManFileName);
}
Buffer = AllocateCopyPool(StrSize(ManFileName), ManFileName);
} else {
Buffer = AllocateZeroPool(StrSize(ManFileName) + 4*sizeof(CHAR16));
if (Buffer != NULL) {
StrCpy(Buffer, ManFileName);
StrCat(Buffer, L".man");
StrnCpy(Buffer, ManFileName, StrLen(ManFileName));
StrnCat(Buffer, L".man", 4);
}
}
return (Buffer);
@ -374,6 +371,9 @@ ManBufferFindTitleSection(
CHAR16 *TitleString;
CHAR16 *TitleEnd;
CHAR16 *CurrentLocation;
UINTN TitleLength;
CONST CHAR16 StartString[] = L".TH ";
CONST CHAR16 EndString[] = L" 0 ";
if ( Buffer == NULL
|| Command == NULL
@ -384,13 +384,17 @@ ManBufferFindTitleSection(
Status = EFI_SUCCESS;
TitleString = AllocateZeroPool((7*sizeof(CHAR16)) + StrSize(Command));
//
// more characters for StartString and EndString
//
TitleLength = StrSize(Command) + (StrLen(StartString) + StrLen(EndString)) * sizeof(CHAR16);
TitleString = AllocateZeroPool(TitleLength);
if (TitleString == NULL) {
return (EFI_OUT_OF_RESOURCES);
}
StrCpy(TitleString, L".TH ");
StrCat(TitleString, Command);
StrCat(TitleString, L" 0 ");
StrnCpy(TitleString, StartString, TitleLength/sizeof(CHAR16) - 1);
StrnCat(TitleString, Command, TitleLength/sizeof(CHAR16) - 1 - StrLen(TitleString));
StrnCat(TitleString, EndString, TitleLength/sizeof(CHAR16) - 1 - StrLen(TitleString));
CurrentLocation = StrStr(*Buffer, TitleString);
if (CurrentLocation == NULL){
@ -467,6 +471,7 @@ ManFileFindTitleSection(
CHAR16 *TitleEnd;
UINTN TitleLen;
BOOLEAN Found;
UINTN TitleSize;
if ( Handle == NULL
|| Command == NULL
@ -484,13 +489,14 @@ ManFileFindTitleSection(
return (EFI_OUT_OF_RESOURCES);
}
TitleString = AllocateZeroPool((4*sizeof(CHAR16)) + StrSize(Command));
TitleSize = (4*sizeof(CHAR16)) + StrSize(Command);
TitleString = AllocateZeroPool(TitleSize);
if (TitleString == NULL) {
FreePool(ReadLine);
return (EFI_OUT_OF_RESOURCES);
}
StrCpy(TitleString, L".TH ");
StrCat(TitleString, Command);
StrnCpy(TitleString, L".TH ", TitleSize/sizeof(CHAR16) - 1);
StrnCat(TitleString, Command, TitleSize/sizeof(CHAR16) - 1 - StrLen(TitleString));
TitleLen = StrLen(TitleString);
for (;!ShellFileHandleEof(Handle);Size = 1024) {
@ -526,7 +532,7 @@ ManFileFindTitleSection(
Status = EFI_OUT_OF_RESOURCES;
break;
}
StrCpy(*BriefDesc, TitleEnd);
StrnCpy(*BriefDesc, TitleEnd, (*BriefSize)/sizeof(CHAR16) - 1);
}
break;
}