1 From: Kamil Dudka <kdudka@redhat.com>
2 Date: Wed, 23 Oct 2013 13:04:22 +0000 (+0200)
3 Origin: http://repo.or.cz/w/libtar.git/commitdiff/ec613af2e9371d7a3e1f7c7a6822164a4255b4d1
4 Subject: decode: avoid using a static buffer in th_get_pathname()
6 decode: avoid using a static buffer in th_get_pathname()
8 A solution suggested by Chris Frey:
9 https://lists.feep.net:8080/pipermail/libtar/2013-October/000377.html
11 Note this can break programs that expect sizeof(TAR) to be fixed.
20 #include <sys/param.h>
25 th_get_pathname(TAR *t)
27 - static TLS_THREAD char filename[MAXPATHLEN];
29 if (t->th_buf.gnu_longname)
30 return t->th_buf.gnu_longname;
32 - if (t->th_buf.prefix[0] != '\0')
33 + /* allocate the th_pathname buffer if not already */
34 + if (t->th_pathname == NULL)
36 + t->th_pathname = malloc(MAXPATHLEN * sizeof(char));
37 + if (t->th_pathname == NULL)
42 + if (t->th_buf.prefix[0] == '\0')
44 + snprintf(t->th_pathname, MAXPATHLEN, "%.100s", t->th_buf.name);
48 - snprintf(filename, sizeof(filename), "%.155s/%.100s",
49 + snprintf(t->th_pathname, MAXPATHLEN, "%.155s/%.100s",
50 t->th_buf.prefix, t->th_buf.name);
54 - snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
56 + /* will be deallocated in tar_close() */
57 + return t->th_pathname;
63 @@ -121,6 +121,7 @@ tar_close(TAR *t)
64 libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
66 : (libtar_freefunc_t)tar_dev_free));
67 + free(t->th_pathname);
73 @@ -85,6 +85,9 @@ typedef struct
75 struct tar_header th_buf;
78 + /* introduced in libtar 1.2.21 */