483580
diff --git a/src/copyin.c b/src/copyin.c
483580
index d505407..db8ee66 100644
483580
--- a/src/copyin.c
483580
+++ b/src/copyin.c
483580
@@ -124,10 +124,30 @@ tape_skip_padding (int in_file_des, off_t offset)
483580
   if (pad != 0)
483580
     tape_toss_input (in_file_des, pad);
483580
 }
483580
-
483580
+
483580
+static char *
483580
+get_link_name (struct cpio_file_stat *file_hdr, int in_file_des)
483580
+{
483580
+  char *link_name;
483580
+  
483580
+  if (file_hdr->c_filesize < 0 || file_hdr->c_filesize > SIZE_MAX-1)
483580
+    {
483580
+      error (0, 0, _("%s: stored filename length is out of range"),
483580
+	     file_hdr->c_name);
483580
+      link_name = NULL;
483580
+    }
483580
+  else
483580
+    {
483580
+      link_name = xmalloc (file_hdr->c_filesize + 1);
483580
+      tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
483580
+      link_name[file_hdr->c_filesize] = '\0';
483580
+      tape_skip_padding (in_file_des, file_hdr->c_filesize);
483580
+    }
483580
+  return link_name;
483580
+}
483580
 
483580
 static void
483580
-list_file(struct cpio_file_stat* file_hdr, int in_file_des)
483580
+list_file (struct cpio_file_stat* file_hdr, int in_file_des)
483580
 {
483580
   if (verbose_flag)
483580
     {
483580
@@ -136,21 +156,16 @@ list_file(struct cpio_file_stat* file_hdr, int in_file_des)
483580
 	{
483580
 	  if (archive_format != arf_tar && archive_format != arf_ustar)
483580
 	    {
483580
-	      char *link_name = NULL;	/* Name of hard and symbolic links.  */
483580
-
483580
-	      link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
483580
-	      link_name[file_hdr->c_filesize] = '\0';
483580
-	      tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
483580
-	      long_format (file_hdr, link_name);
483580
-	      free (link_name);
483580
-	      tape_skip_padding (in_file_des, file_hdr->c_filesize);
483580
-	      return;
483580
+	      char *link_name = get_link_name (file_hdr, in_file_des);
483580
+	      if (link_name)
483580
+		{
483580
+		  long_format (file_hdr, link_name);
483580
+		  free (link_name);
483580
+		}
483580
 	    }
483580
 	  else
483580
-	    {
483580
-	      long_format (file_hdr, file_hdr->c_tar_linkname);
483580
-	      return;
483580
-	    }
483580
+	    long_format (file_hdr, file_hdr->c_tar_linkname);
483580
+	  return;
483580
 	}
483580
       else
483580
 #endif
483580
@@ -650,10 +665,7 @@ copyin_link(struct cpio_file_stat *file_hdr, int in_file_des)
483580
 
483580
   if (archive_format != arf_tar && archive_format != arf_ustar)
483580
     {
483580
-      link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
483580
-      link_name[file_hdr->c_filesize] = '\0';
483580
-      tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
483580
-      tape_skip_padding (in_file_des, file_hdr->c_filesize);
483580
+      link_name = get_link_name (file_hdr, in_file_des);
483580
     }
483580
   else
483580
     {
483580
diff --git a/tests/Makefile.am b/tests/Makefile.am
483580
index b3e8e60..cf186da 100644
483580
--- a/tests/Makefile.am
483580
+++ b/tests/Makefile.am
483580
@@ -52,6 +52,8 @@ TESTSUITE_AT = \
483580
  setstat04.at\
483580
  setstat05.at\
483580
  symlink.at\
483580
+ symlink-bad-length.at\
483580
+ symlink-long.at\
483580
  version.at
483580
 
483580
 TESTSUITE = $(srcdir)/testsuite
483580
diff --git a/tests/symlink-bad-length.at b/tests/symlink-bad-length.at
483580
new file mode 100644
483580
index 0000000..cbf4aa7
483580
--- /dev/null
483580
+++ b/tests/symlink-bad-length.at
483580
@@ -0,0 +1,49 @@
483580
+# Process this file with autom4te to create testsuite.  -*- Autotest -*-
483580
+# Copyright (C) 2014 Free Software Foundation, Inc.
483580
+
483580
+# This program is free software; you can redistribute it and/or modify
483580
+# it under the terms of the GNU General Public License as published by
483580
+# the Free Software Foundation; either version 3, or (at your option)
483580
+# any later version.
483580
+
483580
+# This program is distributed in the hope that it will be useful,
483580
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
483580
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
483580
+# GNU General Public License for more details.
483580
+
483580
+# You should have received a copy of the GNU General Public License
483580
+# along with this program; if not, write to the Free Software
483580
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
483580
+# 02110-1301 USA.
483580
+
483580
+# Cpio v2.11 did segfault with badly set symlink length.
483580
+# References:
483580
+# http://lists.gnu.org/archive/html/bug-cpio/2014-11/msg00007.html
483580
+
483580
+AT_SETUP([symlink-bad-length])
483580
+AT_KEYWORDS([symlink-long copyout])
483580
+
483580
+AT_DATA([ARCHIVE.base64],
483580
+[x3EjAIBAtIEtJy8nAQAAAHRUYW0FAAAADQBGSUxFAABzb21lIGNvbnRlbnQKAMdxIwBgQ/+hLScv
483580
+JwEAAAB0VEhuBQD/////TElOSwAARklMRcdxAAAAAAAAAAAAAAEAAAAAAAAACwAAAAAAVFJBSUxF
483580
+UiEhIQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
483580
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
483580
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
483580
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
483580
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
483580
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
483580
+AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
483580
+])
483580
+
483580
+AT_CHECK([
483580
+base64 -d ARCHIVE.base64 > ARCHIVE || AT_SKIP_TEST
483580
+cpio -ntv < ARCHIVE
483580
+test $? -eq 2
483580
+],
483580
+[0],
483580
+[-rw-rw-r--   1 10029    10031          13 Nov 25 13:52 FILE
483580
+],[cpio: LINK: stored filename length is out of range
483580
+cpio: premature end of file
483580
+])
483580
+
483580
+AT_CLEANUP
483580
diff --git a/tests/symlink-long.at b/tests/symlink-long.at
483580
new file mode 100644
483580
index 0000000..d3def2d
483580
--- /dev/null
483580
+++ b/tests/symlink-long.at
483580
@@ -0,0 +1,46 @@
483580
+# Process this file with autom4te to create testsuite.  -*- Autotest -*-
483580
+# Copyright (C) 2014 Free Software Foundation, Inc.
483580
+
483580
+# This program is free software; you can redistribute it and/or modify
483580
+# it under the terms of the GNU General Public License as published by
483580
+# the Free Software Foundation; either version 3, or (at your option)
483580
+# any later version.
483580
+
483580
+# This program is distributed in the hope that it will be useful,
483580
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
483580
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
483580
+# GNU General Public License for more details.
483580
+
483580
+# You should have received a copy of the GNU General Public License
483580
+# along with this program; if not, write to the Free Software
483580
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
483580
+# 02110-1301 USA.
483580
+
483580
+# Cpio v2.11.90 changed the way symlink name is read from archive.
483580
+# References:
483580
+# http://lists.gnu.org/archive/html/bug-cpio/2014-11/msg00007.html
483580
+
483580
+AT_SETUP([symlink-long])
483580
+AT_KEYWORDS([symlink-long copyout])
483580
+
483580
+AT_CHECK([
483580
+
483580
+# len(dirname) > READBUFSIZE
483580
+dirname=
483580
+for i in {1..52}; do
483580
+    dirname="xxxxxxxxx/$dirname"
483580
+    mkdir "$dirname"
483580
+done
483580
+ln -s "$dirname" x || AT_SKIP_TEST
483580
+
483580
+echo x | cpio -o > ar
483580
+list=`cpio -tv < ar | sed 's|.*-> ||'`
483580
+test "$list" = "$dirname" && echo success || echo fail
483580
+],
483580
+[0],
483580
+[success
483580
+],[2 blocks
483580
+2 blocks
483580
+])
483580
+
483580
+AT_CLEANUP
483580
diff --git a/tests/testsuite.at b/tests/testsuite.at
483580
index 8f3330b..590bdcb 100644
483580
--- a/tests/testsuite.at
483580
+++ b/tests/testsuite.at
483580
@@ -31,6 +31,8 @@ m4_include([version.at])
483580
 
483580
 m4_include([inout.at])
483580
 m4_include([symlink.at])
483580
+m4_include([symlink-bad-length.at])
483580
+m4_include([symlink-long.at])
483580
 m4_include([interdir.at])
483580
 
483580
 m4_include([setstat01.at])