]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Fix memory leak when running Shell script.
authorQiu Shumin <shumin.qiu@intel.com>
Thu, 24 Dec 2015 08:14:51 +0000 (08:14 +0000)
committershenshushi <shenshushi@Edk2>
Thu, 24 Dec 2015 08:14:51 +0000 (08:14 +0000)
When we run following script in Shell:
"
for %a run (1 200)
  echo %a
  memmap
endfor
"
We may find memory leak in system. This patch free buffer in 'BufferToFreeList' to avoid this issue.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qiu Shumin <shumin.qiu@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@19521 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Application/Shell/Shell.c
ShellPkg/Application/Shell/Shell.h

index 97b8c8d6fbee39a890b6b48216fec8d0dd723a88..465f4f3974c12bfaf7469cf88a3bcf2fa2cb82c9 100644 (file)
@@ -1276,6 +1276,36 @@ AddBufferToFreeList(
   return (Buffer);\r
 }\r
 \r
+\r
+/**\r
+  Create a new buffer list and stores the old one to OldBufferList \r
+\r
+  @param OldBufferList   The temporary list head used to store the nodes in BufferToFreeList.\r
+**/\r
+VOID\r
+SaveBufferList (\r
+  OUT LIST_ENTRY     *OldBufferList\r
+  )\r
+{\r
+  CopyMem (OldBufferList, &ShellInfoObject.BufferToFreeList.Link, sizeof (LIST_ENTRY));\r
+  InitializeListHead (&ShellInfoObject.BufferToFreeList.Link);\r
+}\r
+\r
+/**\r
+  Restore previous nodes into BufferToFreeList .\r
+\r
+  @param OldBufferList   The temporary list head used to store the nodes in BufferToFreeList.\r
+**/\r
+VOID\r
+RestoreBufferList (\r
+  IN OUT LIST_ENTRY     *OldBufferList\r
+  )\r
+{\r
+  FreeBufferList (&ShellInfoObject.BufferToFreeList);\r
+  CopyMem (&ShellInfoObject.BufferToFreeList.Link, OldBufferList, sizeof (LIST_ENTRY));\r
+}\r
+\r
+\r
 /**\r
   Add a buffer to the Line History List\r
 \r
@@ -2661,6 +2691,7 @@ RunScriptFileHandle (
   CONST CHAR16        *CurDir;\r
   UINTN               LineCount;\r
   CHAR16              LeString[50];\r
+  LIST_ENTRY          OldBufferList;\r
 \r
   ASSERT(!ShellCommandGetScriptExit());\r
 \r
@@ -2763,6 +2794,8 @@ RunScriptFileHandle (
               PrintBuffSize/sizeof(CHAR16) - 1\r
               );\r
 \r
+    SaveBufferList(&OldBufferList);\r
+\r
     //\r
     // NULL out comments\r
     //\r
@@ -2897,15 +2930,19 @@ RunScriptFileHandle (
 \r
           ShellCommandRegisterExit(FALSE, 0);\r
           Status = EFI_SUCCESS;\r
+          RestoreBufferList(&OldBufferList);\r
           break;\r
         }\r
         if (ShellGetExecutionBreakFlag()) {\r
+          RestoreBufferList(&OldBufferList);\r
           break;\r
         }\r
         if (EFI_ERROR(Status)) {\r
+          RestoreBufferList(&OldBufferList);\r
           break;\r
         }\r
         if (ShellCommandGetExit()) {\r
+          RestoreBufferList(&OldBufferList);\r
           break;\r
         }\r
       }\r
@@ -2924,6 +2961,7 @@ RunScriptFileHandle (
         NewScriptFile->CurrentCommand->Reset = TRUE;\r
       }\r
     }\r
+    RestoreBufferList(&OldBufferList);\r
   }\r
 \r
 \r
index 57263204e388e86c61e0488e9d85ce7852580b9a..351b94188eab9707834eed990f62c0eb4ab51cdf 100644 (file)
@@ -382,5 +382,28 @@ TrimSpaces(
   IN CHAR16 **String\r
   );\r
 \r
+/**\r
+  \r
+  Create a new buffer list and stores the old one to OldBufferList  \r
+\r
+  @param OldBufferList   The temporary list head used to store the nodes in BufferToFreeList.\r
+**/\r
+VOID\r
+SaveBufferList (\r
+  OUT LIST_ENTRY     *OldBufferList\r
+  );\r
+\r
+/**\r
+  Restore previous nodes into BufferToFreeList .\r
+\r
+  @param OldBufferList   The temporary list head used to store the nodes in BufferToFreeList.\r
+**/\r
+VOID\r
+RestoreBufferList (\r
+  IN OUT LIST_ENTRY     *OldBufferList\r
+  );\r
+\r
+\r
+\r
 #endif //_SHELL_INTERNAL_HEADER_\r
 \r