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