Skip to content

Commit

Permalink
log: fix memory leaks in the error case
Browse files Browse the repository at this point in the history
Modified according to the review comments below
#189
  • Loading branch information
takahasi committed Feb 16, 2017
1 parent b832e80 commit 38ce622
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/flb_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ void flb_config_exit(struct flb_config *config)
flb_worker_exit(config);

/* Event flush */
mk_event_del(config->evl, &config->event_flush);
if (config->evl) {
mk_event_del(config->evl, &config->event_flush);
}
close(config->flush_fd);

/* Release scheduler */
Expand All @@ -237,7 +239,9 @@ void flb_config_exit(struct flb_config *config)
flb_free(config->buffer_path);
#endif

mk_event_loop_destroy(config->evl);
if (config->evl) {
mk_event_loop_destroy(config->evl);
}
flb_free(config);
}

Expand Down
4 changes: 4 additions & 0 deletions src/flb_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ flb_ctx_t *flb_create()
/* Initialize logger */
log = flb_log_init(config, FLB_LOG_STDERR, FLB_LOG_INFO, NULL);
if (!log) {
flb_config_exit(ctx->config);
flb_free(ctx);
return NULL;
}
Expand All @@ -126,13 +127,15 @@ flb_ctx_t *flb_create()
ret = flb_pipe_create(config->ch_data);
if (ret == -1) {
perror("pipe");
flb_config_exit(ctx->config);
flb_free(ctx);
return NULL;
}

/* Create the event loop to receive notifications */
ctx->event_loop = mk_event_loop_create(256);
if (!ctx->event_loop) {
flb_config_exit(ctx->config);
flb_free(ctx);
return NULL;
}
Expand All @@ -146,6 +149,7 @@ flb_ctx_t *flb_create()
ctx->event_channel);
if (ret != 0) {
flb_error("[lib] could not create notification channels");
flb_config_exit(ctx->config);
flb_destroy(ctx);
return NULL;
}
Expand Down
14 changes: 10 additions & 4 deletions src/flb_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
if (!evl) {
fprintf(stderr, "[log] could not create event loop\n");
flb_free(log);
config->log = NULL;
return NULL;
}

Expand All @@ -194,8 +195,9 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
ret = flb_pipe_create(log->ch_mng);
if (ret == -1) {
fprintf(stderr, "[log] could not create pipe(2)");
mk_event_loop_destroy(log->evl);
flb_free(log);
mk_event_loop_destroy(evl);
config->log = NULL;
return NULL;
}
MK_EVENT_NEW(&log->event);
Expand All @@ -205,8 +207,9 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
FLB_LOG_MNG, MK_EVENT_READ, &log->event);
if (ret == -1) {
fprintf(stderr, "[log] could not register event\n");
mk_event_loop_destroy(log->evl);
flb_free(log);
mk_event_loop_destroy(evl);
config->log = NULL;
return NULL;
}

Expand All @@ -218,8 +221,9 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
worker = flb_malloc(sizeof(struct flb_worker));
if (!worker) {
flb_errno();
mk_event_loop_destroy(log->evl);
flb_free(log);
mk_event_loop_destroy(evl);
config->log = NULL;
return NULL;
}
worker->func = NULL;
Expand All @@ -232,8 +236,9 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
ret = flb_log_worker_init(worker);
if (ret == -1) {
flb_errno();
mk_event_loop_destroy(log->evl);
flb_free(log);
mk_event_loop_destroy(evl);
config->log = NULL;
flb_free(worker);
return NULL;
}
Expand All @@ -255,6 +260,7 @@ struct flb_log *flb_log_init(struct flb_config *config, int type,
mk_event_loop_destroy(log->evl);
flb_free(log->worker);
flb_free(log);
config->log = NULL;
return NULL;
}

Expand Down

0 comments on commit 38ce622

Please sign in to comment.