From c0111d42712202df5ccb01c4e07e88ab5c8b94b8 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Tue, 2 Apr 2013 23:04:55 +0300 Subject: [PATCH] cfparser: require that first section must be present otherwise, when run on empty file, the main section defaults are not filled in and program may crash. --- usual/cfparser.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/usual/cfparser.c b/usual/cfparser.c index ff9f4e5..b216d9a 100644 --- a/usual/cfparser.c +++ b/usual/cfparser.c @@ -278,6 +278,7 @@ struct LoaderCtx { const struct CfContext *cf; const char *cur_sect; void *top_base; + bool got_main_sect; }; static bool fill_defaults(struct LoaderCtx *ctx) @@ -289,6 +290,9 @@ static bool fill_defaults(struct LoaderCtx *ctx) if (!s) goto fail; + if (s == ctx->cf->sect_list) + ctx->got_main_sect = true; + if (s->section_start) { if (!s->section_start(ctx->top_base, ctx->cur_sect)) return false; @@ -340,6 +344,10 @@ bool cf_load_file(const struct CfContext *cf, const char *fn) ok = parse_ini_file(fn, load_handler, &ctx); if (ctx.cur_sect) free(ctx.cur_sect); + if (ok && !ctx.got_main_sect) { + log_error("load_init_file: main section missing from config file"); + return false; + } return ok; } -- 2.39.5