Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
23 | magnus | 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() |
||
5 | |||
6 | decode: avoid using a static buffer in th_get_pathname() |
||
7 | |||
8 | A solution suggested by Chris Frey: |
||
9 | https://lists.feep.net:8080/pipermail/libtar/2013-October/000377.html |
||
10 | |||
11 | Note this can break programs that expect sizeof(TAR) to be fixed. |
||
12 | |||
13 | --- a/lib/decode.c |
||
14 | +++ b/lib/decode.c |
||
15 | @@ -26,20 +26,30 @@ |
||
16 | char * |
||
17 | th_get_pathname(TAR *t) |
||
18 | { |
||
19 | - static TLS_THREAD char filename[MAXPATHLEN]; |
||
20 | - |
||
21 | if (t->th_buf.gnu_longname) |
||
22 | return t->th_buf.gnu_longname; |
||
23 | |||
24 | - if (t->th_buf.prefix[0] != '\0') |
||
25 | + /* allocate the th_pathname buffer if not already */ |
||
26 | + if (t->th_pathname == NULL) |
||
27 | + { |
||
28 | + t->th_pathname = malloc(MAXPATHLEN * sizeof(char)); |
||
29 | + if (t->th_pathname == NULL) |
||
30 | + /* out of memory */ |
||
31 | + return NULL; |
||
32 | + } |
||
33 | + |
||
34 | + if (t->th_buf.prefix[0] == '\0') |
||
35 | + { |
||
36 | + snprintf(t->th_pathname, MAXPATHLEN, "%.100s", t->th_buf.name); |
||
37 | + } |
||
38 | + else |
||
39 | { |
||
40 | - snprintf(filename, sizeof(filename), "%.155s/%.100s", |
||
41 | + snprintf(t->th_pathname, MAXPATHLEN, "%.155s/%.100s", |
||
42 | t->th_buf.prefix, t->th_buf.name); |
||
43 | - return filename; |
||
44 | } |
||
45 | |||
46 | - snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name); |
||
47 | - return filename; |
||
48 | + /* will be deallocated in tar_close() */ |
||
49 | + return t->th_pathname; |
||
50 | } |
||
51 | |||
52 | |||
53 | --- a/lib/handle.c |
||
54 | +++ b/lib/handle.c |
||
55 | @@ -121,6 +121,7 @@ tar_close(TAR *t) |
||
56 | libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY |
||
57 | ? free |
||
58 | : (libtar_freefunc_t)tar_dev_free)); |
||
59 | + free(t->th_pathname); |
||
60 | free(t); |
||
61 | |||
62 | return i; |
||
63 | --- a/lib/libtar.h |
||
64 | +++ b/lib/libtar.h |
||
65 | @@ -85,6 +85,9 @@ typedef struct |
||
66 | int options; |
||
67 | struct tar_header th_buf; |
||
68 | libtar_hash_t *h; |
||
69 | + |
||
70 | + /* introduced in libtar 1.2.21 */ |
||
71 | + char *th_pathname; |
||
72 | } |
||
73 | TAR; |
||
74 |