]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/FileImage.c
ShellPkg: Add checking for memory allocation and pointer returns from functions.
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / HexEdit / FileImage.c
index adf79281974e77c84807f056109878bb7f0363f4..ff3819c6c61aac604acc3230c73697dd9868a8c7 100644 (file)
@@ -35,26 +35,15 @@ HEFI_EDITOR_BUFFER_IMAGE          HFileImageConst = {
   FALSE\r
 };\r
 \r
+/**\r
+  Initialization function for HFileImage\r
\r
+  @retval EFI_SUCCESS     The operation was successful.\r
+**/\r
 EFI_STATUS\r
 HFileImageInit (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Initialization function for HFileImage\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_LOAD_ERROR\r
-\r
---*/\r
 {\r
   //\r
   // basically initialize the HFileImage\r
@@ -70,29 +59,17 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Backup function for HFileImage. Only a few fields need to be backup. \r
+  This is for making the file buffer refresh as few as possible.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+**/\r
 EFI_STATUS\r
 HFileImageBackup (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Backup function for HFileImage\r
-  Only a few fields need to be backup. \r
-  This is for making the file buffer refresh \r
-  as few as possible.\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
   SHELL_FREE_NON_NULL (HFileImageBackupVar.FileName);\r
   HFileImageBackupVar.FileName = CatSPrint(NULL, L"%s", HFileImage.FileName);\r
@@ -103,25 +80,15 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Cleanup function for HFileImage.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+**/\r
 EFI_STATUS\r
 HFileImageCleanup (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Cleanup function for HFileImage\r
-\r
-Arguments:  \r
-\r
-  None\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-\r
---*/\r
 {\r
 \r
   SHELL_FREE_NON_NULL (HFileImage.FileName);\r
@@ -130,26 +97,18 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Set FileName field in HFileImage\r
+\r
+  @param[in] Str  File name to set.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+**/\r
 EFI_STATUS\r
 HFileImageSetFileName (\r
   IN CONST CHAR16 *Str\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Set FileName field in HFileImage\r
-\r
-Arguments:  \r
-\r
-  Str -- File name to set\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
   UINTN Size;\r
   UINTN Index;\r
@@ -175,79 +134,21 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-HFileImageGetFileInfo (\r
-  IN  EFI_FILE_HANDLE Handle,\r
-  IN  CHAR16          *FileName,\r
-  OUT EFI_FILE_INFO   **InfoOut\r
-  )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Get this file's information\r
-\r
-Arguments:  \r
-\r
-  Handle   - in NT32 mode Directory handle, in other mode File Handle\r
-  FileName - The file name\r
-  InfoOut  - parameter to pass file information out\r
-\r
-Returns:  \r
+/**\r
+  Read a file from disk into HBufferImage.\r
 \r
-  EFI_SUCCESS\r
-  EFI_OUT_OF_RESOURCES\r
-  EFI_LOAD_ERROR\r
-\r
---*/\r
-{\r
-\r
-  VOID        *Info;\r
-  UINTN       Size;\r
-  EFI_STATUS  Status;\r
-\r
-  Size  = SIZE_OF_EFI_FILE_INFO + 1024;\r
-  Info  = AllocateZeroPool (Size);\r
-  if (!Info) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // get file information\r
-  //\r
-  Status = Handle->GetInfo (Handle, &gEfiFileInfoGuid, &Size, Info);\r
-  if (EFI_ERROR (Status)) {\r
-    return EFI_LOAD_ERROR;\r
-  }\r
-\r
-  *InfoOut = (EFI_FILE_INFO *) Info;\r
-\r
-  return EFI_SUCCESS;\r
-\r
-}\r
+  @param[in] FileName     filename to read.\r
+  @param[in] Recover      if is for recover, no information print.\r
 \r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @retval EFI_LOAD_ERROR        A load error occured.\r
+**/\r
 EFI_STATUS\r
 HFileImageRead (\r
   IN CONST CHAR16  *FileName,\r
   IN BOOLEAN Recover\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Read a file from disk into HBufferImage\r
-\r
-Arguments:  \r
-\r
-  FileName -- filename to read\r
-  Recover -- if is for recover, no information print\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_LOAD_ERROR\r
-  EFI_OUT_OF_RESOURCES\r
-  \r
---*/\r
 {\r
   HEFI_EDITOR_LINE                *Line;\r
   UINT8                           *Buffer;\r
@@ -268,7 +169,7 @@ Returns:
   // you should set the status string\r
   //\r
   Status = ReadFileIntoBuffer (FileName, (VOID**)&Buffer, &HFileImage.Size, &HFileImage.ReadOnly);\r
-  if (EFI_ERROR(Status)) {\r
+  if (EFI_ERROR(Status) || Buffer == NULL) {\r
     UnicodeBuffer = CatSPrint(NULL, L"Read error on file &s: %r", FileName, Status);\r
     if (UnicodeBuffer == NULL) {\r
       SHELL_FREE_NON_NULL(Buffer);\r
@@ -277,6 +178,7 @@ Returns:
 \r
     StatusBarSetStatusString (UnicodeBuffer);\r
     FreePool (UnicodeBuffer);\r
+    return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
   HFileImageSetFileName (FileName);\r
@@ -301,6 +203,7 @@ Returns:
   HBufferImage.HighBits               = TRUE;\r
   HBufferImage.BufferPosition.Row     = 1;\r
   HBufferImage.BufferPosition.Column  = 1;\r
+  HBufferImage.BufferType = FileTypeFileBuffer;\r
 \r
   if (!Recover) {\r
     UnicodeBuffer = CatSPrint(NULL, L"%d Lines Read", HBufferImage.NumLines);\r
@@ -342,27 +245,19 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Save lines in HBufferImage to disk.\r
+\r
+  @param[in] FileName     The file name.\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.\r
+  @retval EFI_OUT_OF_RESOURCES  A memory allocation failed.\r
+  @retval EFI_LOAD_ERROR        A load error occured.\r
+**/\r
 EFI_STATUS\r
 HFileImageSave (\r
   IN CHAR16 *FileName\r
   )\r
-/*++\r
-\r
-Routine Description: \r
-\r
-  Save lines in HBufferImage to disk\r
-\r
-Arguments:  \r
-\r
-  FileName - The file name\r
-\r
-Returns:  \r
-\r
-  EFI_SUCCESS\r
-  EFI_LOAD_ERROR\r
-  EFI_OUT_OF_RESOURCES\r
-\r
---*/\r
 {\r
 \r
   LIST_ENTRY                      *Link;\r
@@ -382,7 +277,7 @@ Returns:
   //\r
   // if is the old file\r
   //\r
-  if (StrCmp (FileName, HFileImage.FileName) == 0) {\r
+  if (HFileImage.FileName != NULL && FileName != NULL && StrCmp (FileName, HFileImage.FileName) == 0) {\r
     //\r
     // check whether file exists on disk\r
     //\r
@@ -488,7 +383,8 @@ Returns:
   //\r
   // now everything is ready , you can set the new file name to filebuffer\r
   //\r
-  if (BufferTypeBackup != FileTypeFileBuffer || StringNoCaseCompare (&FileName, &HFileImage.FileName) != 0) {\r
+  if ((BufferTypeBackup != FileTypeFileBuffer && FileName != NULL) ||\r
+     (FileName != NULL && HFileImage.FileName != NULL && StringNoCaseCompare (&FileName, &HFileImage.FileName) != 0)){\r
     //\r
     // not the same\r
     //\r