]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: InternalShellExecuteDevicePath: avoid memory leaks
authorBrendan Jackman <Brendan.Jackman@arm.com>
Tue, 11 Feb 2014 22:46:56 +0000 (22:46 +0000)
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 11 Feb 2014 22:46:56 +0000 (22:46 +0000)
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Brendan Jackman <Brendan.Jackman@arm.com>
Reviewed-by: Olivier Martin <olivier.martin@arm.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15224 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Application/Shell/ShellProtocol.c

index 502713770501158cf80c12e596f6a5bc2b4eef38..0a19793a1b777fa12c05c0a362eb891ede896ebf 100644 (file)
@@ -1397,6 +1397,7 @@ InternalShellExecuteDevicePath(
   UINTN                         InternalExitDataSize;\r
   UINTN                         *ExitDataSizePtr;\r
   CHAR16                        *ImagePath;\r
+  UINTN                         Index;\r
 \r
   // ExitDataSize is not OPTIONAL for gBS->BootServices, provide somewhere for\r
   // it to be dumped if the caller doesn't want it.\r
@@ -1482,7 +1483,7 @@ InternalShellExecuteDevicePath(
         ShellParamsProtocol.Argv = AllocatePool (sizeof (CHAR16 *));\r
         if (ShellParamsProtocol.Argv == NULL) {\r
           Status = EFI_OUT_OF_RESOURCES;\r
-          goto Cleanup;\r
+          goto UnloadImage;\r
         }\r
         ShellParamsProtocol.Argc = 1;\r
       } else {\r
@@ -1506,36 +1507,37 @@ InternalShellExecuteDevicePath(
                           ExitDataSizePtr,\r
                           ExitData\r
                           );\r
-    }\r
 \r
-    //\r
-    // Cleanup (and dont overwrite errors)\r
-    //\r
-    if (EFI_ERROR(Status)) {\r
-      CleanupStatus = gBS->UninstallProtocolInterface(\r
-                            NewHandle,\r
-                            &gEfiShellParametersProtocolGuid,\r
-                            &ShellParamsProtocol\r
-                            );\r
-      ASSERT_EFI_ERROR(CleanupStatus);\r
-    } else {\r
       CleanupStatus = gBS->UninstallProtocolInterface(\r
                             NewHandle,\r
                             &gEfiShellParametersProtocolGuid,\r
                             &ShellParamsProtocol\r
                             );\r
       ASSERT_EFI_ERROR(CleanupStatus);\r
+\r
+      goto FreeAlloc;\r
+    }\r
+\r
+UnloadImage:\r
+    // Unload image - We should only get here if we didn't call StartImage\r
+    gBS->UnloadImage (NewHandle);\r
+\r
+FreeAlloc:\r
+    // Free Argv (Allocated in UpdateArgcArgv)\r
+    if (ShellParamsProtocol.Argv != NULL) {\r
+      for (Index = 0; Index < ShellParamsProtocol.Argc; Index++) {\r
+        if (ShellParamsProtocol.Argv[Index] != NULL) {\r
+          FreePool (ShellParamsProtocol.Argv[Index]);\r
+        }\r
+      }\r
+      FreePool (ShellParamsProtocol.Argv);\r
     }\r
   }\r
 \r
+  // Restore environment variables\r
   if (!IsListEmpty(&OrigEnvs)) {\r
-    if (EFI_ERROR(Status)) {\r
-      CleanupStatus = SetEnvironmentVariableList(&OrigEnvs);\r
-      ASSERT_EFI_ERROR(CleanupStatus);\r
-    } else {\r
-      CleanupStatus = SetEnvironmentVariableList(&OrigEnvs);\r
-      ASSERT_EFI_ERROR (CleanupStatus);\r
-    }\r
+    CleanupStatus = SetEnvironmentVariableList(&OrigEnvs);\r
+    ASSERT_EFI_ERROR (CleanupStatus);\r
   }\r
 \r
   return(Status);\r