Skip to content

Commit 27d0d2f

Browse files
committed
mctpd: Fix memory leak
The dfree() defer handler needs to be unreffed, and the default event loop needs to be used as the running main loop. The event loop is now unreffed after use, though it will not be released as there are still pending events at exit. Signed-off-by: Matt Johnston <[email protected]>
1 parent 892bba3 commit 27d0d2f

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/mctpd.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,27 +267,32 @@ static const char* peer_tostr(const peer *peer)
267267
static int defer_free_handler(sd_event_source *s, void *userdata)
268268
{
269269
free(userdata);
270+
sd_event_source_unref(s);
270271
return 0;
271272
}
272273

273274
/* Returns ptr, frees it on the next default event loop cycle (defer)*/
274275
static void* dfree(void* ptr)
275276
{
276-
sd_event *e;
277+
sd_event *e = NULL;
277278
int rc;
278279

279280
if (!ptr)
280281
return NULL;
281282
rc = sd_event_default(&e);
282283
if (rc < 0) {
283284
warnx("defer_free no event loop");
284-
return ptr;
285+
goto out;
285286
}
286287
rc = sd_event_add_defer(e, NULL, defer_free_handler, ptr);
287288
if (rc < 0) {
288289
warnx("defer_free failed adding");
289-
return ptr;
290+
goto out;
290291
}
292+
293+
out:
294+
if (e)
295+
sd_event_unref(e);
291296
return ptr;
292297
}
293298

@@ -2582,7 +2587,8 @@ static int setup_bus(ctx *ctx)
25822587
{
25832588
int rc;
25842589

2585-
rc = sd_event_new(&ctx->event);
2590+
// Must use the default loop so that dfree() can use it without context.
2591+
rc = sd_event_default(&ctx->event);
25862592
if (rc < 0) {
25872593
warnx("sd_event failed");
25882594
goto out;
@@ -3205,6 +3211,7 @@ int main(int argc, char **argv)
32053211

32063212

32073213
rc = sd_event_loop(ctx->event);
3214+
sd_event_unref(ctx->event);
32083215
if (rc < 0) {
32093216
warnx("Error in loop, returned %s %d", strerror(-rc), rc);
32103217
return 1;

0 commit comments

Comments
 (0)