1From b469d621c0143e652c51bb238fd2060135aa2009 Mon Sep 17 00:00:00 2001
2From: Kamil Dudka <kdudka@redhat.com>
3Date: Tue, 6 Nov 2018 17:24:05 +0100
4Subject: [PATCH] libtar: fix programming mistakes detected by static analysis
5
6Authored by Kamil Dudka <kdudka@redhat.com>.
7
8meta-openembedded uses Debian's release tarball [1]. Debian uses
9repo.or.cz/libtar.git as their upstream [2]. repo.or.cz/libtar.git has
10been inactive since 2013 [3].
11
12Upstream-Status: Inactive-Upstream [lastrelease: 2013 lastcommit: 2013]
13
14[1] https://git.openembedded.org/meta-openembedded/tree/meta-oe/recipes-support/libtar/libtar_1.2.20.bb?h=master#n8
15[2] http://svn.kibibyte.se/libtar/trunk/debian/control (rev 51; not tagged)
16[3] https://repo.or.cz/libtar.git/shortlog/refs/heads/master
17
18Signed-off-by: Katariina Lounento <katariina.lounento@vaisala.com>
19---
20 lib/append.c    |  7 +++++++
21 lib/wrapper.c   | 11 +++++++----
22 libtar/libtar.c |  1 +
23 3 files changed, 15 insertions(+), 4 deletions(-)
24
25diff --git a/lib/append.c b/lib/append.c
26index ff58532..6386a50 100644
27--- a/lib/append.c
28+++ b/lib/append.c
29@@ -110,9 +110,16 @@ tar_append_file(TAR *t, const char *realname, const char *savename)
30 		td->td_dev = s.st_dev;
31 		td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash);
32 		if (td->td_h == NULL)
33+		{
34+			free(td);
35 			return -1;
36+		}
37 		if (libtar_hash_add(t->h, td) == -1)
38+		{
39+			libtar_hash_free(td->td_h, free);
40+			free(td);
41 			return -1;
42+		}
43 	}
44 	libtar_hashptr_reset(&hp);
45 	if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino),
46diff --git a/lib/wrapper.c b/lib/wrapper.c
47index 44cc435..2d3f5b9 100644
48--- a/lib/wrapper.c
49+++ b/lib/wrapper.c
50@@ -97,6 +97,7 @@ tar_append_tree(TAR *t, char *realdir, char *savedir)
51 	struct dirent *dent;
52 	DIR *dp;
53 	struct stat s;
54+	int ret = -1;
55
56 #ifdef DEBUG
57 	printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
58@@ -130,24 +131,26 @@ tar_append_tree(TAR *t, char *realdir, char *savedir)
59 				 dent->d_name);
60
61 		if (lstat(realpath, &s) != 0)
62-			return -1;
63+			goto fail;
64
65 		if (S_ISDIR(s.st_mode))
66 		{
67 			if (tar_append_tree(t, realpath,
68 					    (savedir ? savepath : NULL)) != 0)
69-				return -1;
70+				goto fail;
71 			continue;
72 		}
73
74 		if (tar_append_file(t, realpath,
75 				    (savedir ? savepath : NULL)) != 0)
76-			return -1;
77+			goto fail;
78 	}
79
80+	ret = 0;
81+fail:
82 	closedir(dp);
83
84-	return 0;
85+	return ret;
86 }
87
88
89diff --git a/libtar/libtar.c b/libtar/libtar.c
90index 23f8741..ac339e7 100644
91--- a/libtar/libtar.c
92+++ b/libtar/libtar.c
93@@ -92,6 +92,7 @@ gzopen_frontend(char *pathname, int oflags, int mode)
94 	if (!gzf)
95 	{
96 		errno = ENOMEM;
97+		close(fd);
98 		return -1;
99 	}
100
101