From b469d621c0143e652c51bb238fd2060135aa2009 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Tue, 6 Nov 2018 17:24:05 +0100
Subject: [PATCH] libtar: fix programming mistakes detected by static analysis

Authored by Kamil Dudka <kdudka@redhat.com>.

meta-openembedded uses Debian's release tarball [1]. Debian uses
repo.or.cz/libtar.git as their upstream [2]. repo.or.cz/libtar.git has
been inactive since 2013 [3].

Upstream-Status: Inactive-Upstream [lastrelease: 2013 lastcommit: 2013]

[1] https://git.openembedded.org/meta-openembedded/tree/meta-oe/recipes-support/libtar/libtar_1.2.20.bb?h=master#n8
[2] http://svn.kibibyte.se/libtar/trunk/debian/control (rev 51; not tagged)
[3] https://repo.or.cz/libtar.git/shortlog/refs/heads/master

Signed-off-by: Katariina Lounento <katariina.lounento@vaisala.com>
---
 lib/append.c    |  7 +++++++
 lib/wrapper.c   | 11 +++++++----
 libtar/libtar.c |  1 +
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/lib/append.c b/lib/append.c
index ff58532..6386a50 100644
--- a/lib/append.c
+++ b/lib/append.c
@@ -110,9 +110,16 @@ tar_append_file(TAR *t, const char *realname, const char *savename)
 		td->td_dev = s.st_dev;
 		td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash);
 		if (td->td_h == NULL)
+		{
+			free(td);
 			return -1;
+		}
 		if (libtar_hash_add(t->h, td) == -1)
+		{
+			libtar_hash_free(td->td_h, free);
+			free(td);
 			return -1;
+		}
 	}
 	libtar_hashptr_reset(&hp);
 	if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino),
diff --git a/lib/wrapper.c b/lib/wrapper.c
index 44cc435..2d3f5b9 100644
--- a/lib/wrapper.c
+++ b/lib/wrapper.c
@@ -97,6 +97,7 @@ tar_append_tree(TAR *t, char *realdir, char *savedir)
 	struct dirent *dent;
 	DIR *dp;
 	struct stat s;
+	int ret = -1;
 
 #ifdef DEBUG
 	printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n",
@@ -130,24 +131,26 @@ tar_append_tree(TAR *t, char *realdir, char *savedir)
 				 dent->d_name);
 
 		if (lstat(realpath, &s) != 0)
-			return -1;
+			goto fail;
 
 		if (S_ISDIR(s.st_mode))
 		{
 			if (tar_append_tree(t, realpath,
 					    (savedir ? savepath : NULL)) != 0)
-				return -1;
+				goto fail;
 			continue;
 		}
 
 		if (tar_append_file(t, realpath,
 				    (savedir ? savepath : NULL)) != 0)
-			return -1;
+			goto fail;
 	}
 
+	ret = 0;
+fail:
 	closedir(dp);
 
-	return 0;
+	return ret;
 }
 
 
diff --git a/libtar/libtar.c b/libtar/libtar.c
index 23f8741..ac339e7 100644
--- a/libtar/libtar.c
+++ b/libtar/libtar.c
@@ -92,6 +92,7 @@ gzopen_frontend(char *pathname, int oflags, int mode)
 	if (!gzf)
 	{
 		errno = ENOMEM;
+		close(fd);
 		return -1;
 	}
 
