cfparser: require that first section must be present
authorMarko Kreen <[email protected]>
Tue, 2 Apr 2013 20:04:55 +0000 (23:04 +0300)
committerMarko Kreen <[email protected]>
Tue, 2 Apr 2013 20:04:55 +0000 (23:04 +0300)
otherwise, when run on empty file, the main section defaults
are not filled in and program may crash.

usual/cfparser.c

index ff9f4e569942d713232adb118f9db1b16482f5d8..b216d9a566678e58a1c965012e7cf8649981ad27 100644 (file)
@@ -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;
 }