[hackers] [sbase] Avoid infinite loop in tar || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Sat, 20 Jul 2013 19:18:19 +0200

commit f636ac791bd87faa9556e0c6ed858777dd8295c6
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
Date: Sat Jul 20 18:08:58 2013 +0200

    Avoid infinite loop in tar
    
    When the tar file is written in one directory archived by tar
    the function archive enters in an infinite loop due to de
    tar file written. This patch avoid this case checking the
    inode of the tar file before of adding it to the archive.

diff --git a/tar.c b/tar.c
index 87b7236..0f3ce33 100644
--- a/tar.c
+++ b/tar.c
_AT_@ -47,6 +47,7 @@ static void c(char *);
 static void xt(int (*)(char*, int, char[Blksiz]));
 
 static FILE *tarfile;
+static ino_t tarinode;
 
 static void
 usage(void)
_AT_@ -117,9 +118,14 @@ main(int argc, char *argv[])
                 usage();
 
         if(file) {
+ struct stat st;
+
                 tarfile = fopen(file, (mode == 'c') ? "wb" : "rb");
                 if(!tarfile)
                         eprintf("tar: open '%s':", file);
+ if (lstat(file, &st) < 0)
+ eprintf("tar: stat '%s':", file);
+ tarinode = st.st_ino;
         } else {
                 tarfile = (mode == 'c') ? stdout : stdin;
         }
_AT_@ -155,6 +161,10 @@ archive(const char* path, const struct stat* sta, int type)
         mode_t mode;
 
         lstat(path, &st);
+ if (st.st_ino == tarinode) {
+ fprintf(stderr, "ignoring '%s'
", path);
+ return 0;
+ }
         pw = getpwuid(st.st_uid);
         gr = getgrgid(st.st_gid);
 
Received on Sat Jul 20 2013 - 19:18:19 CEST

This archive was generated by hypermail 2.3.0 : Sat Jul 20 2013 - 19:24:13 CEST