]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel1CommandsLib/For.c
ShellPkg/for: Fix potential null pointer deference
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel1CommandsLib / For.c
index 4fdbdd051d4c48b439b1b74c4c8b8fe37c79d1cb..9824977149193ebab07c8c1420e80a991340c67b 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Main file for endfor and for shell level 1 functions.\r
 \r
-  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
+  Copyright (c) 2009 - 2018, 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
@@ -24,7 +25,6 @@
   @retval FALSE   The number is not valid.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 ShellIsValidForNumber (\r
   IN CONST CHAR16 *Number\r
   )\r
@@ -75,12 +75,12 @@ ShellCommandRunEndFor (
   ASSERT_EFI_ERROR(Status);\r
 \r
   if (!gEfiShellProtocol->BatchIsActive()) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"EndFor");\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"endfor");\r
     return (SHELL_UNSUPPORTED);\r
   }\r
 \r
   if (gEfiShellParametersProtocol->Argc > 1) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle, L"endfor");\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
@@ -128,7 +128,6 @@ typedef struct {
   @retval EFI_OUT_OF_RESOURCES  There was not enough free memory.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 InternalUpdateAliasOnList(\r
   IN CONST CHAR16       *Alias,\r
   IN CONST CHAR16       *CommandString,\r
@@ -184,7 +183,6 @@ InternalUpdateAliasOnList(
   @retval FALSE                 The alias is not on the list.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 InternalIsAliasOnList(\r
   IN CONST CHAR16       *Alias,\r
   IN CONST LIST_ENTRY   *List\r
@@ -220,7 +218,6 @@ InternalIsAliasOnList(
   @param[in, out] List           The list to search.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 InternalRemoveAliasFromList(\r
   IN CONST CHAR16       *Alias,\r
   IN OUT LIST_ENTRY     *List\r
@@ -263,7 +260,6 @@ InternalRemoveAliasFromList(
   @retval (UINTN)(-1) An error ocurred.\r
 **/\r
 UINTN\r
-EFIAPI\r
 ReturnUintn(\r
   IN CONST CHAR16 *String\r
   )\r
@@ -294,6 +290,7 @@ ShellCommandRunFor (
   SCRIPT_FILE         *CurrentScriptFile;\r
   CHAR16              *ArgSet;\r
   CHAR16              *ArgSetWalker;\r
+  CHAR16              *Parameter;\r
   UINTN               ArgSize;\r
   UINTN               LoopVar;\r
   SHELL_FOR_INFO      *Info;\r
@@ -309,6 +306,7 @@ ShellCommandRunFor (
   ShellStatus         = SHELL_SUCCESS;\r
   ArgSetWalker        = NULL;\r
   TempString          = NULL;\r
+  Parameter           = NULL;\r
   FirstPass           = FALSE;\r
 \r
   //\r
@@ -321,12 +319,12 @@ ShellCommandRunFor (
   ASSERT_EFI_ERROR(Status);\r
 \r
   if (!gEfiShellProtocol->BatchIsActive()) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"For");\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"for");\r
     return (SHELL_UNSUPPORTED);\r
   }\r
 \r
   if (gEfiShellParametersProtocol->Argc < 4) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel1HiiHandle);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel1HiiHandle, L"for");\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
@@ -391,9 +389,15 @@ ShellCommandRunFor (
             ShellCloseFileMetaArg(&FileList);\r
           }\r
         } else {\r
-          ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" \"", 0);\r
-          ArgSet = StrnCatGrow(&ArgSet, &ArgSize, gEfiShellParametersProtocol->Argv[LoopVar], 0);\r
-          ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L"\"", 0);\r
+          Parameter = gEfiShellParametersProtocol->Argv[LoopVar];\r
+          if (Parameter[0] == L'\"' && Parameter[StrLen(Parameter)-1] == L'\"') {\r
+            ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" ", 0);\r
+            ArgSet = StrnCatGrow(&ArgSet, &ArgSize, Parameter, 0);\r
+          } else {\r
+            ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L" \"", 0);\r
+            ArgSet = StrnCatGrow(&ArgSet, &ArgSize, Parameter, 0);\r
+            ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L"\"", 0);\r
+          }\r
         }\r
       }\r
       if (ArgSet == NULL) {\r
@@ -405,7 +409,10 @@ ShellCommandRunFor (
         NewSize = StrSize(ArgSet);\r
         NewSize += sizeof(SHELL_FOR_INFO)+StrSize(gEfiShellParametersProtocol->Argv[1]);\r
         Info = AllocateZeroPool(NewSize);\r
-        ASSERT(Info != NULL);\r
+        if (Info == NULL) {\r
+          FreePool (ArgSet);\r
+          return SHELL_OUT_OF_RESOURCES;\r
+        }\r
         Info->Signature = SHELL_FOR_INFO_SIGNATURE;\r
         CopyMem(Info->Set, ArgSet, StrSize(ArgSet));\r
         NewSize = StrSize(gEfiShellParametersProtocol->Argv[1]);\r
@@ -429,6 +436,11 @@ ShellCommandRunFor (
         gEfiShellParametersProtocol->Argv[2]) == 0) {\r
       for (LoopVar = 0x3 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {\r
         ASSERT((ArgSet == NULL && ArgSize == 0) || (ArgSet != NULL));\r
+        if (StrStr (gEfiShellParametersProtocol->Argv[LoopVar], L")") != NULL &&\r
+            (LoopVar + 1) < gEfiShellParametersProtocol->Argc\r
+           ) {\r
+          return (SHELL_INVALID_PARAMETER);\r
+        }\r
         if (ArgSet == NULL) {\r
 //        ArgSet = StrnCatGrow(&ArgSet, &ArgSize, L"\"", 0);\r
         } else {\r
@@ -444,7 +456,10 @@ ShellCommandRunFor (
         // set up for a 'run' for loop\r
         //\r
         Info = AllocateZeroPool(sizeof(SHELL_FOR_INFO)+StrSize(gEfiShellParametersProtocol->Argv[1]));\r
-        ASSERT(Info != NULL);\r
+        if (Info == NULL) {\r
+          FreePool (ArgSet);\r
+          return SHELL_OUT_OF_RESOURCES;\r
+        }\r
         Info->Signature = SHELL_FOR_INFO_SIGNATURE;\r
         CopyMem(Info->Set, gEfiShellParametersProtocol->Argv[1], StrSize(gEfiShellParametersProtocol->Argv[1]));\r
         Info->ReplacementName = Info->Set;\r
@@ -609,7 +624,9 @@ ShellCommandRunFor (
   if (CurrentScriptFile != NULL && CurrentScriptFile->CurrentCommand != NULL) {\r
     Info = (SHELL_FOR_INFO*)CurrentScriptFile->CurrentCommand->Data;\r
     if (CurrentScriptFile->CurrentCommand->Reset) {\r
-      Info->CurrentValue  = (CHAR16*)Info->Set;\r
+      if (Info != NULL) {\r
+        Info->CurrentValue = (CHAR16*)Info->Set;\r
+      }\r
       FirstPass = TRUE;\r
       CurrentScriptFile->CurrentCommand->Reset = FALSE;\r
     }\r
@@ -692,12 +709,6 @@ ShellCommandRunFor (
           InternalUpdateAliasOnList(Info->ReplacementName, TempString, &CurrentScriptFile->SubstList);\r
           Info->CurrentValue += StrLen(TempString);\r
 \r
-          if (Info->CurrentValue[0] == L'\"') {\r
-            Info->CurrentValue++;\r
-          }\r
-          while (Info->CurrentValue[0] == L' ') {\r
-            Info->CurrentValue++;\r
-          }\r
           if (Info->CurrentValue[0] == L'\"') {\r
             Info->CurrentValue++;\r
           }\r