]> Savannah Git Hosting - gnulib.git/commitdiff
javacomp, javaversion: Fix resource leak.
authorBruno Haible <bruno@clisp.org>
Sun, 6 Oct 2024 23:14:20 +0000 (01:14 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 16 Oct 2024 12:03:30 +0000 (14:03 +0200)
* lib/javacomp.c (execute_and_read_line): When fdopen fails, terminate
the program.
* lib/javaversion.c (execute_and_read_line): Likewise. When we can't
read a single line, call fclose and wait_subprocess, to free resources.

ChangeLog
lib/javacomp.c
lib/javaversion.c

index cc09d8145974622deabe4cedefc91b9ea755ec2d..d36b122092c13d0b15dd815eaf97a52bc542afff 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-10-06  Bruno Haible  <bruno@clisp.org>
+
+       javacomp, javaversion: Fix resource leak.
+       * lib/javacomp.c (execute_and_read_line): When fdopen fails, terminate
+       the program.
+       * lib/javaversion.c (execute_and_read_line): Likewise. When we can't
+       read a single line, call fclose and wait_subprocess, to free resources.
+
 2024-10-04  Bruno Haible  <bruno@clisp.org>
 
        iconv_open: Fix undefined behaviour.
index 46944f490aff48ec807e35b39fc0ccb76467339c..107f373740ec7ed414a6edce2ddb04e3321cedac 100644 (file)
@@ -352,10 +352,7 @@ execute_and_read_line (const char *progname,
   /* Retrieve its result.  */
   fp = fdopen (fd[0], "r");
   if (fp == NULL)
-    {
-      error (0, errno, _("fdopen() failed"));
-      return NULL;
-    }
+    error (EXIT_FAILURE, errno, _("fdopen() failed"));
 
   line = NULL; linesize = 0;
   linelen = getline (&line, &linesize, fp);
index 60a59def76e473e2745ecc4ef40b69d832c1dd38..fefdfe560de03601d00da19026202ac828b274de 100644 (file)
@@ -61,7 +61,6 @@ execute_and_read_line (const char *progname,
   char *line;
   size_t linesize;
   size_t linelen;
-  int exitstatus;
 
   /* Open a pipe to the JVM.  */
   child = create_pipe_in (progname, prog_path, prog_argv, NULL,
@@ -73,33 +72,35 @@ execute_and_read_line (const char *progname,
   /* Retrieve its result.  */
   fp = fdopen (fd[0], "r");
   if (fp == NULL)
-    {
-      error (0, errno, _("fdopen() failed"));
-      return false;
-    }
+    error (EXIT_FAILURE, errno, _("fdopen() failed"));
 
   line = NULL; linesize = 0;
   linelen = getline (&line, &linesize, fp);
   if (linelen == (size_t)(-1))
     {
       error (0, 0, _("%s subprocess I/O error"), progname);
-      return false;
+      fclose (fp);
+      wait_subprocess (child, progname, true, false, true, false, NULL);
     }
-  if (linelen > 0 && line[linelen - 1] == '\n')
-    line[linelen - 1] = '\0';
+  else
+    {
+      int exitstatus;
 
-  fclose (fp);
+      if (linelen > 0 && line[linelen - 1] == '\n')
+        line[linelen - 1] = '\0';
 
-  /* 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 false;
-    }
+      fclose (fp);
 
-  l->line = line;
+      /* Remove zombie process from process list, and retrieve exit status.  */
+      exitstatus =
+        wait_subprocess (child, progname, true, false, true, false, NULL);
+      if (exitstatus == 0)
+        {
+          l->line = line;
+          return false;
+        }
+    }
+  free (line);
   return false;
 }