This patch fixes a small memory leak in the 'javacomp' module.

2025-07-29  Bruno Haible  <[email protected]>

        javacomp: Fix memory leak.
        * lib/javacomp.c (execute_and_read_line): Free the line after getline()
        failed.

diff --git a/lib/javacomp.c b/lib/javacomp.c
index 936cf79810..ff18f79e35 100644
--- a/lib/javacomp.c
+++ b/lib/javacomp.c
@@ -338,7 +338,6 @@ execute_and_read_line (const char *progname,
   char *line;
   size_t linesize;
   size_t linelen;
-  int exitstatus;
 
   /* Open a pipe to the program.  */
   child = create_pipe_in (progname, prog_path, prog_argv, NULL, NULL,
@@ -359,27 +358,28 @@ execute_and_read_line (const char *progname,
       error (0, 0, _("%s subprocess I/O error"), progname);
       fclose (fp);
       wait_subprocess (child, progname, true, false, true, false, NULL);
-      return NULL;
     }
-  if (linelen > 0 && line[linelen - 1] == '\n')
-    line[linelen - 1] = '\0';
+  else
+    {
+      int exitstatus;
+
+      if (linelen > 0 && line[linelen - 1] == '\n')
+        line[linelen - 1] = '\0';
 
-  /* Read until EOF (otherwise the child process may get a SIGPIPE signal).  */
-  while (getc (fp) != EOF)
-    ;
+      /* Read until EOF (otherwise the child process may get a SIGPIPE 
signal).  */
+      while (getc (fp) != EOF)
+        ;
 
-  fclose (fp);
+      fclose (fp);
 
-  /* Remove zombie process from process list, and retrieve exit status.  */
-  exitstatus =
-    wait_subprocess (child, progname, true, false, true, false, NULL);
-  if (exitstatus != 0)
-    {
-      free (line);
-      return NULL;
+      /* Remove zombie process from process list, and retrieve exit status.  */
+      exitstatus =
+        wait_subprocess (child, progname, true, false, true, false, NULL);
+      if (exitstatus == 0)
+        return line;
     }
-
-  return line;
+  free (line);
+  return NULL;
 }
 
 /* Executes a program, assumed to be a Java compiler with '-version' option.




Reply via email to