cpio: fix unpacking of names with leading slashes
function old new delta
get_header_cpio 968 990 +22
cpio_main 533 526 -7
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/archival/cpio.c b/archival/cpio.c
index 5c16678..067d6e8 100644
--- a/archival/cpio.c
+++ b/archival/cpio.c
@@ -364,7 +364,7 @@
#endif
archive_handle = init_handle();
- archive_handle->src_fd = STDIN_FILENO;
+ /* archive_handle->src_fd = STDIN_FILENO; - done by init_handle */
archive_handle->seek = seek_by_read;
archive_handle->ah_flags = ARCHIVE_EXTRACT_NEWER;
diff --git a/archival/libunarchive/get_header_cpio.c b/archival/libunarchive/get_header_cpio.c
index 302f122..52854df 100644
--- a/archival/libunarchive/get_header_cpio.c
+++ b/archival/libunarchive/get_header_cpio.c
@@ -70,6 +70,15 @@
file_header->name = xzalloc(namesize + 1);
/* Read in filename */
xread(archive_handle->src_fd, file_header->name, namesize);
+ if (file_header->name[0] == '/') {
+ /* Testcase: echo /etc/hosts | cpio -pvd /tmp
+ * Without this code, it tries to unpack /etc/hosts
+ * into "/etc/hosts", not "etc/hosts".
+ */
+ char *p = file_header->name;
+ do p++; while (*p == '/');
+ overlapping_strcpy(file_header->name, p);
+ }
archive_handle->offset += namesize;
/* Update offset amount and skip padding before file contents */