From: Timo Sirainen Date: Wed, 3 May 2023 09:29:26 +0000 (+0300) Subject: config: Fix memory leak on config parsing failure X-Git-Tag: 2.4.0~2101 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a3dfd65ebc43e1e3149dcaf425006caabb6caa0;p=thirdparty%2Fdovecot%2Fcore.git config: Fix memory leak on config parsing failure --- diff --git a/src/config/config-filter.c b/src/config/config-filter.c index e118b06fb3..c325c8e945 100644 --- a/src/config/config-filter.c +++ b/src/config/config-filter.c @@ -117,6 +117,7 @@ struct config_filter_context *config_filter_init(pool_t pool) ctx = p_new(pool, struct config_filter_context, 1); ctx->pool = pool; + pool_ref(ctx->pool); p_array_init(&ctx->errors, pool, 1); return ctx; } diff --git a/src/config/config-parser.c b/src/config/config-parser.c index e9af29d6df..15bd86dd1c 100644 --- a/src/config/config-parser.c +++ b/src/config/config-parser.c @@ -755,6 +755,7 @@ config_parse_line(struct config_parser_context *ctx, static int config_parse_finish(struct config_parser_context *ctx, + enum config_parse_flags flags, struct config_filter_context **filter_r, const char **error_r) { @@ -776,6 +777,10 @@ config_parse_finish(struct config_parser_context *ctx, ctx->path, error); } + if (ret < 0 && (flags & CONFIG_PARSE_FLAG_RETURN_BROKEN_CONFIG) == 0) { + config_filter_deinit(&new_filter); + return -1; + } config_module_parsers = ctx->root_parsers; *filter_r = new_filter; return ret; @@ -1119,7 +1124,8 @@ prevfile: hash_table_destroy(&ctx.seen_settings); str_free(&full_line); if (ret == 0) - ret = config_parse_finish(&ctx, filter_r, error_r); + ret = config_parse_finish(&ctx, flags, filter_r, error_r); + pool_unref(&ctx.pool); return ret < 0 ? ret : 1; } diff --git a/src/config/config-parser.h b/src/config/config-parser.h index 593974c217..f993075cf0 100644 --- a/src/config/config-parser.h +++ b/src/config/config-parser.h @@ -11,6 +11,7 @@ enum config_parse_flags { CONFIG_PARSE_FLAG_EXPAND_VALUES = BIT(0), CONFIG_PARSE_FLAG_HIDE_OBSOLETE_WARNINGS = BIT(1), CONFIG_PARSE_FLAG_DELAY_ERRORS = BIT(3), + CONFIG_PARSE_FLAG_RETURN_BROKEN_CONFIG = BIT(4), }; struct config_module_parser { diff --git a/src/config/doveconf.c b/src/config/doveconf.c index 1a671d0f30..9eebc619ec 100644 --- a/src/config/doveconf.c +++ b/src/config/doveconf.c @@ -920,7 +920,7 @@ int main(int argc, char *argv[]) } } - enum config_parse_flags flags = 0; + enum config_parse_flags flags = CONFIG_PARSE_FLAG_RETURN_BROKEN_CONFIG; if (expand_vars) flags |= CONFIG_PARSE_FLAG_EXPAND_VALUES; if (dump_full && exec_args != NULL && !check_full_config)