public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Yong <jyong@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-6442] libiberty: fix memory leak in pex-win32.c and refactor
Date: Fri,  3 Mar 2023 10:45:56 +0000 (GMT)	[thread overview]
Message-ID: <20230303104556.0142D3858D33@sourceware.org> (raw)

https://gcc.gnu.org/g:59bc2b68de8041adf5eeb5bd18e5921f8a1f9567

commit r13-6442-g59bc2b68de8041adf5eeb5bd18e5921f8a1f9567
Author: Costas Argyris <costas.argyris@gmail.com>
Date:   Sun Feb 26 16:34:11 2023 +0000

    libiberty: fix memory leak in pex-win32.c and refactor
    
    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>

Diff:
---
 libiberty/pex-win32.c | 31 ++++++++++---------------------
 1 file changed, 10 insertions(+), 21 deletions(-)

diff --git a/libiberty/pex-win32.c b/libiberty/pex-win32.c
index 02d3a3e839b..23c6c190a2c 100644
--- a/libiberty/pex-win32.c
+++ b/libiberty/pex-win32.c
@@ -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.

                 reply	other threads:[~2023-03-03 10:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230303104556.0142D3858D33@sourceware.org \
    --to=jyong@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).