]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellManParser.c
ShellPkg: Refactor string manipulation
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellManParser.c
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