]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Diagnose sys_exec_info_script failures
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 1 Nov 2024 16:39:33 +0000 (09:39 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 2 Nov 2024 06:47:23 +0000 (23:47 -0700)
* src/system.c (sys_exec_info_script): Diagnose failures in
getline, fclose.  Don’t worry about freeing memory
as caller will immediately exit anyway.

src/system.c

index 1195255ec97a5e1551cdc0c212b8b930f935e814..944f123f56851ae7b43e94e2782df0eab986d6bd 100644 (file)
@@ -830,25 +830,33 @@ sys_exec_info_script (const char **archive_name, int volume_number)
     {
       /* Master */
 
-      int rc;
       int status;
       char *buf = NULL;
       size_t size = 0;
-      FILE *fp;
 
       xclose (p[PWRITE]);
-      fp = fdopen (p[PREAD], "r");
+      FILE *fp = fdopen (p[PREAD], "r");
       if (!fp)
        {
          signal (SIGPIPE, saved_handler);
          call_arg_error ("fdopen", info_script_option);
          return -1;
        }
-      rc = getline (&buf, &size, fp);
-      fclose (fp);
-
-      if (rc > 0 && buf[rc-1] == '\n')
-       buf[--rc] = 0;
+      ssize_t rc = getline (&buf, &size, fp);
+      if (rc < 0)
+       {
+         signal (SIGPIPE, saved_handler);
+         read_error (info_script_option);
+         return -1;
+       }
+      *archive_name = buf;
+      buf[rc - 1] = '\0';
+      if (fclose (fp) < 0)
+       {
+         signal (SIGPIPE, saved_handler);
+         close_error (info_script_option);
+         return -1;
+       }
 
       while (waitpid (pid, &status, 0) < 0)
        if (errno != EINTR)
@@ -859,18 +867,7 @@ sys_exec_info_script (const char **archive_name, int volume_number)
          }
 
       signal (SIGPIPE, saved_handler);
-
-      if (WIFEXITED (status))
-       {
-         if (WEXITSTATUS (status) == 0 && rc > 0)
-           *archive_name = buf;
-         else
-           free (buf);
-         return WEXITSTATUS (status);
-       }
-
-      free (buf);
-      return -1;
+      return WIFEXITED (status) ? WEXITSTATUS (status) : -1;
     }
 
   /* Child */