]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
ShellPkg: Verify memory allocations without ASSERT.
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cd.c
index c753702e816be46ff7489d552ff6cb72049b22fb..74beb093c41de42e91c6398fb3ebf96a612922c2 100644 (file)
@@ -96,78 +96,47 @@ ShellCommandRunCd (
       }\r
     } else {\r
       Param1Copy = CatSPrint(NULL, L"%s", Param1, NULL);\r
-      Param1Copy = PathCleanUpDirectories(Param1Copy);\r
-      if (StrCmp(Param1Copy, L".") == 0) {\r
-        //\r
-        // nothing to do... change to current directory\r
-        //\r
-      } else if (StrCmp(Param1Copy, L"..") == 0) {\r
-        //\r
-        // Change up one directory...\r
-        //\r
-        Directory = ShellGetCurrentDir(NULL);\r
-        if (Directory == NULL) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
-          ShellStatus = SHELL_NOT_FOUND;\r
-        } else {\r
-          Drive = GetFullyQualifiedPath(Directory);\r
-          PathRemoveLastItem(Drive);\r
-        }\r
-        if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+      if (Param1Copy != NULL) {\r
+        Param1Copy = PathCleanUpDirectories(Param1Copy);\r
+      }\r
+      if (Param1Copy != NULL) {\r
+        if (StrCmp(Param1Copy, L".") == 0) {\r
           //\r
-          // change directory on current drive letter\r
+          // nothing to do... change to current directory\r
           //\r
-          Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
-          if (Status == EFI_NOT_FOUND) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
-            ShellStatus = SHELL_NOT_FOUND;\r
-          }\r
-        }\r
-      } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
-        //\r
-        // Move to root of current drive\r
-        //\r
-        Directory = ShellGetCurrentDir(NULL);\r
-        if (Directory == NULL) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
-          ShellStatus = SHELL_NOT_FOUND;\r
-        } else {\r
-          Drive = GetFullyQualifiedPath(Directory);\r
-          while (PathRemoveLastItem(Drive)) ;\r
-        }\r
-        if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+        } else if (StrCmp(Param1Copy, L"..") == 0) {\r
           //\r
-          // change directory on current drive letter\r
+          // Change up one directory...\r
           //\r
-          Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
-          if (Status == EFI_NOT_FOUND) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+          Directory = ShellGetCurrentDir(NULL);\r
+          if (Directory == NULL) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
             ShellStatus = SHELL_NOT_FOUND;\r
-          }\r
-        }\r
-      } else if (StrStr(Param1Copy, L":") == NULL) {\r
-        if (ShellGetCurrentDir(NULL) == NULL) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
-          ShellStatus = SHELL_NOT_FOUND;\r
-        } else {\r
-          ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
-          Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
-          if (*Param1Copy == L'\\') {\r
-            while (PathRemoveLastItem(Drive)) ;\r
-            Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r
           } else {\r
-            Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r
+            Drive = GetFullyQualifiedPath(Directory);\r
+            PathRemoveLastItem(Drive);\r
           }\r
+          if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+            //\r
+            // change directory on current drive letter\r
+            //\r
+            Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
+            if (Status == EFI_NOT_FOUND) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+              ShellStatus = SHELL_NOT_FOUND;\r
+            }\r
+          }\r
+        } else if (StrCmp(Param1Copy, L"\\") == 0) {\r
           //\r
-          // Verify that this is a valid directory\r
+          // Move to root of current drive\r
           //\r
-          Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
-          if (EFI_ERROR(Status)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
-            ShellStatus = SHELL_NOT_FOUND;\r
-          } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
+          Directory = ShellGetCurrentDir(NULL);\r
+          if (Directory == NULL) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
             ShellStatus = SHELL_NOT_FOUND;\r
+          } else {\r
+            Drive = GetFullyQualifiedPath(Directory);\r
+            while (PathRemoveLastItem(Drive)) ;\r
           }\r
           if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
             //\r
@@ -179,42 +148,77 @@ ShellCommandRunCd (
               ShellStatus = SHELL_NOT_FOUND;\r
             }\r
           }\r
-          if (Handle != NULL) {\r
-            gEfiShellProtocol->CloseFile(Handle);\r
-            DEBUG_CODE(Handle = NULL;);\r
+        } else if (StrStr(Param1Copy, L":") == NULL) {\r
+          if (ShellGetCurrentDir(NULL) == NULL) {\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
+            ShellStatus = SHELL_NOT_FOUND;\r
+          } else {\r
+            ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
+            Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
+            if (*Param1Copy == L'\\') {\r
+              while (PathRemoveLastItem(Drive)) ;\r
+              Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r
+            } else {\r
+              Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy, 0);\r
+            }\r
+            //\r
+            // Verify that this is a valid directory\r
+            //\r
+            Status = gEfiShellProtocol->OpenFileByName(Drive, &Handle, EFI_FILE_MODE_READ);\r
+            if (EFI_ERROR(Status)) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, Drive);\r
+              ShellStatus = SHELL_NOT_FOUND;\r
+            } else if (EFI_ERROR(FileHandleIsDirectory(Handle))) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, Drive);\r
+              ShellStatus = SHELL_NOT_FOUND;\r
+            }\r
+            if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
+              //\r
+              // change directory on current drive letter\r
+              //\r
+              Status = gEfiShellProtocol->SetCurDir(NULL, Drive);\r
+              if (Status == EFI_NOT_FOUND) {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+                ShellStatus = SHELL_NOT_FOUND;\r
+              }\r
+            }\r
+            if (Handle != NULL) {\r
+              gEfiShellProtocol->CloseFile(Handle);\r
+              DEBUG_CODE(Handle = NULL;);\r
+            }\r
           }\r
-        }\r
-      } else {\r
-        //\r
-        // change directory on other drive letter\r
-        //\r
-        Drive = AllocateZeroPool(StrSize(Param1Copy));\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, Param1Copy);\r
-          Path = StrStr(Drive, L":");\r
-          ASSERT(Path != NULL);\r
-          if (*(Path+1) == CHAR_NULL) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
-            ShellStatus = SHELL_NOT_FOUND;\r
+          //\r
+          // change directory on other drive letter\r
+          //\r
+          Drive = AllocateZeroPool(StrSize(Param1Copy));\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
-            *(Path+1) = CHAR_NULL;\r
-            if (Path == Drive + StrLen(Drive)) {\r
+            Drive = StrCpy(Drive, Param1Copy);\r
+            Path = StrStr(Drive, L":");\r
+            ASSERT(Path != NULL);\r
+            if (*(Path+1) == CHAR_NULL) {\r
               ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
               ShellStatus = SHELL_NOT_FOUND;\r
             } else {\r
-              Status = gEfiShellProtocol->SetCurDir(Drive, Path+2);\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\r
+              *(Path+1) = 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+2);\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CD_PRINT), gShellLevel2HiiHandle, ShellGetCurrentDir(Drive));\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, Param1Copy);\r
+              Status = SHELL_NOT_FOUND;\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, Param1Copy);\r
-            Status = SHELL_NOT_FOUND;\r
           }\r
         }\r
       }\r