Blob Blame History Raw
From a4e32c3d29e93866c180b5837f8aee3989dac3e9 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

---
 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;
 	}
 
-- 
2.17.2