From de06580d3227fc662fc89da9a922c5fabb9d792b Mon Sep 17 00:00:00 2001 From: Nick White Date: Fri, 5 Jun 2015 18:22:35 +0100 Subject: [PATCH] Strip leading ./ from path names and don't try to extract '.' directory There was a bug where a tarball created using '.' or './' would not be extracted, because tar would attempt to remove the '.' directory before extracting. E.g. ; tar -c . > ../t.tar ; tar -x < ../t.tar tar: remove ./: Invalid argument This fixes that by ignoring a '.' directory when extracting. Also strip any leading './'s, so './././././' will be sanitized to to '', which will be ignored. This made the above fix easier, but also arguably improves the interface, as tar -t myfile < t.tar works whether myfile was named 'myfile' or './myfile' in the tarball. --- tar.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tar.c b/tar.c index 0bd3fcf..7bc9687 100644 --- a/tar.c +++ b/tar.c @@ -367,7 +367,7 @@ sanitizepath(char *p) /* Strip leading '/' characters */ while(*p == '/') { l = strlen(p); - memmove(p, p+1, l - 1); + memmove(p, p + 1, l - 1); *(p + l - 1) = '\0'; } @@ -377,6 +377,13 @@ sanitizepath(char *p) memmove(s, s + 3, l - 3); *(s + l - 3) = '\0'; } + + /* Strip leading './'s */ + while(!strncmp(p, "./", 2)) { + l = strlen(p); + memmove(p, p + 2, l - 2); + *(p + l - 2) = '\0'; + } } static void @@ -430,6 +437,10 @@ xt(int argc, char *argv[], int (*fn)(char *, ssize_t, char[BLKSIZ])) (int)sizeof(h->name), h->name); sanitizepath(fname); + if(fname[0] == '\0' || + (fname[0] == '.' && fname[1] == '\0')) + continue; + if ((size = strtol(h->size, &p, 8)) < 0 || *p != '\0') eprintf("strtol %s: invalid number\n", h->size); -- 2.1.4