summaryrefslogtreecommitdiff
blob: 98ae0e528c6ca2256763875a448d03fc156e88a4 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
From: Igor Sukhih <igor@openvz.org>
Date: Tue, 22 Aug 2006 13:39:35 +0000 (+0400)
Subject: Fixed memory leaks in realloc()
X-Git-Url: http://git.openvz.org/?p=vzctl;a=commitdiff;h=48bada8af358255c7b54f437ba0002eabf0df368

Fixed memory leaks in realloc()
---

Index: vzctl-3.0.11/src/lib/list.c
===================================================================
--- vzctl-3.0.11.orig/src/lib/list.c
+++ vzctl-3.0.11/src/lib/list.c
@@ -24,7 +24,7 @@
 
 char *list2str_c(char *name, char c, list_head_t *head)
 {
-	char *buf = NULL;
+	char *buf = NULL, *tmp;
 	int buf_len, len, r;
 	char *sp, *ep;
 	const int delta = 256;
@@ -61,9 +61,12 @@ char *list2str_c(char *name, char c, lis
 			int cur_len = sp - buf;
 
 			buf_len += delta > len ? delta : len + 1;
-			buf = realloc(buf, buf_len);
-			if (buf == NULL)
+			tmp = realloc(buf, buf_len);
+			if (tmp == NULL) {
+				free(buf);
 				return NULL;
+			}
+			buf = tmp;
 			ep = buf + buf_len;
 			sp = buf + cur_len;
 		}
Index: vzctl-3.0.11/src/lib/net.c
===================================================================
--- vzctl-3.0.11.orig/src/lib/net.c
+++ vzctl-3.0.11/src/lib/net.c
@@ -349,7 +349,7 @@ static inline int get_vps_ip_ioctl(vps_h
 {
 	int ret = -1;
 	struct vzlist_veipv4ctl veip;
-	uint32_t *addr;
+	uint32_t *addr, *tmp;
 	char buf[16];
 	int i;
 
@@ -366,9 +366,12 @@ static inline int get_vps_ip_ioctl(vps_h
 		else if (ret <= veip.num)
 			break;
 		veip.num = ret;
-		addr = realloc(addr, veip.num * sizeof(*veip.ip));
-		if (addr == NULL)
-			return -1;
+		tmp = realloc(addr, veip.num * sizeof(*veip.ip));
+		if (tmp == NULL) {
+			ret = -1;
+			goto out;
+		}
+		addr = tmp;
 	}
 	if (ret > 0) {
 		for (i = ret - 1; i >= 0; i--) {
Index: vzctl-3.0.11/src/lib/script.c
===================================================================
--- vzctl-3.0.11.orig/src/lib/script.c
+++ vzctl-3.0.11/src/lib/script.c
@@ -42,7 +42,7 @@ static char *envp_bash[] = {"HOME=/", "T
 int read_script(const char *fname, char *include, char **buf)
 {
 	struct stat st;
-	char *p = NULL;
+	char *tmp, *p = NULL;
 	int  fd, len = 0;
 	char *inc;
 
@@ -74,9 +74,10 @@ int read_script(const char *fname, char 
 		goto err;
 	}
 	if (*buf != NULL) {
-		*buf = realloc(*buf, st.st_size + len + 2);
-		if (*buf ==  NULL)
+		tmp = realloc(*buf, st.st_size + len + 2);
+		if (tmp ==  NULL)
 			goto err;
+		*buf = tmp;
 		p = *buf + len;
 	} else {
 		*buf = malloc(st.st_size + 2);
Index: vzctl-3.0.11/src/vzlist.c
===================================================================
--- vzctl-3.0.11.orig/src/vzlist.c
+++ vzctl-3.0.11/src/vzlist.c
@@ -581,11 +581,13 @@ void *x_malloc(int size)
 
 void *x_realloc(void *ptr, int size)
 {
-	if ((ptr = realloc(ptr, size)) == NULL) {
+	void *tmp;
+
+	if ((tmp = realloc(ptr, size)) == NULL) {
 		printf("Error: unable to allocate %d bytes\n", size);
 		exit(1);
 	}
-	return ptr;
+	return tmp;
 }
 
 void usage()
@@ -1637,5 +1639,9 @@ int main(int argc, char **argv)
 		return ret;
 	print_ve();
 	free_veinfo();
+	if (host_pattern != NULL) free(host_pattern);
+	if (name_pattern != NULL) free(name_pattern);
+	if (f_order != NULL) free(f_order);
+
 	return 0;
 }