Verify memory allocations were successful.
authorjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 4 Oct 2010 16:30:40 +0000 (16:30 +0000)
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Mon, 4 Oct 2010 16:30:40 +0000 (16:30 +0000)
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@10909 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Library/UefiShellLevel1CommandsLib/For.c
ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ShellPkg/Library/UefiShellLevel2CommandsLib/Ls.c
ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c
ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
ShellPkg/Library/UefiShellLevel3CommandsLib/Alias.c

index 0e68119..297a137 100644 (file)
@@ -103,8 +103,11 @@ typedef struct {
   @param[in] Alias              The alias to test for.\r
   @param[in] CommandString      The updated command string.\r
   @param[in,out] List           The list to search.\r
+\r
+  @retval EFI_SUCCESS           The operation was completed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES  There was not enough free memory.\r
 **/\r
-VOID\r
+EFI_STATUS\r
 EFIAPI\r
 InternalUpdateAliasOnList(\r
   IN CONST CHAR16       *Alias,\r
@@ -139,12 +142,16 @@ InternalUpdateAliasOnList(
   }\r
   if (!Found) {\r
     Node = AllocateZeroPool(sizeof(ALIAS_LIST));\r
+    if (Node == NULL) {\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
     ASSERT(Node->Alias == NULL);\r
     Node->Alias         = StrnCatGrow(&Node->Alias, NULL, Alias, 0);\r
     ASSERT(Node->CommandString == NULL);\r
     Node->CommandString = StrnCatGrow(&Node->CommandString, NULL, CommandString, 0);\r
     InsertTailList(List, &Node->Link);\r
   }\r
+  return (EFI_SUCCESS);\r
 }\r
 \r
 /**\r
index 78403c2..99680b4 100644 (file)
@@ -186,17 +186,27 @@ ShellCommandRunCd (
         // change directory on other drive letter\r
         //\r
         Drive = AllocateZeroPool(StrSize(Param1));\r
-        Drive = StrCpy(Drive, Param1);\r
-        Path = StrStr(Drive, L":");\r
-        *(++Path) = CHAR_NULL;\r
-        Status = gEfiShellProtocol->SetCurDir(Drive, ++Path);\r
+        if (Drive == NULL) {\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
+          ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+        } else {\r
+          Drive = StrCpy(Drive, Param1);\r
+          Path = StrStr(Drive, L":");\r
+          *(++Path) = CHAR_NULL;\r
+          if (Path == Drive + StrLen(Drive)) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+            ShellStatus = SHELL_NOT_FOUND;\r
+          } else {\r
+            Status = gEfiShellProtocol->SetCurDir(Drive, ++Path);\r
+          }\r
 \r
-        if (Status == EFI_NOT_FOUND) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
-          Status = SHELL_NOT_FOUND;\r
-        } else if (EFI_ERROR(Status)) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1);\r
-          Status = SHELL_NOT_FOUND;\r
+          if (Status == EFI_NOT_FOUND) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+            Status = SHELL_NOT_FOUND;\r
+          } else if (EFI_ERROR(Status)) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Param1);\r
+            Status = SHELL_NOT_FOUND;\r
+          }\r
         }\r
       }\r
     }\r
index 94161cf..9344c33 100644 (file)
@@ -287,6 +287,13 @@ ValidateAndCopyFiles(
   HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
   DestPath    = AllocatePool(PathLen);\r
 \r
+  if (HiiOutput == NULL || HiiOutput == NULL || HiiResultOk == NULL) {\r
+    SHELL_FREE_NON_NULL(DestPath);\r
+    SHELL_FREE_NON_NULL(HiiOutput);\r
+    SHELL_FREE_NON_NULL(HiiResultOk);\r
+    return (SHELL_OUT_OF_RESOURCES);\r
+  }\r
+\r
   //\r
   // Go through the list of files to copy...\r
   //\r
index 61d4897..440d245 100644 (file)
@@ -349,30 +349,35 @@ PrintLsOutput(
 \r
   if (Rec){\r
     DirectoryName = AllocatePool(LongestPath + 2*sizeof(CHAR16));\r
-    for ( Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&ListHead->Link)\r
-        ; !IsNull(&ListHead->Link, &Node->Link)\r
-        ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&ListHead->Link, &Node->Link)\r
-       ){\r
-      //\r
-      // recurse on any directory except the traversing ones...\r
-      //\r
-      if (((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY)\r
-        && StrCmp(Node->FileName, L".") != 0\r
-        && StrCmp(Node->FileName, L"..") != 0\r
-       ){\r
-        StrCpy(DirectoryName, Node->FullName);\r
-        StrCat(DirectoryName, L"\\*");\r
-        PrintLsOutput(\r
-          Rec,\r
-          Attribs,\r
-          Sfo,\r
-          DirectoryName,\r
-          FALSE,\r
-          Count,\r
-          TimeZone);\r
+    if (DirectoryName == NULL) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
+      ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+    } else {\r
+      for ( Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&ListHead->Link)\r
+          ; !IsNull(&ListHead->Link, &Node->Link)\r
+          ; Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&ListHead->Link, &Node->Link)\r
+         ){\r
+        //\r
+        // recurse on any directory except the traversing ones...\r
+        //\r
+        if (((Node->Info->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY)\r
+          && StrCmp(Node->FileName, L".") != 0\r
+          && StrCmp(Node->FileName, L"..") != 0\r
+         ){\r
+          StrCpy(DirectoryName, Node->FullName);\r
+          StrCat(DirectoryName, L"\\*");\r
+          PrintLsOutput(\r
+            Rec,\r
+            Attribs,\r
+            Sfo,\r
+            DirectoryName,\r
+            FALSE,\r
+            Count,\r
+            TimeZone);\r
+        }\r
       }\r
+      FreePool(DirectoryName);\r
     }\r
-    FreePool(DirectoryName);\r
   }\r
 \r
   FreePool(CorrectedPath);\r
index c4c8c87..f696523 100644 (file)
@@ -99,7 +99,7 @@ UpdateMapping (
   //\r
   // Find each handle with Simple File System\r
   //\r
-  HandleList = GetHandleListByPotocol(&gEfiSimpleFileSystemProtocolGuid);\r
+  HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
   if (HandleList != NULL) {\r
     //\r
     // Do a count of the handles\r
@@ -503,6 +503,9 @@ PerformMappingDisplay(
     HandleBuffer);\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
     HandleBuffer = AllocatePool(BufferSize);\r
+    if (HandleBuffer == NULL) {\r
+      return (SHELL_OUT_OF_RESOURCES);\r
+    }\r
     Status = gBS->LocateHandle(\r
       ByProtocol,\r
       &gEfiDevicePathProtocolGuid,\r
@@ -542,6 +545,9 @@ PerformMappingDisplay(
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
     FreePool(HandleBuffer);\r
     HandleBuffer = AllocatePool(BufferSize);\r
+    if (HandleBuffer == NULL) {\r
+      return (SHELL_OUT_OF_RESOURCES);\r
+    }\r
     Status = gBS->LocateHandle(\r
       ByProtocol,\r
       &gEfiBlockIoProtocolGuid,\r
@@ -549,37 +555,37 @@ PerformMappingDisplay(
       &BufferSize,\r
       HandleBuffer);\r
   }\r
-  ASSERT_EFI_ERROR(Status);\r
-\r
-  //\r
-  // Get the map name(s) for each one.\r
-  //\r
-  for ( LoopVar = 0\r
-      ; LoopVar < BufferSize / sizeof(EFI_HANDLE)\r
-      ; LoopVar ++\r
-     ){\r
+  if (!EFI_ERROR(Status)) {\r
     //\r
-    // Skip any that were already done...\r
+    // Get the map name(s) for each one.\r
     //\r
-    if (gBS->OpenProtocol(\r
-      HandleBuffer[LoopVar],\r
-      &gEfiDevicePathProtocolGuid,\r
-      NULL,\r
-      gImageHandle,\r
-      NULL,\r
-      EFI_OPEN_PROTOCOL_TEST_PROTOCOL) == EFI_SUCCESS) {\r
-        continue;\r
+    for ( LoopVar = 0\r
+        ; LoopVar < BufferSize / sizeof(EFI_HANDLE)\r
+        ; LoopVar ++\r
+       ){\r
+      //\r
+      // Skip any that were already done...\r
+      //\r
+      if (gBS->OpenProtocol(\r
+        HandleBuffer[LoopVar],\r
+        &gEfiDevicePathProtocolGuid,\r
+        NULL,\r
+        gImageHandle,\r
+        NULL,\r
+        EFI_OPEN_PROTOCOL_TEST_PROTOCOL) == EFI_SUCCESS) {\r
+          continue;\r
+      }\r
+      PerformSingleMappingDisplay(\r
+        Verbose,\r
+        Consist,\r
+        Normal,\r
+        Test,\r
+        SFO,\r
+        Specific,\r
+        HandleBuffer[LoopVar]);\r
     }\r
-    PerformSingleMappingDisplay(\r
-      Verbose,\r
-      Consist,\r
-      Normal,\r
-      Test,\r
-      SFO,\r
-      Specific,\r
-      HandleBuffer[LoopVar]);\r
+    FreePool(HandleBuffer);\r
   }\r
-  FreePool(HandleBuffer);\r
   return (SHELL_SUCCESS);\r
 }\r
 \r
index adb6f99..a628781 100644 (file)
@@ -152,6 +152,10 @@ GetDestinationLocation(
       NewSize = StrSize(Cwd);\r
       NewSize += StrSize(DestDir);\r
       DestPath = AllocateZeroPool(NewSize);\r
+      if (DestPath == NULL) {\r
+        ShellCloseFileMetaArg(&DestList);\r
+        return (SHELL_OUT_OF_RESOURCES);\r
+      }\r
       StrCpy(DestPath, Cwd);\r
       if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
         StrCat(DestPath, L"\\");\r
@@ -162,6 +166,10 @@ GetDestinationLocation(
     } else {\r
       ASSERT(DestPath == NULL);\r
       DestPath = StrnCatGrow(&DestPath, NULL, DestDir, 0);\r
+      if (DestPath == NULL) {\r
+        ShellCloseFileMetaArg(&DestList);\r
+        return (SHELL_OUT_OF_RESOURCES);\r
+      }\r
     }\r
   } else {\r
     Node = (EFI_SHELL_FILE_INFO*)GetFirstNode(&DestList->Link);\r
@@ -175,6 +183,10 @@ GetDestinationLocation(
     }\r
     if (ShellIsDirectory(Node->FullName)==EFI_SUCCESS) {\r
       DestPath = AllocateZeroPool(StrSize(Node->FullName)+sizeof(CHAR16));\r
+      if (DestPath == NULL) {\r
+        ShellCloseFileMetaArg(&DestList);\r
+        return (SHELL_OUT_OF_RESOURCES);\r
+      }\r
       StrCpy(DestPath, Node->FullName);\r
       StrCat(DestPath, L"\\");\r
     } else {\r
@@ -287,62 +299,66 @@ ValidateAndMoveFiles(
     NewSize = StrSize(DestPath);\r
     NewSize += StrSize(Node->FileName) + sizeof(EFI_FILE_INFO) + sizeof(CHAR16);\r
     NewFileInfo = AllocateZeroPool(NewSize);\r
-    ASSERT(NewFileInfo != NULL);\r
-    CopyMem(NewFileInfo, Node->Info, sizeof(EFI_FILE_INFO));\r
-    if (DestPath[0] != L'\\') {\r
-      StrCpy(NewFileInfo->FileName, L"\\");\r
-      StrCat(NewFileInfo->FileName, DestPath);\r
+    if (NewFileInfo == NULL) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_MEM), gShellLevel2HiiHandle);\r
+      ShellStatus = SHELL_OUT_OF_RESOURCES;\r
     } else {\r
-      StrCpy(NewFileInfo->FileName, DestPath);\r
-    }\r
-    if (NewFileInfo->FileName[StrLen(NewFileInfo->FileName)-1] == L'\\') {\r
-      if (Node->FileName[0] == L'\\') {\r
-        //\r
-        // Don't allow for double slashes. Eliminate one of them.\r
-        //\r
-        NewFileInfo->FileName[StrLen(NewFileInfo->FileName)-1] = CHAR_NULL;\r
+      CopyMem(NewFileInfo, Node->Info, sizeof(EFI_FILE_INFO));\r
+      if (DestPath[0] != L'\\') {\r
+        StrCpy(NewFileInfo->FileName, L"\\");\r
+        StrCat(NewFileInfo->FileName, DestPath);\r
+      } else {\r
+        StrCpy(NewFileInfo->FileName, DestPath);\r
       }\r
-      StrCat(NewFileInfo->FileName, Node->FileName);\r
-    }\r
-    NewFileInfo->Size = sizeof(EFI_FILE_INFO) + StrSize(NewFileInfo->FileName);\r
+      if (NewFileInfo->FileName[StrLen(NewFileInfo->FileName)-1] == L'\\') {\r
+        if (Node->FileName[0] == L'\\') {\r
+          //\r
+          // Don't allow for double slashes. Eliminate one of them.\r
+          //\r
+          NewFileInfo->FileName[StrLen(NewFileInfo->FileName)-1] = CHAR_NULL;\r
+        }\r
+        StrCat(NewFileInfo->FileName, Node->FileName);\r
+      }\r
+      NewFileInfo->Size = sizeof(EFI_FILE_INFO) + StrSize(NewFileInfo->FileName);\r
 \r
-    ShellPrintEx(-1, -1, HiiOutput, Node->FullName, NewFileInfo->FileName);\r
+      ShellPrintEx(-1, -1, HiiOutput, Node->FullName, NewFileInfo->FileName);\r
 \r
-    //\r
-    // Perform the move operation\r
-    //\r
-    Status = ShellSetFileInfo(Node->Handle, NewFileInfo);\r
+      //\r
+      // Perform the move operation\r
+      //\r
+      Status = ShellSetFileInfo(Node->Handle, NewFileInfo);\r
 \r
-    //\r
-    // Free the info object we used...\r
-    //\r
-    ASSERT  (NewFileInfo != NULL);\r
-    FreePool(NewFileInfo);\r
+      //\r
+      // Free the info object we used...\r
+      //\r
+      ASSERT  (NewFileInfo != NULL);\r
+      FreePool(NewFileInfo);\r
 \r
-    //\r
-    // Check our result\r
-    //\r
-    if (EFI_ERROR(Status)) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);\r
       //\r
-      // move failed\r
+      // Check our result\r
       //\r
-      switch(Status){\r
-        default:\r
-          ShellStatus = SHELL_INVALID_PARAMETER;\r
-        case EFI_SECURITY_VIOLATION:\r
-          ShellStatus = SHELL_SECURITY_VIOLATION;\r
-        case EFI_WRITE_PROTECTED:\r
-          ShellStatus = SHELL_WRITE_PROTECTED;\r
-        case EFI_OUT_OF_RESOURCES:\r
-          ShellStatus = SHELL_OUT_OF_RESOURCES;\r
-        case EFI_DEVICE_ERROR:\r
-          ShellStatus = SHELL_DEVICE_ERROR;\r
-        case EFI_ACCESS_DENIED:\r
-          ShellStatus = SHELL_ACCESS_DENIED;\r
-      } // switch\r
-    } else {\r
-      ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
+      if (EFI_ERROR(Status)) {\r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);\r
+        //\r
+        // move failed\r
+        //\r
+        switch(Status){\r
+          default:\r
+            ShellStatus = SHELL_INVALID_PARAMETER;\r
+          case EFI_SECURITY_VIOLATION:\r
+            ShellStatus = SHELL_SECURITY_VIOLATION;\r
+          case EFI_WRITE_PROTECTED:\r
+            ShellStatus = SHELL_WRITE_PROTECTED;\r
+          case EFI_OUT_OF_RESOURCES:\r
+            ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+          case EFI_DEVICE_ERROR:\r
+            ShellStatus = SHELL_DEVICE_ERROR;\r
+          case EFI_ACCESS_DENIED:\r
+            ShellStatus = SHELL_ACCESS_DENIED;\r
+        } // switch\r
+      } else {\r
+        ShellPrintEx(-1, -1, L"%s", HiiResultOk);\r
+      }\r
     }\r
   } // for loop\r
 \r
index da53af4..1428d37 100644 (file)
@@ -41,6 +41,9 @@ PrintAllShellAlias(
     return (SHELL_SUCCESS);\r
   }\r
   Alias = AllocateZeroPool(StrSize(ConstAllAliasList));\r
+  if (Alias == NULL) {\r
+    return (SHELL_OUT_OF_RESOURCES);\r
+  }\r
   Walker = (CHAR16*)ConstAllAliasList;\r
 \r
   do {\r