summaryrefslogtreecommitdiffstats
path: root/meta-oe/recipes-support/libtar/files/0007-fix-memleaks-in-libtar-sample-program.patch
blob: f88bcbf9cf4783809799cf1444dc0a6598fe1725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
From e3888e452aee72e0d658185ac20e8e63bed1aff8 Mon Sep 17 00:00:00 2001
From: Huzaifa Sidhpurwala <huzaifas@fedoraproject.org>
Date: Tue, 15 Oct 2013 20:05:04 -0400
Subject: [PATCH] fix memleaks in libtar sample program

Authored by Huzaifa Sidhpurwala <huzaifas@fedoraproject.org>.

Upstream-Status: Backport [https://repo.or.cz/libtar.git/commit/f3c711cf3054ff366a1a3500cdc8c64ecc2d2da6]

Signed-off-by: Katariina Lounento <katariina.lounento@vaisala.com>
---
 libtar/libtar.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/libtar/libtar.c b/libtar/libtar.c
index bb5644c..23f8741 100644
--- a/libtar/libtar.c
+++ b/libtar/libtar.c
@@ -253,6 +253,7 @@ extract(char *tarfile, char *rootdir)
 	if (tar_extract_all(t, rootdir) != 0)
 	{
 		fprintf(stderr, "tar_extract_all(): %s\n", strerror(errno));
+		tar_close(t);
 		return -1;
 	}
 
@@ -270,12 +271,13 @@ extract(char *tarfile, char *rootdir)
 
 
 void
-usage()
+usage(void *rootdir)
 {
 	printf("Usage: %s [-C rootdir] [-g] [-z] -x|-t filename.tar\n",
 	       progname);
 	printf("       %s [-C rootdir] [-g] [-z] -c filename.tar ...\n",
 	       progname);
+	free(rootdir);
 	exit(-1);
 }
 
@@ -292,6 +294,7 @@ main(int argc, char *argv[])
 	int c;
 	int mode = 0;
 	libtar_list_t *l;
+	int return_code = -2;
 
 	progname = basename(argv[0]);
 
@@ -313,17 +316,17 @@ main(int argc, char *argv[])
 			break;
 		case 'c':
 			if (mode)
-				usage();
+				usage(rootdir);
 			mode = MODE_CREATE;
 			break;
 		case 'x':
 			if (mode)
-				usage();
+				usage(rootdir);
 			mode = MODE_EXTRACT;
 			break;
 		case 't':
 			if (mode)
-				usage();
+				usage(rootdir);
 			mode = MODE_LIST;
 			break;
 #ifdef HAVE_LIBZ
@@ -332,7 +335,7 @@ main(int argc, char *argv[])
 			break;
 #endif /* HAVE_LIBZ */
 		default:
-			usage();
+			usage(rootdir);
 		}
 
 	if (!mode || ((argc - optind) < (mode == MODE_CREATE ? 2 : 1)))
@@ -341,7 +344,7 @@ main(int argc, char *argv[])
 		printf("argc - optind == %d\tmode == %d\n", argc - optind,
 		       mode);
 #endif
-		usage();
+		usage(rootdir);
 	}
 
 #ifdef DEBUG
@@ -351,21 +354,25 @@ main(int argc, char *argv[])
 	switch (mode)
 	{
 	case MODE_EXTRACT:
-		return extract(argv[optind], rootdir);
+		return_code = extract(argv[optind], rootdir);
+		break;
 	case MODE_CREATE:
 		tarfile = argv[optind];
 		l = libtar_list_new(LIST_QUEUE, NULL);
 		for (c = optind + 1; c < argc; c++)
 			libtar_list_add(l, argv[c]);
-		return create(tarfile, rootdir, l);
+		return_code = create(tarfile, rootdir, l);
+		libtar_list_free(l, NULL);
+		break;
 	case MODE_LIST:
-		return list(argv[optind]);
+		return_code = list(argv[optind]);
+		break;
 	default:
 		break;
 	}
 
-	/* NOTREACHED */
-	return -2;
+	free(rootdir);
+	return return_code;
 }