Merge lp:~cbehrens/agent-smith/error-fixes into lp:agent-smith

Proposed by Chris Behrens
Status: Merged
Merged at revision: 71
Proposed branch: lp:~cbehrens/agent-smith/error-fixes
Merge into: lp:agent-smith
Diff against target: 295 lines (+103/-60)
6 files modified
configure.ac (+1/-1)
src/slist.c (+3/-0)
src/spool.c (+39/-14)
src/util.c (+56/-41)
tests/Makefile.am (+3/-3)
tests/check_spool.c (+1/-1)
To merge this branch: bzr merge lp:~cbehrens/agent-smith/error-fixes
Reviewer Review Type Date Requested Status
Chris Behrens Approve
Review via email: mp+33488@code.launchpad.net

Description of the change

Unfortunately I don't have a way to compile this right now, but the diff looks sane. This contains a bunch of error checking fixes. It also fixes at least 1 memory leak under normal conditions and a few under error conditions...

To post a comment you must log in.
lp:~cbehrens/agent-smith/error-fixes updated
71. By Chris Behrens

fix so the tests at least build...
missing argument to strbuf_free
autoreconf requirement dropped a rev since I'm using older ubuntu for now

Revision history for this message
Chris Behrens (cbehrens) wrote :

Looks ok now after fixing compile issues and making tests build.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'configure.ac'
2--- configure.ac 2010-08-09 13:17:54 +0000
3+++ configure.ac 2010-08-24 15:57:43 +0000
4@@ -1,4 +1,4 @@
5-AC_PREREQ([2.65])
6+AC_PREREQ([2.64])
7 AC_INIT([agentsmith], [0.1], [soren.hansen@rackspace.com])
8 AC_CONFIG_SRCDIR([src/xen.h])
9 AC_CONFIG_HEADERS([config.h])
10
11=== modified file 'src/slist.c'
12--- src/slist.c 2010-07-07 09:26:52 +0000
13+++ src/slist.c 2010-08-24 15:57:43 +0000
14@@ -19,6 +19,9 @@
15 SList *slist_append(SList *list, void *data) {
16 SList *elem = malloc(sizeof(SList));
17
18+ if (elem == NULL)
19+ return NULL;
20+
21 elem->data = data;
22 elem->next = NULL;
23
24
25=== modified file 'src/spool.c'
26--- src/spool.c 2010-08-23 23:18:52 +0000
27+++ src/spool.c 2010-08-24 15:57:43 +0000
28@@ -43,7 +43,7 @@
29 int spool_init(void) {
30 char *tmp;
31
32- if (spool_ensure_dir(tmp = spool_incoming_dir()) == 0)
33+ if (spool_ensure_dir(tmp = spool_incoming_dir()) < 0)
34 free(tmp);
35 else {
36 syslog(LOG_DAEMON | LOG_CRIT, "Failed to create incoming spool dir: %s", tmp);
37@@ -51,7 +51,7 @@
38 return -1;
39 }
40
41- if (spool_ensure_dir(tmp = spool_outgoing_dir()) == 0)
42+ if (spool_ensure_dir(tmp = spool_outgoing_dir()) < 0)
43 free(tmp);
44 else {
45 syslog(LOG_DAEMON | LOG_CRIT, "Failed to create outgoing spool dir: %s", tmp);
46@@ -90,7 +90,12 @@
47
48 basedir = spool_basedir();
49 retval = util_join_paths(basedir, in ? INCOMING_DIR : OUTGOING_DIR);
50- spool_ensure_dir(retval);
51+ if (retval == NULL)
52+ return NULL;
53+ if (spool_ensure_dir(retval) < 0) {
54+ free(retval);
55+ return NULL;
56+ }
57 return retval;
58 }
59
60@@ -207,14 +212,24 @@
61 char *dir, *msgpath;
62
63 dir = spool_outgoing_dir();
64+ if (dir == NULL)
65+ return -1;
66
67- if (spool_ensure_dir(dir) < 0)
68+ if (spool_ensure_dir(dir) < 0) {
69+ free(dir);
70 return -1;
71+ }
72
73 msgpath = util_join_paths(dir, msgid);
74
75- if (spool_dump_buf_to_file(buf, buflen, msgpath) < 0)
76+ free(dir);
77+
78+ if (spool_dump_buf_to_file(buf, buflen, msgpath) < 0) {
79+ free(msgpath);
80 return -1;
81+ }
82+
83+ free(msgpath);
84
85 return 0;
86 }
87@@ -229,26 +244,36 @@
88 if (inotify_fd == -1) {
89 inotify_fd = inotify_init();
90 if (inotify_fd < 0)
91- return 0;
92+ return -1;
93
94 if (fcntl(inotify_fd, F_SETFL, O_NONBLOCK) < 0) {
95 close(inotify_fd);
96- return 0;
97+ inotify_fd = -1;
98+ return -1;
99 }
100 }
101
102 incoming_dir = spool_incoming_dir();
103- if (spool_ensure_dir(incoming_dir) < 0)
104- return 0;
105+ if (spool_ensure_dir(incoming_dir) < 0) {
106+ free(incoming_dir);
107+ return -1;
108+ }
109
110- if (inotify_add_watch(inotify_fd, incoming_dir, IN_CLOSE_WRITE) < 0)
111+ if (inotify_add_watch(inotify_fd, incoming_dir, IN_CLOSE_WRITE) < 0) {
112+ free(incoming_dir);
113 perror("inotify_add_watch failed");
114+ return -1;
115+ }
116+
117+ free(incoming_dir);
118
119 callbacks = slist_append(callbacks, cb);
120-
121- free(incoming_dir);
122-
123- return 1;
124+ if (callbacks == NULL)
125+ {
126+ return -1;
127+ }
128+
129+ return 0;
130 }
131
132 #define INOTIFY_BUFF_SIZE ((sizeof(struct inotify_event)+FILENAME_MAX))
133
134=== modified file 'src/util.c'
135--- src/util.c 2010-08-20 15:18:18 +0000
136+++ src/util.c 2010-08-24 15:57:43 +0000
137@@ -25,17 +25,18 @@
138 * @returns: a pointer to a strbuf on success, NULL otherwise
139 */
140 strbuf *strbuf_init() {
141- strbuf *str;
142- if ((str = malloc(sizeof(strbuf))) == NULL)
143- return NULL;
144-
145- if ((str->str = strdup("")) == NULL) {
146- free(str);
147- return NULL;
148- }
149-
150- str->len = 0;
151- return str;
152+ strbuf *str;
153+
154+ if ((str = malloc(sizeof(strbuf))) == NULL)
155+ return NULL;
156+
157+ if ((str->str = strdup("")) == NULL) {
158+ free(str);
159+ return NULL;
160+ }
161+
162+ str->len = 0;
163+ return str;
164 }
165
166 /*
167@@ -46,35 +47,41 @@
168 * freed, otherwise it is returned.
169 */
170 char *strbuf_free(strbuf *str, char free_it) {
171- char *s = str->str;
172-
173- free(str);
174-
175- if (free_it) {
176- free(s);
177- return NULL;
178- } else {
179- return s;
180- }
181+ char *s = str->str;
182+
183+ free(str);
184+
185+ if (free_it) {
186+ free(s);
187+ return NULL;
188+ } else {
189+ return s;
190+ }
191 }
192
193 strbuf *strbuf_append_printf(strbuf *str, const char *fmt, ...) {
194- char *s = NULL;
195- size_t strlen;
196- va_list ap;
197-
198- va_start(ap, fmt);
199- strlen = vsnprintf(s, 0, fmt, ap);
200- va_end(ap);
201-
202- str->str = realloc(str->str, str->len + strlen + 1);
203- va_start(ap, fmt);
204- vsnprintf(str->str+str->len, strlen+1, fmt, ap);
205- va_end(ap);
206-
207- str->len+=strlen;
208-
209- return str;
210+ char *s = NULL;
211+ void *vptr;
212+ size_t strlen;
213+ va_list ap;
214+
215+ va_start(ap, fmt);
216+ strlen = vsnprintf(s, 0, fmt, ap);
217+ va_end(ap);
218+
219+ vptr = realloc(str->str, str->len + strlen + 1);
220+ if (vptr == NULL)
221+ return NULL;
222+
223+ str->str = vptr;
224+
225+ va_start(ap, fmt);
226+ vsnprintf(str->str+str->len, strlen+1, fmt, ap);
227+ va_end(ap);
228+
229+ str->len+=strlen;
230+
231+ return str;
232 }
233
234 /**
235@@ -85,9 +92,17 @@
236 * @return Newly allocated string: part1 + "/" + part2
237 */
238 char *util_join_paths(const char *part1, const char *part2) {
239- strbuf *str;
240+ strbuf *str;
241+ strbuf *nstr;
242
243- str = strbuf_init();
244- str = strbuf_append_printf(str, "%s/%s", part1, part2);
245- return strbuf_free(str, 0);
246+ str = strbuf_init();
247+ if (str == NULL)
248+ return NULL;
249+ nstr = strbuf_append_printf(str, "%s/%s", part1, part2);
250+ if (nstr == NULL) {
251+ strbuf_free(str, 1);
252+ return NULL;
253+ }
254+ str = nstr;
255+ return strbuf_free(str, 0);
256 }
257
258=== modified file 'tests/Makefile.am'
259--- tests/Makefile.am 2010-07-20 17:18:16 +0000
260+++ tests/Makefile.am 2010-08-24 15:57:43 +0000
261@@ -8,13 +8,13 @@
262 check_slist_LDADD = $(top_builddir)/src/slist.o @CHECK_LIBS@
263
264 check_xen_SOURCES = check_xen.c $(top_builddir)/src/slist.h $(top_builddir)/src/xen.h mock_xenstore.c
265-check_xen_LDADD = $(top_builddir)/src/xen.o $(top_builddir)/src/slist.o @CHECK_LIBS@
266+check_xen_LDADD = $(top_builddir)/src/xen.o $(top_builddir)/src/slist.o $(top_builddir)/src/log.o @CHECK_LIBS@
267
268 check_mock_xenstore_SOURCES = check_mock_xenstore.c mock_xenstore.c
269 check_mock_xenstore_LDADD = @CHECK_LIBS@ $(top_builddir)/src/slist.o
270
271 check_spool_SOURCES = check_spool.c $(top_builddir)/src/spool.h $(top_builddir)/src/util.h
272-check_spool_LDADD = $(top_builddir)/src/slist.o $(top_builddir)/src/spool.o $(top_builddir)/src/util.o @CHECK_LIBS@
273+check_spool_LDADD = $(top_builddir)/src/slist.o $(top_builddir)/src/spool.o $(top_builddir)/src/util.o $(top_builddir)/src/log.o @CHECK_LIBS@
274
275 check_util_SOURCES = check_util.c $(top_builddir)/src/util.h
276 check_util_LDADD = $(top_builddir)/src/util.o @CHECK_LIBS@
277@@ -23,4 +23,4 @@
278 check_json_LDADD = $(top_builddir)/src/json.o $(top_builddir)/src/util.o @CHECK_LIBS@ -ljson
279
280 check_agent_SOURCES = check_agent.c $(top_builddir)/src/agent.h $(top_builddir)/src/xen.h $(top_builddir)/src/util.h $(top_builddir)/src/slist.h mock_xenstore.c
281-check_agent_LDADD = $(top_builddir)/src/json.o $(top_builddir)/src/agent.o $(top_builddir)/src/spool.o $(top_builddir)/src/util.o $(top_builddir)/src/xen.o $(top_builddir)/src/slist.o @CHECK_LIBS@ -ljson
282+check_agent_LDADD = $(top_builddir)/src/json.o $(top_builddir)/src/agent.o $(top_builddir)/src/spool.o $(top_builddir)/src/util.o $(top_builddir)/src/xen.o $(top_builddir)/src/slist.o $(top_builddir)/src/log.o @CHECK_LIBS@ -ljson
283
284=== modified file 'tests/check_spool.c'
285--- tests/check_spool.c 2010-07-07 09:26:52 +0000
286+++ tests/check_spool.c 2010-08-24 15:57:43 +0000
287@@ -112,7 +112,7 @@
288 FILE *fp;
289
290 setenv("AGENT_MSG_SPOOL", testdir, 1);
291- fail_unless(spool_watch(spool_cb) == 1, "Failed to add watch.");
292+ fail_unless(spool_watch(spool_cb) == 0, "Failed to add watch.");
293 spool_process_pending_events();
294 fail_unless(spool_cb_called == 0, "Spool callback got called even though nothing had happened yet.");
295 fp = fopen(util_join_paths(spool_incoming_dir(), "testfile"), "a+");

Subscribers

People subscribed via source and target branches