Blame SOURCES/tar-1.28-sparse-inf-loops.patch

7893ae
From b451bfd224da44e93cf842f23455d755e48421dd Mon Sep 17 00:00:00 2001
7893ae
From: Pavel Raiskup <praiskup@redhat.com>
7893ae
Date: Mon, 28 Jul 2014 08:17:55 +0200
7893ae
Subject: [PATCH 8/9] Fix for infinite loops during sparse file handling
7893ae
7893ae
Upstream bugreport (still downstream):
7893ae
http://www.mail-archive.com/bug-tar@gnu.org/msg04432.html
7893ae
7893ae
Resolves: #1082608
7893ae
7893ae
---
7893ae
 THANKS       |  1 +
7893ae
 src/sparse.c | 48 ++++++++++++++++++++++++++++++++----------------
7893ae
 2 files changed, 33 insertions(+), 16 deletions(-)
7893ae
7893ae
diff --git a/THANKS b/THANKS
7893ae
index b4c5427..e74f71c 100644
7893ae
--- a/THANKS
7893ae
+++ b/THANKS
7893ae
@@ -175,6 +175,7 @@ Fabio d'Alessi		cars@civ.bio.unipd.it
7893ae
 Frank Heckenbach	frank@g-n-u.de
7893ae
 Frank Koenen		koenfr@lidp.com
7893ae
 Franz-Werner Gergen	gergen@edvulx.mpi-stuttgart.mpg.de
7893ae
+François Ouellet	fouell@gmail.com
7893ae
 François Pinard		pinard@iro.umontreal.ca
7893ae
 Fritz Elfert		fritz@fsun.triltsch.de
7893ae
 George Chyu		gschyu@ccgate.dp.beckman.com
7893ae
diff --git a/src/sparse.c b/src/sparse.c
7893ae
index 6a97676..53c1868 100644
7893ae
--- a/src/sparse.c
7893ae
+++ b/src/sparse.c
7893ae
@@ -301,6 +301,7 @@ sparse_dump_region (struct tar_sparse_file *file, size_t i)
7893ae
 {
7893ae
   union block *blk;
7893ae
   off_t bytes_left = file->stat_info->sparse_map[i].numbytes;
7893ae
+  const char *file_name = file->stat_info->orig_file_name;
7893ae
 
7893ae
   if (!lseek_or_error (file, file->stat_info->sparse_map[i].offset))
7893ae
     return false;
7893ae
@@ -314,13 +315,23 @@ sparse_dump_region (struct tar_sparse_file *file, size_t i)
7893ae
       bytes_read = safe_read (file->fd, blk->buffer, bufsize);
7893ae
       if (bytes_read == SAFE_READ_ERROR)
7893ae
 	{
7893ae
-          read_diag_details (file->stat_info->orig_file_name,
7893ae
+          read_diag_details (file_name,
7893ae
 	                     (file->stat_info->sparse_map[i].offset
7893ae
 			      + file->stat_info->sparse_map[i].numbytes
7893ae
 			      - bytes_left),
7893ae
 			     bufsize);
7893ae
 	  return false;
7893ae
 	}
7893ae
+      if (bytes_read == 0)
7893ae
+        {
7893ae
+          char buf[UINTMAX_STRSIZE_BOUND];
7893ae
+          FATAL_ERROR ((0, 0,
7893ae
+                        ngettext ("%s: File shrank by %s byte",
7893ae
+                                  "%s: File shrank by %s bytes",
7893ae
+                                  bytes_left),
7893ae
+                        quotearg_colon (file_name),
7893ae
+                        offtostr (bytes_left, buf)));
7893ae
+        }
7893ae
 
7893ae
       memset (blk->buffer + bytes_read, 0, BLOCKSIZE - bytes_read);
7893ae
       bytes_left -= bytes_read;
7893ae
@@ -475,33 +486,37 @@ sparse_skip_file (struct tar_stat_info *st)
7893ae
 static bool
7893ae
 check_sparse_region (struct tar_sparse_file *file, off_t beg, off_t end)
7893ae
 {
7893ae
-  if (!lseek_or_error (file, beg))
7893ae
+  off_t offset = beg;
7893ae
+
7893ae
+  if (!lseek_or_error (file, offset))
7893ae
     return false;
7893ae
 
7893ae
-  while (beg < end)
7893ae
+  while (offset < end)
7893ae
     {
7893ae
       size_t bytes_read;
7893ae
-      size_t rdsize = BLOCKSIZE < end - beg ? BLOCKSIZE : end - beg;
7893ae
+      size_t rdsize = BLOCKSIZE < end - offset ? BLOCKSIZE : end - offset;
7893ae
       char diff_buffer[BLOCKSIZE];
7893ae
 
7893ae
       bytes_read = safe_read (file->fd, diff_buffer, rdsize);
7893ae
       if (bytes_read == SAFE_READ_ERROR)
7893ae
 	{
7893ae
           read_diag_details (file->stat_info->orig_file_name,
7893ae
-	                     beg,
7893ae
-			     rdsize);
7893ae
-	  return false;
7893ae
-	}
7893ae
-      if (!zero_block_p (diff_buffer, bytes_read))
7893ae
-	{
7893ae
-	  char begbuf[INT_BUFSIZE_BOUND (off_t)];
7893ae
- 	  report_difference (file->stat_info,
7893ae
-			     _("File fragment at %s is not a hole"),
7893ae
-			     offtostr (beg, begbuf));
7893ae
+	                     offset, rdsize);
7893ae
 	  return false;
7893ae
 	}
7893ae
 
7893ae
-      beg += bytes_read;
7893ae
+      if (bytes_read == 0
7893ae
+          || !zero_block_p (diff_buffer, bytes_read))
7893ae
+        {
7893ae
+          char begbuf[INT_BUFSIZE_BOUND (off_t)];
7893ae
+          const char *msg = bytes_read ? _("File fragment at %s is not a hole")
7893ae
+                                       : _("Hole starting at %s is truncated");
7893ae
+
7893ae
+          report_difference (file->stat_info, msg, offtostr (beg, begbuf));
7893ae
+          return false;
7893ae
+        }
7893ae
+
7893ae
+      offset += bytes_read;
7893ae
     }
7893ae
   return true;
7893ae
 }
7893ae
@@ -542,7 +557,8 @@ check_data_region (struct tar_sparse_file *file, size_t i)
7893ae
       file->dumped_size += bytes_read;
7893ae
       size_left -= bytes_read;
7893ae
       mv_size_left (file->stat_info->archive_file_size - file->dumped_size);
7893ae
-      if (memcmp (blk->buffer, diff_buffer, rdsize))
7893ae
+      if (bytes_read == 0
7893ae
+          || memcmp (blk->buffer, diff_buffer, bytes_read))
7893ae
 	{
7893ae
 	  report_difference (file->stat_info, _("Contents differ"));
7893ae
 	  return false;
7893ae
-- 
7893ae
1.9.3
7893ae