]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
xsparse dry runs should not create output
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 3 Aug 2024 04:58:53 +0000 (21:58 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 4 Aug 2024 08:41:43 +0000 (01:41 -0700)
* scripts/xsparse.c (expand_sparse, main): Check for syscall
failure.  Do not create output file if a dry run.

scripts/xsparse.c

index 704bb266717473451311438e5b2713eb22c966af..a6c36602c2ec1e2e28fbd4dcc4dbb430df255124 100644 (file)
@@ -106,7 +106,7 @@ get_line (char *s, int size, FILE *stream)
     die (1, "unexpected end of file");
   len = strlen (p);
   if (s[len - 1] != '\n')
-    die (1, "buffer overflow");
+    die (1, "invalid or too-long data");
   s[len - 1] = '\0';
 }
 
@@ -300,17 +300,25 @@ expand_sparse (FILE *sfp, int ofd)
       off_t size = sparse_map[i].numbytes;
 
       if (size == 0)
-       ftruncate (ofd, sparse_map[i].offset);
+       {
+         if (0 <= ofd && ftruncate (ofd, sparse_map[i].offset) < 0)
+           die (1, "ftruncate error (%d)", errno);
+       }
       else
        {
-         lseek (ofd, sparse_map[i].offset, SEEK_SET);
+         if (0 <= ofd && lseek (ofd, sparse_map[i].offset, SEEK_SET) < 0)
+           die (1, "lseek error (%d)", errno);
          while (size)
            {
              size_t rdsize = (size < maxbytes) ? size : maxbytes;
              if (rdsize != fread (buffer, 1, rdsize, sfp))
                die (1, "read error (%d)", errno);
-             if (rdsize != write (ofd, buffer, rdsize))
-               die (1, "write error (%d)", errno);
+             if (0 <= ofd)
+               {
+                 ssize_t written = write (ofd, buffer, rdsize);
+                 if (written != rdsize)
+                   die (1, "write error (%d)", written < 0 ? errno : 0);
+               }
              size -= rdsize;
            }
        }
@@ -405,17 +413,15 @@ main (int argc, char **argv)
   if (!outname)
     guess_outname (inname);
 
-  int ofd = open (outname, O_RDWR|O_CREAT|O_TRUNC, st.st_mode);
-  if (ofd < 0)
-    die (1, "cannot open file %s (%d)", outname, errno);
-
   if (verbose)
     printf ("Expanding file '%s' to '%s'\n", inname, outname);
 
-  if (dry_run)
+  int ofd = -1;
+  if (!dry_run)
     {
-      printf ("Finished dry run\n");
-      return 0;
+      ofd = open (outname, O_RDWR | O_CREAT | O_TRUNC, st.st_mode);
+      if (ofd < 0)
+       die (1, "cannot open file %s (%d)", outname, errno);
     }
 
   expand_sparse (ifp, ofd);
@@ -428,6 +434,12 @@ main (int argc, char **argv)
   if (verbose)
     printf ("Done\n");
 
+  if (dry_run)
+    {
+      printf ("Finished dry run\n");
+      return 0;
+    }
+
   if (outsize)
     {
       if (stat (outname, &st) < 0)