summaryrefslogtreecommitdiff
blob: 816c96925512aae84a24765ab34c7920e7372513 (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
From abd0274e6b2f708e9eaa29414b07b3f542cec694 Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Tue, 15 Oct 2013 19:48:41 -0400
Subject: [PATCH] fix file descriptor leaks reported by cppcheck

Bug: https://bugzilla.redhat.com/785760
---
 lib/append.c    | 14 +++++++++-----
 lib/extract.c   |  4 ++++
 libtar/libtar.c |  3 +++
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/lib/append.c b/lib/append.c
index e8bd89d..ff58532 100644
--- a/lib/append.c
+++ b/lib/append.c
@@ -216,6 +216,7 @@ tar_append_regfile(TAR *t, const char *realname)
 	int filefd;
 	int i, j;
 	size_t size;
+	int rv = -1;
 
 	filefd = open(realname, O_RDONLY);
 	if (filefd == -1)
@@ -234,25 +235,28 @@ tar_append_regfile(TAR *t, const char *realname)
 		{
 			if (j != -1)
 				errno = EINVAL;
-			return -1;
+			goto fail;
 		}
 		if (tar_block_write(t, &block) == -1)
-			return -1;
+			goto fail;
 	}
 
 	if (i > 0)
 	{
 		j = read(filefd, &block, i);
 		if (j == -1)
-			return -1;
+			goto fail;
 		memset(&(block[i]), 0, T_BLOCKSIZE - i);
 		if (tar_block_write(t, &block) == -1)
-			return -1;
+			goto fail;
 	}
 
+	/* success! */
+	rv = 0;
+fail:
 	close(filefd);
 
-	return 0;
+	return rv;
 }
 
 
diff --git a/lib/extract.c b/lib/extract.c
index 36357e7..9fc6ad5 100644
--- a/lib/extract.c
+++ b/lib/extract.c
@@ -228,13 +228,17 @@ tar_extract_regfile(TAR *t, char *realname)
 		{
 			if (k != -1)
 				errno = EINVAL;
+			close(fdout);
 			return -1;
 		}
 
 		/* write block to output file */
 		if (write(fdout, buf,
 			  ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : i)) == -1)
+		{
+			close(fdout);
 			return -1;
+		}
 	}
 
 	/* close output file */
diff --git a/libtar/libtar.c b/libtar/libtar.c
index 9fa92b2..bb5644c 100644
--- a/libtar/libtar.c
+++ b/libtar/libtar.c
@@ -83,7 +83,10 @@ gzopen_frontend(char *pathname, int oflags, int mode)
 		return -1;
 
 	if ((oflags & O_CREAT) && fchmod(fd, mode))
+	{
+		close(fd);
 		return -1;
+	}
 
 	gzf = gzdopen(fd, gzoflags);
 	if (!gzf)
-- 
2.10.5.GIT