Subversion Repositories

?revision_form?Rev ?revision_input??revision_submit??revision_endform?

Rev 23 | Details | Compare with Previous | 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
32 magnus 15
@@ -13,6 +13,7 @@
16
 #include <internal.h>
17
 
18
 #include <stdio.h>
19
+#include <stdlib.h>
20
 #include <sys/param.h>
21
 #include <pwd.h>
22
 #include <grp.h>
23
@@ -26,20 +27,30 @@
23 magnus 24
 char *
25
 th_get_pathname(TAR *t)
26
 {
27
-       static TLS_THREAD char filename[MAXPATHLEN];
28
-
29
        if (t->th_buf.gnu_longname)
30
                return t->th_buf.gnu_longname;
31
 
32
-       if (t->th_buf.prefix[0] != '\0')
33
+       /* allocate the th_pathname buffer if not already */
34
+       if (t->th_pathname == NULL)
35
+       {
36
+               t->th_pathname = malloc(MAXPATHLEN * sizeof(char));
37
+               if (t->th_pathname == NULL)
38
+                       /* out of memory */
39
+                       return NULL;
40
+       }
41
+
42
+       if (t->th_buf.prefix[0] == '\0')
43
+       {
44
+               snprintf(t->th_pathname, MAXPATHLEN, "%.100s", t->th_buf.name);
45
+       }
46
+       else
47
        {
48
-               snprintf(filename, sizeof(filename), "%.155s/%.100s",
49
+               snprintf(t->th_pathname, MAXPATHLEN, "%.155s/%.100s",
50
                         t->th_buf.prefix, t->th_buf.name);
51
-               return filename;
52
        }
53
 
54
-       snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name);
55
-       return filename;
56
+       /* will be deallocated in tar_close() */
57
+       return t->th_pathname;
58
 }
59
 
60
 
61
--- a/lib/handle.c
62
+++ b/lib/handle.c
63
@@ -121,6 +121,7 @@ tar_close(TAR *t)
64
                libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY
65
                                        ? free
66
                                        : (libtar_freefunc_t)tar_dev_free));
67
+       free(t->th_pathname);
68
        free(t);
69
 
70
        return i;
71
--- a/lib/libtar.h
72
+++ b/lib/libtar.h
73
@@ -85,6 +85,9 @@ typedef struct
74
        int options;
75
        struct tar_header th_buf;
76
        libtar_hash_t *h;
77
+
78
+       /* introduced in libtar 1.2.21 */
79
+       char *th_pathname;
80
 }
81
 TAR;
82