]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libiberty: fix memory leak in pex-win32.c and refactor
authorCostas Argyris <costas.argyris@gmail.com>
Sun, 26 Feb 2023 16:34:11 +0000 (16:34 +0000)
committerJonathan Yong <10walls@gmail.com>
Fri, 3 Mar 2023 10:45:43 +0000 (10:45 +0000)
Fix memory leak of cmdline buffer and refactor to have
cleanup code appear once for all exit cases.

libiberty/ChangeLog:

* pex-win32.c (win32_spawn): Fix memory leak of cmdline
buffer and refactor to have cleanup code appear once
for all exit cases.

Signed-off-by: Jonathan Yong <10walls@gmail.com>
libiberty/pex-win32.c

index 02d3a3e839b08854276f1f0ac4d8a407a7edafb8..23c6c190a2ca39306dd948defb90cb8f43aa01db 100644 (file)
@@ -577,14 +577,12 @@ win32_spawn (const char *executable,
             LPSTARTUPINFO si,
             LPPROCESS_INFORMATION pi)
 {
-  char *full_executable;
-  char *cmdline;
+  char *full_executable = NULL;
+  char *cmdline = NULL;
+  pid_t pid = (pid_t) -1;
   char **env_copy;
   char *env_block = NULL;
 
-  full_executable = NULL;
-  cmdline = NULL;
-
   if (env)
     {
       int env_size;
@@ -622,13 +620,13 @@ win32_spawn (const char *executable,
 
   full_executable = find_executable (executable, search);
   if (!full_executable)
-    goto error;
+    goto exit;
   cmdline = argv_to_cmdline (argv);
   if (!cmdline)
-    goto error;
+    goto exit;
     
   /* Create the child process.  */  
-  if (!CreateProcess (full_executable, cmdline, 
+  if (CreateProcess (full_executable, cmdline,
                      /*lpProcessAttributes=*/NULL,
                      /*lpThreadAttributes=*/NULL,
                      /*bInheritHandles=*/TRUE,
@@ -638,26 +636,17 @@ win32_spawn (const char *executable,
                      si,
                      pi))
     {
-      free (env_block);
-
-      free (full_executable);
-
-      return (pid_t) -1;
+      CloseHandle (pi->hThread);
+      pid = (pid_t) pi->hProcess;
     }
 
+ exit:
   /* Clean up.  */
-  CloseHandle (pi->hThread);
-  free (full_executable);
-  free (env_block);
-
-  return (pid_t) pi->hProcess;
-
- error:
   free (env_block);
   free (cmdline);
   free (full_executable);
 
-  return (pid_t) -1;
+  return pid;
 }
 
 /* Spawn a script.  This simulates the Unix script execution mechanism.