From 7f79b01e8ef43901785dbccc1f4676cdf9a53d31 Mon Sep 17 00:00:00 2001 From: Jaben Carsey Date: Tue, 2 Sep 2014 20:17:38 +0000 Subject: [PATCH] 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 Reviewed-by: Erik Bjorge git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16038 6f19259b-4bc3-4df7-8a09-765794883524 --- .../Application/Shell/FileHandleWrappers.c | 11 ++-- ShellPkg/Application/Shell/Shell.c | 53 +++++++++---------- ShellPkg/Application/Shell/ShellEnvVar.c | 46 +++++++++++----- ShellPkg/Application/Shell/ShellManParser.c | 36 +++++++------ .../Shell/ShellParametersProtocol.c | 32 +++++------ .../Shell/ShellParametersProtocol.h | 6 ++- ShellPkg/Application/Shell/ShellProtocol.c | 29 ++++------ 7 files changed, 115 insertions(+), 98 deletions(-) diff --git a/ShellPkg/Application/Shell/FileHandleWrappers.c b/ShellPkg/Application/Shell/FileHandleWrappers.c index 59c1ee55ee..be786bb2b5 100644 --- a/ShellPkg/Application/Shell/FileHandleWrappers.c +++ b/ShellPkg/Application/Shell/FileHandleWrappers.c @@ -509,21 +509,20 @@ FileInterfaceStdInRead( if (StrStr(CurrentString + TabPos, L":") == NULL) { Cwd = ShellInfoObject.NewEfiShellProtocol->GetCurDir(NULL); if (Cwd != NULL) { - StrCpy(TabStr, Cwd); + StrnCpy(TabStr, Cwd, (*BufferSize)/sizeof(CHAR16) - 1); if (TabStr[StrLen(TabStr)-1] == L'\\' && *(CurrentString + TabPos) == L'\\' ) { TabStr[StrLen(TabStr)-1] = CHAR_NULL; } StrnCat(TabStr, CurrentString + TabPos, (StringLen - TabPos) * sizeof (CHAR16)); } else { - StrCpy(TabStr, L""); + *TabStr = CHAR_NULL; StrnCat(TabStr, CurrentString + TabPos, (StringLen - TabPos) * sizeof (CHAR16)); } } else { - StrCpy(TabStr, CurrentString + TabPos); + StrnCpy(TabStr, CurrentString + TabPos, (*BufferSize)/sizeof(CHAR16) - 1); } - StrCat(TabStr, L"*"); + StrnCat(TabStr, L"*", (*BufferSize)/sizeof(CHAR16) - 1 - StrLen(TabStr)); FoundFileList = NULL; -// TabStr = PathCleanUpDirectories(TabStr); Status = ShellInfoObject.NewEfiShellProtocol->FindFiles(TabStr, &FoundFileList); for ( TempStr = CurrentString ; *TempStr == L' ' @@ -1168,7 +1167,7 @@ CreateFileInterfaceEnv( EnvFileInterface->Delete = FileInterfaceEnvDelete; EnvFileInterface->Read = FileInterfaceEnvRead; - StrCpy(EnvFileInterface->Name, EnvName); + StrnCpy(EnvFileInterface->Name, EnvName, StrLen(EnvName)); // // Assign the different members for Volatile and Non-Volatile variables diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 1d3064531d..976c314cd3 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -937,7 +937,7 @@ ProcessCommandLine( continue; } - ShellInfoObject.ShellInitSettings.FileName = AllocateZeroPool(StrSize(CurrentArg)); + ShellInfoObject.ShellInitSettings.FileName = AllocateCopyPool(StrSize(CurrentArg), CurrentArg); if (ShellInfoObject.ShellInitSettings.FileName == NULL) { return (EFI_OUT_OF_RESOURCES); } @@ -945,8 +945,6 @@ ProcessCommandLine( // We found `file-name`. // ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1; - - StrCpy (ShellInfoObject.ShellInitSettings.FileName, CurrentArg); LoopVar++; // Add `file-name-options` @@ -1027,10 +1025,10 @@ DoStartupScript( if (FileStringPath == NULL) { return (EFI_OUT_OF_RESOURCES); } - StrCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName); + StrnCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName, NewSize/sizeof(CHAR16) -1); if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) { - StrCat(FileStringPath, L" "); - StrCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions); + StrnCat(FileStringPath, L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1); + StrnCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1); } Status = RunCommand(FileStringPath, ExitStatus); FreePool(FileStringPath); @@ -1247,9 +1245,8 @@ AddLineToCommandHistory( Node = AllocateZeroPool(sizeof(BUFFER_LIST)); ASSERT(Node != NULL); - Node->Buffer = AllocateZeroPool(StrSize(Buffer)); + Node->Buffer = AllocateCopyPool(StrSize(Buffer), Buffer); ASSERT(Node->Buffer != NULL); - StrCpy(Node->Buffer, Buffer); InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link); } @@ -1280,11 +1277,10 @@ ShellConvertAlias( return (EFI_SUCCESS); } FreePool(*CommandString); - *CommandString = AllocateZeroPool(StrSize(NewString)); + *CommandString = AllocateCopyPool(StrSize(NewString), NewString); if (*CommandString == NULL) { return (EFI_OUT_OF_RESOURCES); } - StrCpy(*CommandString, NewString); return (EFI_SUCCESS); } @@ -1477,7 +1473,7 @@ ShellConvertVariables ( // // now do the replacements... // - NewCommandLine1 = AllocateZeroPool(NewSize); + NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine); NewCommandLine2 = AllocateZeroPool(NewSize); ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) { @@ -1486,16 +1482,15 @@ ShellConvertVariables ( SHELL_FREE_NON_NULL(ItemTemp); return (NULL); } - StrCpy(NewCommandLine1, OriginalCommandLine); for (MasterEnvList = EfiShellGetEnv(NULL) - ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL + ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL ; MasterEnvList += StrLen(MasterEnvList) + 1 ){ - StrCpy(ItemTemp, L"%"); - StrCat(ItemTemp, MasterEnvList); - StrCat(ItemTemp, L"%"); + *ItemTemp = L'%'; + StrnCat(ItemTemp, MasterEnvList, ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16))-1 - StrLen(ItemTemp)); + StrnCat(ItemTemp, L"%", ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16))-1 - StrLen(ItemTemp)); ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE); - StrCpy(NewCommandLine1, NewCommandLine2); + StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1); } if (CurrentScriptFile != NULL) { for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList) @@ -1503,7 +1498,7 @@ ShellConvertVariables ( ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link) ){ ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE); - StrCpy(NewCommandLine1, NewCommandLine2); + StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1); } // @@ -1516,7 +1511,7 @@ ShellConvertVariables ( // Now cleanup any straggler intentionally ignored "%" characters // ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE); - StrCpy(NewCommandLine1, NewCommandLine2); + StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1); FreePool(NewCommandLine2); FreePool(ItemTemp); @@ -1850,7 +1845,7 @@ IsValidSplit( return (EFI_OUT_OF_RESOURCES); } TempWalker = (CHAR16*)Temp; - GetNextParameter(&TempWalker, &FirstParameter); + GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine)); if (GetOperationType(FirstParameter) == Unknown_Invalid) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter); @@ -2018,7 +2013,7 @@ DoHelpUpdate( Walker = *CmdLine; while(Walker != NULL && *Walker != CHAR_NULL) { LastWalker = Walker; - GetNextParameter(&Walker, &CurrentParameter); + GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine)); if (StrStr(CurrentParameter, L"-?") == CurrentParameter) { LastWalker[0] = L' '; LastWalker[1] = L' '; @@ -2027,8 +2022,12 @@ DoHelpUpdate( Status = EFI_OUT_OF_RESOURCES; break; } - StrCpy(NewCommandLine, L"help "); - StrCat(NewCommandLine, *CmdLine); + + // + // We know the space is sufficient since we just calculated it. + // + StrnCpy(NewCommandLine, L"help ", 5); + StrnCat(NewCommandLine, *CmdLine, StrLen(*CmdLine)); SHELL_FREE_NON_NULL(*CmdLine); *CmdLine = NewCommandLine; break; @@ -2507,7 +2506,7 @@ RunCommand( return (EFI_OUT_OF_RESOURCES); } TempWalker = CleanOriginal; - GetNextParameter(&TempWalker, &FirstParameter); + GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal)); // // Depending on the first parameter we change the behavior @@ -2703,7 +2702,7 @@ RunScriptFileHandle ( ; // conditional increment in the body of the loop ){ ASSERT(CommandLine2 != NULL); - StrCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl); + StrnCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1); // // NULL out comments @@ -2722,7 +2721,7 @@ RunScriptFileHandle ( // // Due to variability in starting the find and replace action we need to have both buffers the same. // - StrCpy(CommandLine, CommandLine2); + StrnCpy(CommandLine, CommandLine2, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1); // // Remove the %0 to %9 from the command line (if we have some arguments) @@ -2774,7 +2773,7 @@ RunScriptFileHandle ( Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", L"\"\"", FALSE, FALSE); Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", L"\"\"", FALSE, FALSE); - StrCpy(CommandLine2, CommandLine); + StrnCpy(CommandLine2, CommandLine, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1); LastCommand = NewScriptFile->CurrentCommand; diff --git a/ShellPkg/Application/Shell/ShellEnvVar.c b/ShellPkg/Application/Shell/ShellEnvVar.c index f40a867cc7..b94711142a 100644 --- a/ShellPkg/Application/Shell/ShellEnvVar.c +++ b/ShellPkg/Application/Shell/ShellEnvVar.c @@ -152,7 +152,7 @@ GetEnvironmentVariableList( if (VariableName == NULL) { return (EFI_OUT_OF_RESOURCES); } - StrCpy(VariableName, L""); + *VariableName = CHAR_NULL; while (!EFI_ERROR(Status)) { NameSize = (UINTN)MaxVarSize; @@ -178,13 +178,12 @@ GetEnvironmentVariableList( } } if (!EFI_ERROR(Status) && VarList != NULL) { - VarList->Key = AllocateZeroPool(StrSize(VariableName)); + VarList->Key = AllocateCopyPool(StrSize(VariableName), VariableName); if (VarList->Key == NULL) { SHELL_FREE_NON_NULL(VarList->Val); SHELL_FREE_NON_NULL(VarList); Status = EFI_OUT_OF_RESOURCES; } else { - StrCpy(VarList->Key, VariableName); InsertTailList(ListHead, &VarList->Link); } } @@ -286,7 +285,6 @@ SetEnvironmentVariables( UINTN CurrentCount; ENV_VAR_LIST *VarList; ENV_VAR_LIST *Node; - UINTN NewSize; VarList = NULL; @@ -307,20 +305,44 @@ SetEnvironmentVariables( } ASSERT(StrStr(CurrentString, L"=") != NULL); Node = AllocateZeroPool(sizeof(ENV_VAR_LIST)); - ASSERT(Node != NULL); + if (Node == NULL) { + SetEnvironmentVariableList(&VarList->Link); + return (EFI_OUT_OF_RESOURCES); + } + Node->Key = AllocateZeroPool((StrStr(CurrentString, L"=") - CurrentString + 1) * sizeof(CHAR16)); - ASSERT(Node->Key != NULL); + if (Node->Key == NULL) { + SHELL_FREE_NON_NULL(Node); + SetEnvironmentVariableList(&VarList->Link); + return (EFI_OUT_OF_RESOURCES); + } + + // + // Copy the string into the Key, leaving the last character allocated as NULL to terminate + // StrnCpy(Node->Key, CurrentString, StrStr(CurrentString, L"=") - CurrentString); - NewSize = StrSize(CurrentString); - NewSize -= StrLen(Node->Key) - 1; - Node->Val = AllocateZeroPool(NewSize); - ASSERT(Node->Val != NULL); - StrCpy(Node->Val, CurrentString + StrLen(Node->Key) + 1); + + // + // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other) + // + Node->Val = AllocateCopyPool(StrSize(CurrentString) - StrSize(Node->Key), CurrentString + StrLen(Node->Key) + 1); + if (Node->Val == NULL) { + SHELL_FREE_NON_NULL(Node->Key); + SHELL_FREE_NON_NULL(Node); + SetEnvironmentVariableList(&VarList->Link); + return (EFI_OUT_OF_RESOURCES); + } + Node->Atts = EFI_VARIABLE_BOOTSERVICE_ACCESS; if (VarList == NULL) { VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST)); - ASSERT(VarList != NULL); + if (VarList == NULL) { + SHELL_FREE_NON_NULL(Node->Key); + SHELL_FREE_NON_NULL(Node->Val); + SHELL_FREE_NON_NULL(Node); + return (EFI_OUT_OF_RESOURCES); + } InitializeListHead(&VarList->Link); } InsertTailList(&VarList->Link, &Node->Link); diff --git a/ShellPkg/Application/Shell/ShellManParser.c b/ShellPkg/Application/Shell/ShellManParser.c index 525c9343bd..0cbfbaabba 100644 --- a/ShellPkg/Application/Shell/ShellManParser.c +++ b/ShellPkg/Application/Shell/ShellManParser.c @@ -1,7 +1,7 @@ /** @file Provides interface to shell MAN file parser. - Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
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; } diff --git a/ShellPkg/Application/Shell/ShellParametersProtocol.c b/ShellPkg/Application/Shell/ShellParametersProtocol.c index e052d24d5d..ff149009b1 100644 --- a/ShellPkg/Application/Shell/ShellParametersProtocol.c +++ b/ShellPkg/Application/Shell/ShellParametersProtocol.c @@ -35,8 +35,9 @@ VOID EFIAPI GetNextParameter( - CHAR16 **Walker, - CHAR16 **TempParameter + IN OUT CHAR16 **Walker, + IN OUT CHAR16 **TempParameter, + IN CONST UINTN Length ) { CHAR16 *NextDelim; @@ -82,7 +83,7 @@ GetNextParameter( // // found "" // - StrCpy(*TempParameter, L""); + *(*TempParameter) = CHAR_NULL; *Walker = NextDelim + 1; } else if (NextDelim != NULL) { @@ -95,7 +96,7 @@ GetNextParameter( // // last one... someone forgot the training quote! // - StrCpy(*TempParameter, *Walker); + StrnCpy(*TempParameter, *Walker, Length/sizeof(CHAR16) - 1); *Walker = NULL; } for (TempLoc = *TempParameter ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) { @@ -117,7 +118,7 @@ GetNextParameter( // // last one. // - StrCpy(*TempParameter, *Walker); + StrnCpy(*TempParameter, *Walker, Length/sizeof(CHAR16) - 1); *Walker = NULL; } for (NextDelim = *TempParameter ; NextDelim != NULL && *NextDelim != CHAR_NULL ; NextDelim++) { @@ -181,17 +182,10 @@ ParseCommandLineToArgs( for ( Count = 0 , Walker = (CHAR16*)CommandLine ; Walker != NULL && *Walker != CHAR_NULL - ; GetNextParameter(&Walker, &TempParameter) + ; GetNextParameter(&Walker, &TempParameter, Size) , Count++ ); -/* Count = 0; - Walker = (CHAR16*)CommandLine; - while(Walker != NULL) { - GetNextParameter(&Walker, &TempParameter); - Count++; - } -*/ // // lets allocate the pointer array // @@ -205,10 +199,12 @@ ParseCommandLineToArgs( Walker = (CHAR16*)CommandLine; while(Walker != NULL && *Walker != CHAR_NULL) { SetMem16(TempParameter, Size, CHAR_NULL); - GetNextParameter(&Walker, &TempParameter); - NewParam = AllocateZeroPool(StrSize(TempParameter)); - ASSERT(NewParam != NULL); - StrCpy(NewParam, TempParameter); + GetNextParameter(&Walker, &TempParameter, Size); + NewParam = AllocateCopyPool(StrSize(TempParameter), TempParameter); + if (NewParam == NULL){ + SHELL_FREE_NON_NULL(TempParameter); + return (EFI_OUT_OF_RESOURCES); + } ((CHAR16**)(*Argv))[(*Argc)] = NewParam; (*Argc)++; } @@ -976,7 +972,7 @@ UpdateStdInStdOutStdErr( // // re-populate the string to support any filenames that were in quotes. // - StrCpy(CommandLineCopy, NewCommandLine); + StrnCpy(CommandLineCopy, NewCommandLine, StrLen(NewCommandLine)); if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy) && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine)) diff --git a/ShellPkg/Application/Shell/ShellParametersProtocol.h b/ShellPkg/Application/Shell/ShellParametersProtocol.h index e2d28816d2..15b76b7691 100644 --- a/ShellPkg/Application/Shell/ShellParametersProtocol.h +++ b/ShellPkg/Application/Shell/ShellParametersProtocol.h @@ -190,13 +190,15 @@ ParseCommandLineToArgs( @param[in, out] Walker pointer to string of command line. Adjusted to reminaing command line on return @param[in, out] TempParameter pointer to string of command line item extracted. + @param[in] Length Length of (*TempParameter) in bytes **/ VOID EFIAPI GetNextParameter( - CHAR16 **Walker, - CHAR16 **TempParameter + IN OUT CHAR16 **Walker, + IN OUT CHAR16 **TempParameter, + IN CONST UINTN Length ); #endif //_SHELL_PARAMETERS_PROTOCOL_PROVIDER_HEADER_ diff --git a/ShellPkg/Application/Shell/ShellProtocol.c b/ShellPkg/Application/Shell/ShellProtocol.c index 9b7b893fe3..a44afde436 100644 --- a/ShellPkg/Application/Shell/ShellProtocol.c +++ b/ShellPkg/Application/Shell/ShellProtocol.c @@ -530,18 +530,17 @@ EfiShellGetDevicePathFromFilePath( if (Cwd == NULL) { return (NULL); } - Size = StrSize(Cwd); - Size += StrSize(Path); + Size = StrSize(Cwd) + StrSize(Path) - sizeof(CHAR16); NewPath = AllocateZeroPool(Size); if (NewPath == NULL) { return (NULL); } - StrCpy(NewPath, Cwd); + StrnCpy(NewPath, Cwd, Size/sizeof(CHAR16)-1); if (*Path == L'\\') { Path++; while (PathRemoveLastItem(NewPath)) ; } - StrCat(NewPath, Path); + StrnCat(NewPath, Path, Size/sizeof(CHAR16) - 1 - StrLen(NewPath)); DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath); FreePool(NewPath); return (DevicePathForReturn); @@ -1846,10 +1845,9 @@ InternalDuplicateShellFileInfo( if (NewNode == NULL) { return (NULL); } - NewNode->FullName = AllocateZeroPool(StrSize(Node->FullName)); - - NewNode->FileName = AllocateZeroPool(StrSize(Node->FileName)); - NewNode->Info = AllocateZeroPool((UINTN)Node->Info->Size); + NewNode->FullName = AllocateCopyPool(StrSize(Node->FullName), Node->FullName); + NewNode->FileName = AllocateCopyPool(StrSize(Node->FileName), Node->FileName); + NewNode->Info = AllocateCopyPool((UINTN)Node->Info->Size, Node->Info); if ( NewNode->FullName == NULL || NewNode->FileName == NULL || NewNode->Info == NULL @@ -1865,9 +1863,6 @@ InternalDuplicateShellFileInfo( if (!Save) { Node->Handle = NULL; } - StrCpy((CHAR16*)NewNode->FullName, Node->FullName); - StrCpy((CHAR16*)NewNode->FileName, Node->FileName); - CopyMem(NewNode->Info, Node->Info, (UINTN)Node->Info->Size); return((EFI_SHELL_FILE_INFO*)NewNode); } @@ -2055,7 +2050,7 @@ EfiShellFindFilesInDir( } SHELL_FREE_NON_NULL(BasePath); return(Status); -} + } /** Get the GUID value from a human readable name. @@ -2313,8 +2308,8 @@ ShellSearchHandle( if (NewFullName == NULL) { Status = EFI_OUT_OF_RESOURCES; } else { - StrCpy(NewFullName, MapName); - StrCat(NewFullName, ShellInfoNode->FullName+1); + StrnCpy(NewFullName, MapName, Size/sizeof(CHAR16)-1); + StrnCat(NewFullName, ShellInfoNode->FullName+1, (Size/sizeof(CHAR16))-StrLen(NewFullName)-1); FreePool((VOID*)ShellInfoNode->FullName); ShellInfoNode->FullName = NewFullName; } @@ -2437,11 +2432,10 @@ EfiShellFindFiles( RootDevicePath = NULL; RootFileHandle = NULL; MapName = NULL; - PatternCopy = AllocateZeroPool(StrSize(FilePattern)); + PatternCopy = AllocateCopyPool(StrSize(FilePattern), FilePattern); if (PatternCopy == NULL) { return (EFI_OUT_OF_RESOURCES); } - StrCpy(PatternCopy, FilePattern); PatternCopy = PathCleanUpDirectories(PatternCopy); @@ -2645,7 +2639,7 @@ EfiShellGetEnvEx( ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link) ){ ASSERT(Node->Key != NULL); - StrCpy(CurrentWriteLocation, Node->Key); + StrnCpy(CurrentWriteLocation, Node->Key, (Size)/sizeof(CHAR16) - (CurrentWriteLocation - ((CHAR16*)Buffer)) - 1); CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1; } @@ -2669,7 +2663,6 @@ EfiShellGetEnvEx( // Allocate the space and recall the get function // Buffer = AllocateZeroPool(Size); - ASSERT(Buffer != NULL); Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer); } // -- 2.39.2