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

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