]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellManParser.c
ShellPkg: fix RVCT warning due to CONST in typecast.
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellManParser.c
index fe9facb483ced97a78f6d95e74cbbc98df0fd816..78b8decd832412584732dea1459dffacb010e46a 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Provides interface to shell MAN file parser.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2015, 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
@@ -32,17 +32,28 @@ GetManFileName(
   )\r
 {\r
   CHAR16 *Buffer;\r
-  ASSERT(ManFileName != NULL);\r
+  if (ManFileName == NULL) {\r
+    return (NULL);\r
+  }\r
   //\r
   // Fix the file name\r
   //\r
   if (StrnCmp(ManFileName+StrLen(ManFileName)-4, L".man", 4)==0) {\r
-    Buffer = AllocateZeroPool(StrSize(ManFileName));\r
-    StrCpy(Buffer, ManFileName);\r
+    Buffer = AllocateCopyPool(StrSize(ManFileName), ManFileName);\r
   } else {\r
     Buffer = AllocateZeroPool(StrSize(ManFileName) + 4*sizeof(CHAR16));\r
-    StrCpy(Buffer, ManFileName);\r
-    StrCat(Buffer, L".man");\r
+    if (Buffer != NULL) {\r
+      StrnCpyS( Buffer, \r
+                (StrSize(ManFileName) + 4*sizeof(CHAR16))/sizeof(CHAR16), \r
+                ManFileName, \r
+                StrLen(ManFileName)\r
+                );\r
+      StrnCatS( Buffer, \r
+                (StrSize(ManFileName) + 4*sizeof(CHAR16))/sizeof(CHAR16),\r
+                L".man", \r
+                4\r
+                );\r
+    }\r
   }\r
   return (Buffer);\r
 }\r
@@ -174,6 +185,10 @@ ManBufferFindSections(
         TempString2 = MIN(TempString2, StrStr(CurrentLocation, L"\n"));\r
         ASSERT(TempString == NULL);\r
         TempString = StrnCatGrow(&TempString, NULL, CurrentLocation, TempString2==NULL?0:TempString2 - CurrentLocation);\r
+        if (TempString == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
         SectionName = TempString;\r
         SectionLen = StrLen(SectionName);\r
         SectionName = StrStr(Sections, SectionName);\r
@@ -191,12 +206,24 @@ ManBufferFindSections(
         TempString2 = MIN(TempString2, StrStr(CurrentLocation, L"\n"));\r
         ASSERT(TempString == NULL);\r
         TempString = StrnCatGrow(&TempString, NULL, CurrentLocation, TempString2==NULL?0:TempString2 - CurrentLocation);\r
+        if (TempString == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
         //\r
         // copy and save the current line.\r
         //\r
         ASSERT((*HelpText == NULL && *HelpSize == 0) || (*HelpText != NULL));\r
         StrnCatGrow (HelpText, HelpSize, TempString, 0);\r
+        if (HelpText == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
         StrnCatGrow (HelpText, HelpSize, L"\r\n", 0);\r
+        if (HelpText == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          break;\r
+        }\r
       }\r
     }\r
     SHELL_FREE_NON_NULL(TempString);\r
@@ -352,6 +379,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
@@ -362,13 +392,17 @@ ManBufferFindTitleSection(
 \r
   Status    = EFI_SUCCESS;\r
 \r
-  TitleString = AllocatePool((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
+  StrCpyS(TitleString, TitleLength/sizeof(CHAR16), StartString);\r
+  StrCatS(TitleString, TitleLength/sizeof(CHAR16), Command);\r
+  StrCatS(TitleString, TitleLength/sizeof(CHAR16), EndString);\r
 \r
   CurrentLocation = StrStr(*Buffer, TitleString);\r
   if (CurrentLocation == NULL){\r
@@ -383,24 +417,27 @@ ManBufferFindTitleSection(
       ;  CurrentLocation++);\r
 \r
     TitleEnd = StrStr(CurrentLocation, L"\"");\r
-    ASSERT(TitleEnd != NULL);\r
-    if (BriefDesc != NULL) {\r
-      *BriefSize = StrSize(TitleEnd);\r
-      *BriefDesc = AllocateZeroPool(*BriefSize);\r
-      if (*BriefDesc == NULL) {\r
-        Status = EFI_OUT_OF_RESOURCES;\r
-      } else {\r
-        StrnCpy(*BriefDesc, CurrentLocation, TitleEnd-CurrentLocation);\r
+    if (TitleEnd == NULL) {\r
+      Status = EFI_DEVICE_ERROR;\r
+    } else {\r
+      if (BriefDesc != NULL) {\r
+        *BriefSize = StrSize(TitleEnd);\r
+        *BriefDesc = AllocateZeroPool(*BriefSize);\r
+        if (*BriefDesc == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+        } else {\r
+          StrnCpyS(*BriefDesc, (*BriefSize)/sizeof(CHAR16), CurrentLocation, TitleEnd-CurrentLocation);\r
+        }\r
       }\r
-    }\r
 \r
-    for (CurrentLocation = TitleEnd\r
-      ;  *CurrentLocation != L'\n'\r
-      ;  CurrentLocation++);\r
-    for (\r
-      ;  *CurrentLocation == L' ' || *CurrentLocation == L'\n' || *CurrentLocation == L'\r'\r
-      ;  CurrentLocation++);\r
-    *Buffer = CurrentLocation;\r
+      for (CurrentLocation = TitleEnd\r
+        ;  *CurrentLocation != L'\n'\r
+        ;  CurrentLocation++);\r
+      for (\r
+        ;  *CurrentLocation == L' ' || *CurrentLocation == L'\n' || *CurrentLocation == L'\r'\r
+        ;  CurrentLocation++);\r
+      *Buffer = CurrentLocation;\r
+    }\r
   }\r
 \r
   FreePool(TitleString);\r
@@ -414,12 +451,12 @@ ManBufferFindTitleSection(
 \r
   Upon a sucessful return the caller is responsible to free the memory in *BriefDesc\r
 \r
-  @param[in] Handle             FileHandle to read from\r
-  @param[in] Command            name of command's section to find\r
-  @param[out] BriefDesc         pointer to pointer to string where description goes.\r
-  @param[out] BriefSize         pointer to size of allocated BriefDesc\r
-  @param[in,out] Ascii          TRUE if the file is ASCII, FALSE otherwise, will be\r
-                                set if the file handle is at the 0 position.\r
+  @param[in] Handle              FileHandle to read from\r
+  @param[in] Command             name of command's section to find\r
+  @param[out] BriefDesc          pointer to pointer to string where description goes.\r
+  @param[out] BriefSize          pointer to size of allocated BriefDesc\r
+  @param[in, out] Ascii          TRUE if the file is ASCII, FALSE otherwise, will be\r
+                                 set if the file handle is at the 0 position.\r
 \r
   @retval EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
   @retval EFI_SUCCESS           the section was found and its description sotred in\r
@@ -442,6 +479,7 @@ ManFileFindTitleSection(
   CHAR16      *TitleEnd;\r
   UINTN       TitleLen;\r
   BOOLEAN     Found;\r
+  UINTN       TitleSize;\r
 \r
   if ( Handle     == NULL\r
     || Command    == NULL\r
@@ -459,13 +497,15 @@ ManFileFindTitleSection(
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
-  TitleString = AllocatePool((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
+  StrCpyS(TitleString, TitleSize/sizeof(CHAR16), L".TH ");\r
+  StrCatS(TitleString, TitleSize/sizeof(CHAR16), Command);\r
+\r
   TitleLen = StrLen(TitleString);\r
   for (;!ShellFileHandleEof(Handle);Size = 1024) {\r
    Status = ShellFileHandleReadLine(Handle, ReadLine, &Size, TRUE, Ascii);\r
@@ -500,7 +540,7 @@ ManFileFindTitleSection(
           Status = EFI_OUT_OF_RESOURCES;\r
           break;\r
         }\r
-        StrCpy(*BriefDesc, TitleEnd);\r
+        StrCpyS(*BriefDesc, (*BriefSize)/sizeof(CHAR16), TitleEnd);\r
       }\r
       break;\r
     }\r
@@ -539,7 +579,8 @@ ManFileFindTitleSection(
   @retval EFI_SUCCESS           The help text was returned.\r
   @retval EFI_OUT_OF_RESOURCES  The necessary buffer could not be allocated to hold the\r
                                 returned help text.\r
-  @retval EFI_INVALID_PARAMETER HelpText is NULL\r
+  @retval EFI_INVALID_PARAMETER HelpText is NULL.\r
+  @retval EFI_INVALID_PARAMETER ManFileName is invalid.\r
   @retval EFI_NOT_FOUND         There is no help text available for Command.\r
 **/\r
 EFI_STATUS\r
@@ -572,6 +613,7 @@ ProcessManFile(
   HelpSize    = 0;\r
   BriefSize   = 0;\r
   TempString  = NULL;\r
+  Ascii       = FALSE;\r
   //\r
   // See if it's in HII first\r
   //\r
@@ -585,6 +627,9 @@ ProcessManFile(
   } else {\r
     FileHandle    = NULL;\r
     TempString  = GetManFileName(ManFileName);\r
+    if (TempString == NULL) {\r
+      return (EFI_INVALID_PARAMETER);\r
+    }\r
 \r
     Status = SearchPathForFile(TempString, &FileHandle);\r
     if (EFI_ERROR(Status)) {\r