]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Refactor string manipulation
authorJaben Carsey <jaben.carsey@intel.com>
Tue, 2 Sep 2014 20:17:38 +0000 (20:17 +0000)
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 2 Sep 2014 20:17:38 +0000 (20:17 +0000)
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

ShellPkg/Application/Shell/FileHandleWrappers.c
ShellPkg/Application/Shell/Shell.c
ShellPkg/Application/Shell/ShellEnvVar.c
ShellPkg/Application/Shell/ShellManParser.c
ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg/Application/Shell/ShellParametersProtocol.h
ShellPkg/Application/Shell/ShellProtocol.c

index 59c1ee55eeaec7ca8a1d7ca3ce73c6ef65a59eee..be786bb2b53bd4e131dcd5cac615a627839b42cc 100644 (file)
@@ -509,21 +509,20 @@ FileInterfaceStdInRead(
         if (StrStr(CurrentString + TabPos, L":") == NULL) {\r
           Cwd = ShellInfoObject.NewEfiShellProtocol->GetCurDir(NULL);\r
           if (Cwd != NULL) {\r
-            StrCpy(TabStr, Cwd);\r
+            StrnCpy(TabStr, Cwd, (*BufferSize)/sizeof(CHAR16) - 1);\r
             if (TabStr[StrLen(TabStr)-1] == L'\\' && *(CurrentString + TabPos) == L'\\' ) {\r
               TabStr[StrLen(TabStr)-1] = CHAR_NULL;\r
             }\r
             StrnCat(TabStr, CurrentString + TabPos, (StringLen - TabPos) * sizeof (CHAR16));\r
           } else {\r
-            StrCpy(TabStr, L"");\r
+            *TabStr = CHAR_NULL;\r
             StrnCat(TabStr, CurrentString + TabPos, (StringLen - TabPos) * sizeof (CHAR16));\r
           }\r
         } else {\r
-          StrCpy(TabStr, CurrentString + TabPos);\r
+          StrnCpy(TabStr, CurrentString + TabPos, (*BufferSize)/sizeof(CHAR16) - 1);\r
         }\r
-        StrCat(TabStr, L"*");\r
+        StrnCat(TabStr, L"*", (*BufferSize)/sizeof(CHAR16) - 1 - StrLen(TabStr));\r
         FoundFileList = NULL;\r
-//        TabStr = PathCleanUpDirectories(TabStr);\r
         Status  = ShellInfoObject.NewEfiShellProtocol->FindFiles(TabStr, &FoundFileList);\r
         for ( TempStr = CurrentString\r
             ; *TempStr == L' '\r
@@ -1168,7 +1167,7 @@ CreateFileInterfaceEnv(
   EnvFileInterface->Delete      = FileInterfaceEnvDelete;\r
   EnvFileInterface->Read        = FileInterfaceEnvRead;\r
 \r
-  StrCpy(EnvFileInterface->Name, EnvName);\r
+  StrnCpy(EnvFileInterface->Name, EnvName, StrLen(EnvName));\r
 \r
   //\r
   // Assign the different members for Volatile and Non-Volatile variables\r
index 1d3064531dc4559cce9d4ab7f131e702b3bae8fd..976c314cd3d4277c40b20c4e41b992c656b64aa8 100644 (file)
@@ -937,7 +937,7 @@ ProcessCommandLine(
         continue;\r
       }\r
 \r
-      ShellInfoObject.ShellInitSettings.FileName = AllocateZeroPool(StrSize(CurrentArg));\r
+      ShellInfoObject.ShellInitSettings.FileName = AllocateCopyPool(StrSize(CurrentArg), CurrentArg);\r
       if (ShellInfoObject.ShellInitSettings.FileName == NULL) {\r
         return (EFI_OUT_OF_RESOURCES);\r
       }\r
@@ -945,8 +945,6 @@ ProcessCommandLine(
       // We found `file-name`.\r
       //\r
       ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;\r
-\r
-      StrCpy (ShellInfoObject.ShellInitSettings.FileName, CurrentArg);\r
       LoopVar++;\r
 \r
       // Add `file-name-options`\r
@@ -1027,10 +1025,10 @@ DoStartupScript(
     if (FileStringPath == NULL) {\r
       return (EFI_OUT_OF_RESOURCES);\r
     }\r
-    StrCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName);\r
+    StrnCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName, NewSize/sizeof(CHAR16) -1);\r
     if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {\r
-      StrCat(FileStringPath, L" ");\r
-      StrCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions);\r
+      StrnCat(FileStringPath, L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r
+      StrnCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);\r
     }\r
     Status = RunCommand(FileStringPath, ExitStatus);\r
     FreePool(FileStringPath);\r
@@ -1247,9 +1245,8 @@ AddLineToCommandHistory(
 \r
   Node = AllocateZeroPool(sizeof(BUFFER_LIST));\r
   ASSERT(Node != NULL);\r
-  Node->Buffer = AllocateZeroPool(StrSize(Buffer));\r
+  Node->Buffer = AllocateCopyPool(StrSize(Buffer), Buffer);\r
   ASSERT(Node->Buffer != NULL);\r
-  StrCpy(Node->Buffer, Buffer);\r
 \r
   InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);\r
 }\r
@@ -1280,11 +1277,10 @@ ShellConvertAlias(
     return (EFI_SUCCESS);\r
   }\r
   FreePool(*CommandString);\r
-  *CommandString = AllocateZeroPool(StrSize(NewString));\r
+  *CommandString = AllocateCopyPool(StrSize(NewString), NewString);\r
   if (*CommandString == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(*CommandString, NewString);\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -1477,7 +1473,7 @@ ShellConvertVariables (
   //\r
   // now do the replacements...\r
   //\r
-  NewCommandLine1 = AllocateZeroPool(NewSize);\r
+  NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine);\r
   NewCommandLine2 = AllocateZeroPool(NewSize);\r
   ItemTemp        = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));\r
   if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {\r
@@ -1486,16 +1482,15 @@ ShellConvertVariables (
     SHELL_FREE_NON_NULL(ItemTemp);\r
     return (NULL);\r
   }\r
-  StrCpy(NewCommandLine1, OriginalCommandLine);\r
   for (MasterEnvList = EfiShellGetEnv(NULL)\r
-    ;  MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL\r
+    ;  MasterEnvList != NULL && *MasterEnvList != CHAR_NULL\r
     ;  MasterEnvList += StrLen(MasterEnvList) + 1\r
    ){\r
-    StrCpy(ItemTemp, L"%");\r
-    StrCat(ItemTemp, MasterEnvList);\r
-    StrCat(ItemTemp, L"%");\r
+    *ItemTemp = L'%';\r
+    StrnCat(ItemTemp, MasterEnvList, ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16))-1 - StrLen(ItemTemp));\r
+    StrnCat(ItemTemp, L"%", ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16))-1 - StrLen(ItemTemp));\r
     ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);\r
-    StrCpy(NewCommandLine1, NewCommandLine2);\r
+    StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1);\r
   }\r
   if (CurrentScriptFile != NULL) {\r
     for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)\r
@@ -1503,7 +1498,7 @@ ShellConvertVariables (
       ;  AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)\r
    ){\r
     ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);\r
-    StrCpy(NewCommandLine1, NewCommandLine2);\r
+    StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1);\r
     }\r
 \r
     //\r
@@ -1516,7 +1511,7 @@ ShellConvertVariables (
   // Now cleanup any straggler intentionally ignored "%" characters\r
   //\r
   ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);\r
-  StrCpy(NewCommandLine1, NewCommandLine2);\r
+  StrnCpy(NewCommandLine1, NewCommandLine2, NewSize/sizeof(CHAR16)-1);\r
   \r
   FreePool(NewCommandLine2);\r
   FreePool(ItemTemp);\r
@@ -1850,7 +1845,7 @@ IsValidSplit(
       return (EFI_OUT_OF_RESOURCES);\r
     }\r
     TempWalker = (CHAR16*)Temp;\r
-    GetNextParameter(&TempWalker, &FirstParameter);\r
+    GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine));\r
 \r
     if (GetOperationType(FirstParameter) == Unknown_Invalid) {\r
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
@@ -2018,7 +2013,7 @@ DoHelpUpdate(
   Walker = *CmdLine;\r
   while(Walker != NULL && *Walker != CHAR_NULL) {\r
     LastWalker = Walker;\r
-    GetNextParameter(&Walker, &CurrentParameter);\r
+    GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine));\r
     if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {\r
       LastWalker[0] = L' ';\r
       LastWalker[1] = L' ';\r
@@ -2027,8 +2022,12 @@ DoHelpUpdate(
         Status = EFI_OUT_OF_RESOURCES;\r
         break;\r
       }\r
-      StrCpy(NewCommandLine, L"help ");\r
-      StrCat(NewCommandLine, *CmdLine);\r
+\r
+      //\r
+      // We know the space is sufficient since we just calculated it.\r
+      //\r
+      StrnCpy(NewCommandLine, L"help ", 5);\r
+      StrnCat(NewCommandLine, *CmdLine, StrLen(*CmdLine));\r
       SHELL_FREE_NON_NULL(*CmdLine);\r
       *CmdLine = NewCommandLine;\r
       break;\r
@@ -2507,7 +2506,7 @@ RunCommand(
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
   TempWalker = CleanOriginal;\r
-  GetNextParameter(&TempWalker, &FirstParameter);\r
+  GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal));\r
 \r
   //\r
   // Depending on the first parameter we change the behavior\r
@@ -2703,7 +2702,7 @@ RunScriptFileHandle (
       ; // conditional increment in the body of the loop\r
   ){\r
     ASSERT(CommandLine2 != NULL);\r
-    StrCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl);\r
+    StrnCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1);\r
 \r
     //\r
     // NULL out comments\r
@@ -2722,7 +2721,7 @@ RunScriptFileHandle (
       //\r
       // Due to variability in starting the find and replace action we need to have both buffers the same.\r
       //\r
-      StrCpy(CommandLine, CommandLine2);\r
+      StrnCpy(CommandLine, CommandLine2, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1);\r
 \r
       //\r
       // Remove the %0 to %9 from the command line (if we have some arguments)\r
@@ -2774,7 +2773,7 @@ RunScriptFileHandle (
       Status = ShellCopySearchAndReplace(CommandLine,  CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", L"\"\"", FALSE, FALSE);\r
       Status = ShellCopySearchAndReplace(CommandLine2,  CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", L"\"\"", FALSE, FALSE);\r
 \r
-      StrCpy(CommandLine2, CommandLine);\r
+      StrnCpy(CommandLine2, CommandLine, PcdGet16(PcdShellPrintBufferSize)/sizeof(CHAR16)-1);\r
 \r
       LastCommand = NewScriptFile->CurrentCommand;\r
 \r
index f40a867cc7ae564a6f4e607e36e4ed56205fab5f..b94711142af6c3ff4532ed1b6127e2b70eea9e31 100644 (file)
@@ -152,7 +152,7 @@ GetEnvironmentVariableList(
   if (VariableName == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(VariableName, L"");\r
+  *VariableName = CHAR_NULL;\r
 \r
   while (!EFI_ERROR(Status)) {\r
     NameSize = (UINTN)MaxVarSize;\r
@@ -178,13 +178,12 @@ GetEnvironmentVariableList(
           }\r
         }\r
         if (!EFI_ERROR(Status) && VarList != NULL) {\r
-          VarList->Key = AllocateZeroPool(StrSize(VariableName));\r
+          VarList->Key = AllocateCopyPool(StrSize(VariableName), VariableName);\r
           if (VarList->Key == NULL) {\r
             SHELL_FREE_NON_NULL(VarList->Val);\r
             SHELL_FREE_NON_NULL(VarList);\r
             Status = EFI_OUT_OF_RESOURCES;\r
           } else {\r
-            StrCpy(VarList->Key, VariableName);\r
             InsertTailList(ListHead, &VarList->Link);\r
           }\r
         }\r
@@ -286,7 +285,6 @@ SetEnvironmentVariables(
   UINTN         CurrentCount;\r
   ENV_VAR_LIST  *VarList;\r
   ENV_VAR_LIST  *Node;\r
-  UINTN         NewSize;\r
 \r
   VarList = NULL;\r
 \r
@@ -307,20 +305,44 @@ SetEnvironmentVariables(
     }\r
     ASSERT(StrStr(CurrentString, L"=") != NULL);\r
     Node = AllocateZeroPool(sizeof(ENV_VAR_LIST));\r
-    ASSERT(Node != NULL);\r
+    if (Node == NULL) {\r
+      SetEnvironmentVariableList(&VarList->Link);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+\r
     Node->Key = AllocateZeroPool((StrStr(CurrentString, L"=") - CurrentString + 1) * sizeof(CHAR16));\r
-    ASSERT(Node->Key != NULL);\r
+    if (Node->Key == NULL) {\r
+      SHELL_FREE_NON_NULL(Node);\r
+      SetEnvironmentVariableList(&VarList->Link);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+\r
+    //\r
+    // Copy the string into the Key, leaving the last character allocated as NULL to terminate\r
+    //\r
     StrnCpy(Node->Key, CurrentString, StrStr(CurrentString, L"=") - CurrentString);\r
-    NewSize = StrSize(CurrentString);\r
-    NewSize -= StrLen(Node->Key) - 1;\r
-    Node->Val = AllocateZeroPool(NewSize);\r
-    ASSERT(Node->Val != NULL);\r
-    StrCpy(Node->Val, CurrentString + StrLen(Node->Key) + 1);\r
+\r
+    //\r
+    // ValueSize = TotalSize - already removed size - size for '=' + size for terminator (the last 2 items cancel each other)\r
+    //\r
+    Node->Val = AllocateCopyPool(StrSize(CurrentString) - StrSize(Node->Key), CurrentString + StrLen(Node->Key) + 1);\r
+    if (Node->Val == NULL) {\r
+      SHELL_FREE_NON_NULL(Node->Key);\r
+      SHELL_FREE_NON_NULL(Node);\r
+      SetEnvironmentVariableList(&VarList->Link);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+\r
     Node->Atts = EFI_VARIABLE_BOOTSERVICE_ACCESS;\r
 \r
     if (VarList == NULL) {\r
       VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));\r
-      ASSERT(VarList != NULL);\r
+      if (VarList == NULL) {\r
+        SHELL_FREE_NON_NULL(Node->Key);\r
+        SHELL_FREE_NON_NULL(Node->Val);\r
+        SHELL_FREE_NON_NULL(Node);\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
       InitializeListHead(&VarList->Link);\r
     }\r
     InsertTailList(&VarList->Link, &Node->Link);\r
index 525c9343bd96c9481ab78287eb07d74404b6e9d2..0cbfbaabbaf4199f7a97b654d9b83d2f6e353f9f 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Provides interface to shell MAN file parser.\r
 \r
-  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -39,15 +39,12 @@ GetManFileName(
   // Fix the file name\r
   //\r
   if (StrnCmp(ManFileName+StrLen(ManFileName)-4, L".man", 4)==0) {\r
-    Buffer = AllocateZeroPool(StrSize(ManFileName));\r
-    if (Buffer != NULL) {\r
-      StrCpy(Buffer, ManFileName);\r
-    }\r
+    Buffer = AllocateCopyPool(StrSize(ManFileName), ManFileName);\r
   } else {\r
     Buffer = AllocateZeroPool(StrSize(ManFileName) + 4*sizeof(CHAR16));\r
     if (Buffer != NULL) {\r
-      StrCpy(Buffer, ManFileName);\r
-      StrCat(Buffer, L".man");\r
+      StrnCpy(Buffer, ManFileName, StrLen(ManFileName));\r
+      StrnCat(Buffer, L".man", 4);\r
     }\r
   }\r
   return (Buffer);\r
@@ -374,6 +371,9 @@ ManBufferFindTitleSection(
   CHAR16        *TitleString;\r
   CHAR16        *TitleEnd;\r
   CHAR16        *CurrentLocation;\r
+  UINTN         TitleLength;\r
+  CONST CHAR16  StartString[] = L".TH ";\r
+  CONST CHAR16  EndString[]   = L" 0 ";\r
 \r
   if ( Buffer     == NULL\r
     || Command    == NULL\r
@@ -384,13 +384,17 @@ ManBufferFindTitleSection(
 \r
   Status    = EFI_SUCCESS;\r
 \r
-  TitleString = AllocateZeroPool((7*sizeof(CHAR16)) + StrSize(Command));\r
+  //\r
+  // more characters for StartString and EndString\r
+  //\r
+  TitleLength = StrSize(Command) + (StrLen(StartString) + StrLen(EndString)) * sizeof(CHAR16);\r
+  TitleString = AllocateZeroPool(TitleLength);\r
   if (TitleString == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(TitleString, L".TH ");\r
-  StrCat(TitleString, Command);\r
-  StrCat(TitleString, L" 0 ");\r
+  StrnCpy(TitleString, StartString, TitleLength/sizeof(CHAR16) - 1);\r
+  StrnCat(TitleString, Command,     TitleLength/sizeof(CHAR16) - 1 - StrLen(TitleString));\r
+  StrnCat(TitleString, EndString,   TitleLength/sizeof(CHAR16) - 1 - StrLen(TitleString));\r
 \r
   CurrentLocation = StrStr(*Buffer, TitleString);\r
   if (CurrentLocation == NULL){\r
@@ -467,6 +471,7 @@ ManFileFindTitleSection(
   CHAR16      *TitleEnd;\r
   UINTN       TitleLen;\r
   BOOLEAN     Found;\r
+  UINTN       TitleSize;\r
 \r
   if ( Handle     == NULL\r
     || Command    == NULL\r
@@ -484,13 +489,14 @@ ManFileFindTitleSection(
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
-  TitleString = AllocateZeroPool((4*sizeof(CHAR16)) + StrSize(Command));\r
+  TitleSize = (4*sizeof(CHAR16)) + StrSize(Command);\r
+  TitleString = AllocateZeroPool(TitleSize);\r
   if (TitleString == NULL) {\r
     FreePool(ReadLine);\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(TitleString, L".TH ");\r
-  StrCat(TitleString, Command);\r
+  StrnCpy(TitleString, L".TH ", TitleSize/sizeof(CHAR16) - 1);\r
+  StrnCat(TitleString, Command, TitleSize/sizeof(CHAR16) - 1 - StrLen(TitleString));\r
 \r
   TitleLen = StrLen(TitleString);\r
   for (;!ShellFileHandleEof(Handle);Size = 1024) {\r
@@ -526,7 +532,7 @@ ManFileFindTitleSection(
           Status = EFI_OUT_OF_RESOURCES;\r
           break;\r
         }\r
-        StrCpy(*BriefDesc, TitleEnd);\r
+        StrnCpy(*BriefDesc, TitleEnd, (*BriefSize)/sizeof(CHAR16) - 1);\r
       }\r
       break;\r
     }\r
index e052d24d5d336cf08a0176c42b4697768f6fc9b8..ff149009b14cda223df7d93e1c774da916129aa9 100644 (file)
@@ -35,8 +35,9 @@
 VOID\r
 EFIAPI\r
 GetNextParameter(\r
-  CHAR16 **Walker,\r
-  CHAR16 **TempParameter\r
+  IN OUT CHAR16   **Walker,\r
+  IN OUT CHAR16   **TempParameter,\r
+  IN CONST UINTN  Length\r
   )\r
 {\r
   CHAR16 *NextDelim;\r
@@ -82,7 +83,7 @@ GetNextParameter(
       //\r
       // found ""\r
       //\r
-      StrCpy(*TempParameter, L"");\r
+      *(*TempParameter) = CHAR_NULL;\r
       *Walker = NextDelim + 1;\r
     } else if (NextDelim != NULL) {\r
 \r
@@ -95,7 +96,7 @@ GetNextParameter(
       //\r
       // last one... someone forgot the training quote!\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
+      StrnCpy(*TempParameter, *Walker, Length/sizeof(CHAR16) - 1);\r
       *Walker = NULL;\r
     }\r
     for (TempLoc = *TempParameter ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
@@ -117,7 +118,7 @@ GetNextParameter(
       //\r
       // last one.\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
+      StrnCpy(*TempParameter, *Walker, Length/sizeof(CHAR16) - 1);\r
       *Walker = NULL;\r
     }\r
     for (NextDelim = *TempParameter ; NextDelim != NULL && *NextDelim != CHAR_NULL ; NextDelim++) {\r
@@ -181,17 +182,10 @@ ParseCommandLineToArgs(
   for ( Count = 0\r
       , Walker = (CHAR16*)CommandLine\r
       ; Walker != NULL && *Walker != CHAR_NULL\r
-      ; GetNextParameter(&Walker, &TempParameter)\r
+      ; GetNextParameter(&Walker, &TempParameter, Size)\r
       , Count++\r
      );\r
 \r
-/*  Count = 0;\r
-  Walker = (CHAR16*)CommandLine;\r
-  while(Walker != NULL) {\r
-    GetNextParameter(&Walker, &TempParameter);\r
-    Count++;\r
-  }\r
-*/\r
   //\r
   // lets allocate the pointer array\r
   //\r
@@ -205,10 +199,12 @@ ParseCommandLineToArgs(
   Walker = (CHAR16*)CommandLine;\r
   while(Walker != NULL && *Walker != CHAR_NULL) {\r
     SetMem16(TempParameter, Size, CHAR_NULL);\r
-    GetNextParameter(&Walker, &TempParameter);\r
-    NewParam = AllocateZeroPool(StrSize(TempParameter));\r
-    ASSERT(NewParam != NULL);\r
-    StrCpy(NewParam, TempParameter);\r
+    GetNextParameter(&Walker, &TempParameter, Size);\r
+    NewParam = AllocateCopyPool(StrSize(TempParameter), TempParameter);\r
+    if (NewParam == NULL){\r
+      SHELL_FREE_NON_NULL(TempParameter);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
     ((CHAR16**)(*Argv))[(*Argc)] = NewParam;\r
     (*Argc)++;\r
   }\r
@@ -976,7 +972,7 @@ UpdateStdInStdOutStdErr(
   //\r
   // re-populate the string to support any filenames that were in quotes.\r
   //\r
-  StrCpy(CommandLineCopy, NewCommandLine);\r
+  StrnCpy(CommandLineCopy, NewCommandLine, StrLen(NewCommandLine));\r
 \r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
     && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
index e2d28816d2c7c738330c0374c818c99113537145..15b76b76918605d03e6efc61beaa8e039cd485e4 100644 (file)
@@ -190,13 +190,15 @@ ParseCommandLineToArgs(
   @param[in, out] Walker        pointer to string of command line.  Adjusted to\r
                                 reminaing command line on return\r
   @param[in, out] TempParameter pointer to string of command line item extracted.\r
+  @param[in]      Length        Length of (*TempParameter) in bytes\r
 \r
 **/\r
 VOID\r
 EFIAPI\r
 GetNextParameter(\r
-  CHAR16 **Walker,\r
-  CHAR16 **TempParameter\r
+  IN OUT CHAR16   **Walker,\r
+  IN OUT CHAR16   **TempParameter,\r
+  IN CONST UINTN  Length\r
   );\r
 \r
 #endif //_SHELL_PARAMETERS_PROTOCOL_PROVIDER_HEADER_\r
index 9b7b893fe3dddfeaf8c93928bed41975f9ce360a..a44afde436ce92672688b4bf9b7285d275987304 100644 (file)
@@ -530,18 +530,17 @@ EfiShellGetDevicePathFromFilePath(
     if (Cwd == NULL) {\r
       return (NULL);\r
     }\r
-    Size = StrSize(Cwd);\r
-    Size += StrSize(Path);\r
+    Size = StrSize(Cwd) + StrSize(Path) - sizeof(CHAR16);\r
     NewPath = AllocateZeroPool(Size);\r
     if (NewPath == NULL) {\r
       return (NULL);\r
     }\r
-    StrCpy(NewPath, Cwd);\r
+    StrnCpy(NewPath, Cwd, Size/sizeof(CHAR16)-1);\r
     if (*Path == L'\\') {\r
       Path++;\r
       while (PathRemoveLastItem(NewPath)) ;\r
     }\r
-    StrCat(NewPath, Path);\r
+    StrnCat(NewPath, Path, Size/sizeof(CHAR16) - 1 - StrLen(NewPath));\r
     DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath);\r
     FreePool(NewPath);\r
     return (DevicePathForReturn);\r
@@ -1846,10 +1845,9 @@ InternalDuplicateShellFileInfo(
   if (NewNode == NULL) {\r
     return (NULL);\r
   }\r
-  NewNode->FullName = AllocateZeroPool(StrSize(Node->FullName));\r
-\r
-  NewNode->FileName = AllocateZeroPool(StrSize(Node->FileName));\r
-  NewNode->Info     = AllocateZeroPool((UINTN)Node->Info->Size);\r
+  NewNode->FullName = AllocateCopyPool(StrSize(Node->FullName), Node->FullName);\r
+  NewNode->FileName = AllocateCopyPool(StrSize(Node->FileName), Node->FileName);\r
+  NewNode->Info     = AllocateCopyPool((UINTN)Node->Info->Size, Node->Info);\r
   if ( NewNode->FullName == NULL\r
     || NewNode->FileName == NULL\r
     || NewNode->Info == NULL\r
@@ -1865,9 +1863,6 @@ InternalDuplicateShellFileInfo(
   if (!Save) {\r
     Node->Handle = NULL;\r
   }\r
-  StrCpy((CHAR16*)NewNode->FullName, Node->FullName);\r
-  StrCpy((CHAR16*)NewNode->FileName, Node->FileName);\r
-  CopyMem(NewNode->Info, Node->Info, (UINTN)Node->Info->Size);\r
 \r
   return((EFI_SHELL_FILE_INFO*)NewNode);\r
 }\r
@@ -2055,7 +2050,7 @@ EfiShellFindFilesInDir(
   }\r
   SHELL_FREE_NON_NULL(BasePath);\r
   return(Status);\r
-}\r
+  }\r
 \r
 /**\r
   Get the GUID value from a human readable name.\r
@@ -2313,8 +2308,8 @@ ShellSearchHandle(
             if (NewFullName == NULL) {\r
               Status = EFI_OUT_OF_RESOURCES;\r
             } else {\r
-              StrCpy(NewFullName, MapName);\r
-              StrCat(NewFullName, ShellInfoNode->FullName+1);\r
+              StrnCpy(NewFullName, MapName, Size/sizeof(CHAR16)-1);\r
+              StrnCat(NewFullName, ShellInfoNode->FullName+1, (Size/sizeof(CHAR16))-StrLen(NewFullName)-1);\r
               FreePool((VOID*)ShellInfoNode->FullName);\r
               ShellInfoNode->FullName = NewFullName;\r
             }\r
@@ -2437,11 +2432,10 @@ EfiShellFindFiles(
   RootDevicePath = NULL;\r
   RootFileHandle = NULL;\r
   MapName        = NULL;\r
-  PatternCopy = AllocateZeroPool(StrSize(FilePattern));\r
+  PatternCopy = AllocateCopyPool(StrSize(FilePattern), FilePattern);\r
   if (PatternCopy == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(PatternCopy, FilePattern);\r
 \r
   PatternCopy = PathCleanUpDirectories(PatternCopy);\r
 \r
@@ -2645,7 +2639,7 @@ EfiShellGetEnvEx(
       ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)\r
      ){\r
       ASSERT(Node->Key != NULL);\r
-      StrCpy(CurrentWriteLocation, Node->Key);\r
+      StrnCpy(CurrentWriteLocation, Node->Key,  (Size)/sizeof(CHAR16) - (CurrentWriteLocation - ((CHAR16*)Buffer)) - 1);\r
       CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;\r
     }\r
 \r
@@ -2669,7 +2663,6 @@ EfiShellGetEnvEx(
       // Allocate the space and recall the get function\r
       //\r
       Buffer = AllocateZeroPool(Size);\r
-      ASSERT(Buffer != NULL);\r
       Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
     }\r
     //\r