From 8dcce562cd5b2c29ff7125f16fbec12a13920ec8 Mon Sep 17 00:00:00 2001 From: Liam Waldron Date: Thu, 4 May 2023 13:13:06 -0400 Subject: [PATCH] fix segfault when config.emk does not exist --- src/parse.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/parse.c b/src/parse.c index eb351aa..dabdf82 100644 --- a/src/parse.c +++ b/src/parse.c @@ -26,6 +26,12 @@ #include "parse.h" +/* Variable indicating whether config.emk exists + * 1 means config.emk exists + * 0 means config.emk does not exist + */ +int emkConfigExists; + struct emkfileData { char cc; char prefix; @@ -60,6 +66,7 @@ char checkForFiles() { printf(COL_YELLOW "WRN" COL_RESET " | use a config.emk file, rather\n"); printf(COL_YELLOW "WRN" COL_RESET " | than embedding settings directly\n"); printf(COL_YELLOW "WRN" COL_RESET " | within the emkfile.\n"); + emkConfigExists = 1; } else if (getcwd(cwd, sizeof(cwd)) != NULL) { printf(COL_BLUE "INF" COL_RESET " | using config.emk at %s/config.emk\n", cwd); } @@ -85,8 +92,10 @@ char includeConfig() { FILE *emkconfig, *emkfile, *emkfile_new; char filename[100], contents; - printf(COL_BLUE "INF" COL_RESET " | parsing config.emk\n"); - emkconfig = fopen(EMKCONFIG, "r"); + if (emkConfigExists == 1) { + printf(COL_BLUE "INF" COL_RESET " | parsing config.emk\n"); + emkconfig = fopen(EMKCONFIG, "r"); + } printf(COL_BLUE "INF" COL_RESET " | parsing emkfile\n"); emkfile = fopen(EMKFILE, "r"); @@ -95,10 +104,12 @@ char includeConfig() { printf(COL_BLUE "INF" COL_RESET " | running preprocessing tasks...\n"); /* write emkconfig to new emkfile */ - contents = fgetc(emkconfig); - while (contents != EOF) { - fputc(contents, emkfile_new); + if (emkConfigExists == 1) { contents = fgetc(emkconfig); + while (contents != EOF) { + fputc(contents, emkfile_new); + contents = fgetc(emkconfig); + } } /* write emkfile to new emkfile */ @@ -109,7 +120,9 @@ char includeConfig() { } printf(COL_BLUE "INF" COL_RESET " | closing files\n"); - fclose(emkconfig); + if (emkConfigExists == 1) { + fclose(emkconfig); + } fclose(emkfile); fclose(emkfile_new); }