dd3c47
From 5e1e4fae6a9eab069866eb46b2f91d60fae34efe Mon Sep 17 00:00:00 2001
dd3c47
From: Sergey Poznyakoff <gray@gnu.org.ua>
dd3c47
Date: Thu, 11 Dec 2014 12:51:21 +0200
dd3c47
Subject: [PATCH] Fix error recovery in copy-in mode
dd3c47
dd3c47
(+ complete sync of symlink-bad-length.at test)
dd3c47
dd3c47
* src/copyin.c (copyin_link): Fix null dereference.
dd3c47
(read_in_header): Fix error recovery (bug introduced by
dd3c47
27e0ae55).
dd3c47
* tests/symlink-bad-length.at: Test error recovery.
dd3c47
Catch various architecture-dependent error messages (suggested
dd3c47
by Pavel Raiskup).
dd3c47
---
dd3c47
 src/copyin.c                | 10 ++++++----
dd3c47
 tests/symlink-bad-length.at | 16 ++++++++++++----
dd3c47
 2 files changed, 18 insertions(+), 8 deletions(-)
dd3c47
dd3c47
diff --git a/src/copyin.c b/src/copyin.c
dd3c47
index 29d0fb1..274bd49 100644
dd3c47
--- a/src/copyin.c
dd3c47
+++ b/src/copyin.c
dd3c47
@@ -655,7 +655,7 @@ copyin_device (struct cpio_file_stat* file_hdr)
dd3c47
 }
dd3c47
 
dd3c47
 static void
dd3c47
-copyin_link(struct cpio_file_stat *file_hdr, int in_file_des)
dd3c47
+copyin_link (struct cpio_file_stat *file_hdr, int in_file_des)
dd3c47
 {
dd3c47
   char *link_name = NULL;	/* Name of hard and symbolic links.  */
dd3c47
   int res;			/* Result of various function calls.  */
dd3c47
@@ -666,6 +666,8 @@ copyin_link(struct cpio_file_stat *file_hdr, int in_file_des)
dd3c47
   if (archive_format != arf_tar && archive_format != arf_ustar)
dd3c47
     {
dd3c47
       link_name = get_link_name (file_hdr, in_file_des);
dd3c47
+      if (!link_name)
dd3c47
+	return;
dd3c47
     }
dd3c47
   else
dd3c47
     {
dd3c47
@@ -1020,7 +1022,7 @@ read_in_header (struct cpio_file_stat *file_hdr, int in_des)
dd3c47
 
dd3c47
   file_hdr->c_tar_linkname = NULL;
dd3c47
 
dd3c47
-  tape_buffered_read (magic.str, in_des, 6L);
dd3c47
+  tape_buffered_read (magic.str, in_des, sizeof (magic.str));
dd3c47
   while (1)
dd3c47
     {
dd3c47
       if (append_flag)
dd3c47
@@ -1065,8 +1067,8 @@ read_in_header (struct cpio_file_stat *file_hdr, int in_des)
dd3c47
 	  break;
dd3c47
 	}
dd3c47
       bytes_skipped++;
dd3c47
-      memmove (magic.str, magic.str + 1, 5);
dd3c47
-      tape_buffered_read (magic.str, in_des, 1L);
dd3c47
+      memmove (magic.str, magic.str + 1, sizeof (magic.str) - 1);
dd3c47
+      tape_buffered_read (magic.str + sizeof (magic.str) - 1, in_des, 1L);
dd3c47
     }
dd3c47
 }
dd3c47
 
dd3c47
diff --git a/tests/symlink-bad-length.at b/tests/symlink-bad-length.at
dd3c47
index 84123a4..fd2c87e 100644
dd3c47
--- a/tests/symlink-bad-length.at
dd3c47
+++ b/tests/symlink-bad-length.at
dd3c47
@@ -1,5 +1,5 @@
dd3c47
 # Process this file with autom4te to create testsuite.  -*- Autotest -*-
dd3c47
-# Copyright (C) 2014 Free Software Foundation, Inc.
dd3c47
+# Copyright (C) 2014-2015, 2017 Free Software Foundation, Inc.
dd3c47
 
dd3c47
 # This program is free software; you can redistribute it and/or modify
dd3c47
 # it under the terms of the GNU General Public License as published by
dd3c47
@@ -35,18 +35,26 @@ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
dd3c47
 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
dd3c47
 ])
dd3c47
 
dd3c47
+# The exact error message and exit status depend on the host architecture,
dd3c47
+# therefore strderr is filtered out and error code is not checked.
dd3c47
+
dd3c47
+# So far the only case when cpio would exit with code 0 is when it skips
dd3c47
+# several bytes and encounters a valid record header.  Perhaps it should
dd3c47
+# exit with code 2 (non-critical error), if at least one byte was skipped,
dd3c47
+# but that could hurt backward compatibility.
dd3c47
+
dd3c47
 AT_CHECK([
dd3c47
 base64 -d ARCHIVE.base64 > ARCHIVE || AT_SKIP_TEST
dd3c47
-TZ=UTC cpio -ntv < ARCHIVE 2>stderr
dd3c47
-rc=$?
dd3c47
+TZ=UTC cpio -ntv < ARCHIVE 2>stderr 
dd3c47
 cat stderr | grep -v \
dd3c47
     -e 'stored filename length is out of range' \
dd3c47
     -e 'premature end of file' \
dd3c47
     -e 'archive header has reverse byte-order' \
dd3c47
     -e 'memory exhausted' \
dd3c47
+    -e 'skipped [[0-9][0-9]*] bytes of junk' \
dd3c47
+    -e '[[0-9][0-9]*] block' \
dd3c47
     >&2
dd3c47
 echo >&2 STDERR
dd3c47
-test "$rc" -ne 0
dd3c47
 ],
dd3c47
 [0],
dd3c47
 [-rw-rw-r--   1 10029    10031          13 Nov 25  2014 FILE
dd3c47
-- 
dd3c47
2.9.3
dd3c47