decode: avoid using a static buffer in th_get_pathname()
authorKamil Dudka <kdudka@redhat.com>
Wed, 23 Oct 2013 13:04:22 +0000 (23 15:04 +0200)
committerChris Frey <cdfrey@foursquare.net>
Thu, 24 Oct 2013 21:54:08 +0000 (24 17:54 -0400)
A solution suggested by Chris Frey:
https://lists.feep.net:8080/pipermail/libtar/2013-October/000377.html

Note this can break programs that expect sizeof(TAR) to be fixed.

lib/decode.c
lib/handle.c
lib/libtar.h

index c16ea2d..edb2185 100644 (file)
 char *
 th_get_pathname(TAR *t)
 {
-       static TLS_THREAD char filename[MAXPATHLEN];
-
        if (t->th_buf.gnu_longname)
                return t->th_buf.gnu_longname;
 
-       if (t->th_buf.prefix[0] != '\0')
+       /* allocate the th_pathname buffer if not already */
+       if (t->th_pathname == NULL)
+       {
+               t->th_pathname = malloc(MAXPATHLEN * sizeof(char));
+               if (t->th_pathname == NULL)
+                       /* out of memory */
+                       return NULL;
+       }
+
+       if (t->th_buf.prefix[0] == '\0')
+       {
+               snprintf(t->th_pathname, MAXPATHLEN, "%.100s", t->th_buf.name);
+       }
+       else
        {
-               snprintf(filename, sizeof(filename), "%.155s/%.100s",
+               snprintf(t->th_pathname, MAXPATHLEN, "%.155s/%.100s",
                         t->th_buf.prefix, t->th_buf.name);
-               return filename;
        }
 
-       snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
-       return filename;
+       /* will be deallocated in tar_close() */
+       return t->th_pathname;
 }
 
 
index 002d23c..a19c046 100644 (file)
@@ -122,6 +122,7 @@ tar_close(TAR *t)
                libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
                                        ? free
                                        : (libtar_freefunc_t)tar_dev_free));
+       free(t->th_pathname);
        free(t);
 
        return i;
index 616ca8f..2fefee0 100644 (file)
@@ -86,6 +86,9 @@ typedef struct
        int options;
        struct tar_header th_buf;
        libtar_hash_t *h;
+
+       /* introduced in libtar 1.2.21 */
+       char *th_pathname;
 }
 TAR;