1From d998b9f75c79aab68255dace641dd30db239eff6 Mon Sep 17 00:00:00 2001
2From: Kamil Dudka <kdudka@redhat.com>
3Date: Tue, 15 Oct 2013 19:48:41 -0400
4Subject: [PATCH] fix file descriptor leaks reported by cppcheck
5
6Bug: https://bugzilla.redhat.com/785760
7
8Authored by Kamil Dudka <kdudka@redhat.com>.
9
10Upstream-Status: Backport [https://repo.or.cz/libtar.git/commit/abd0274e6b2f708e9eaa29414b07b3f542cec694]
11
12Signed-off-by: Katariina Lounento <katariina.lounento@vaisala.com>
13---
14 lib/append.c    | 14 +++++++++-----
15 lib/extract.c   |  4 ++++
16 libtar/libtar.c |  3 +++
17 3 files changed, 16 insertions(+), 5 deletions(-)
18
19diff --git a/lib/append.c b/lib/append.c
20index e8bd89d..ff58532 100644
21--- a/lib/append.c
22+++ b/lib/append.c
23@@ -216,6 +216,7 @@ tar_append_regfile(TAR *t, const char *realname)
24 	int filefd;
25 	int i, j;
26 	size_t size;
27+	int rv = -1;
28
29 	filefd = open(realname, O_RDONLY);
30 	if (filefd == -1)
31@@ -234,25 +235,28 @@ tar_append_regfile(TAR *t, const char *realname)
32 		{
33 			if (j != -1)
34 				errno = EINVAL;
35-			return -1;
36+			goto fail;
37 		}
38 		if (tar_block_write(t, &block) == -1)
39-			return -1;
40+			goto fail;
41 	}
42
43 	if (i > 0)
44 	{
45 		j = read(filefd, &block, i);
46 		if (j == -1)
47-			return -1;
48+			goto fail;
49 		memset(&(block[i]), 0, T_BLOCKSIZE - i);
50 		if (tar_block_write(t, &block) == -1)
51-			return -1;
52+			goto fail;
53 	}
54
55+	/* success! */
56+	rv = 0;
57+fail:
58 	close(filefd);
59
60-	return 0;
61+	return rv;
62 }
63
64
65diff --git a/lib/extract.c b/lib/extract.c
66index 36357e7..9fc6ad5 100644
67--- a/lib/extract.c
68+++ b/lib/extract.c
69@@ -228,13 +228,17 @@ tar_extract_regfile(TAR *t, char *realname)
70 		{
71 			if (k != -1)
72 				errno = EINVAL;
73+			close(fdout);
74 			return -1;
75 		}
76
77 		/* write block to output file */
78 		if (write(fdout, buf,
79 			  ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : i)) == -1)
80+		{
81+			close(fdout);
82 			return -1;
83+		}
84 	}
85
86 	/* close output file */
87diff --git a/libtar/libtar.c b/libtar/libtar.c
88index 9fa92b2..bb5644c 100644
89--- a/libtar/libtar.c
90+++ b/libtar/libtar.c
91@@ -83,7 +83,10 @@ gzopen_frontend(char *pathname, int oflags, int mode)
92 		return -1;
93
94 	if ((oflags & O_CREAT) && fchmod(fd, mode))
95+	{
96+		close(fd);
97 		return -1;
98+	}
99
100 	gzf = gzdopen(fd, gzoflags);
101 	if (!gzf)
102