Commit 2a8bc9d6 authored by Daniel Salzman's avatar Daniel Salzman

modules: fix memory leaks if load fails

parent 0be999ee
......@@ -149,6 +149,14 @@ static int ratelimit_apply(int state, knot_pkt_t *pkt, struct query_data *qdata,
}
}
static void ctx_free(knot_mm_t *mm, rrl_ctx_t *ctx)
{
assert(ctx);
rrl_destroy(ctx->rrl);
mm_free(mm, ctx);
}
int rrl_load(struct query_module *self)
{
assert(self);
......@@ -164,14 +172,14 @@ int rrl_load(struct query_module *self)
conf_val_t val = conf_mod_get(self->config, MOD_TBL_SIZE, self->id);
ctx->rrl = rrl_create(conf_int(&val));
if (ctx->rrl == NULL) {
mm_free(self->mm, ctx);
ctx_free(self->mm, ctx);
return KNOT_ENOMEM;
}
// Set locks.
int ret = rrl_setlocks(ctx->rrl, RRL_LOCK_GRANULARITY);
if (ret != KNOT_EOK) {
rrl_unload(self);
ctx_free(self->mm, ctx);
return ret;
}
......@@ -179,7 +187,7 @@ int rrl_load(struct query_module *self)
val = conf_mod_get(self->config, MOD_RATE_LIMIT, self->id);
ret = rrl_setrate(ctx->rrl, conf_int(&val));
if (ret != KNOT_EOK) {
rrl_unload(self);
ctx_free(self->mm, ctx);
return ret;
}
......@@ -194,13 +202,13 @@ int rrl_load(struct query_module *self)
// Set up statistics counters.
ret = mod_stats_add(self, "slipped", 1, NULL);
if (ret != KNOT_EOK) {
rrl_unload(self);
ctx_free(self->mm, ctx);
return ret;
}
ret = mod_stats_add(self, "dropped", 1, NULL);
if (ret != KNOT_EOK) {
rrl_unload(self);
ctx_free(self->mm, ctx);
return ret;
}
......@@ -216,6 +224,5 @@ void rrl_unload(struct query_module *self)
rrl_ctx_t *ctx = self->ctx;
rrl_destroy(ctx->rrl);
mm_free(self->mm, self->ctx);
ctx_free(self->mm, ctx);
}
......@@ -545,6 +545,7 @@ int stats_load(struct query_module *self)
int ret = mod_stats_add(self, enabled ? desc->conf_name + 1 : NULL,
desc->count, desc->fcn);
if (ret != KNOT_EOK) {
mm_free(self->mm, stats);
return ret;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment