From be27cccf45940bdb369a500ca8db49b3421c0741 Mon Sep 17 00:00:00 2001 From: snow flurry Date: Mon, 16 Nov 2020 20:56:25 -0800 Subject: [PATCH] config: fix some glaring errors... ... like not properly initializing the config struct. Sigh. --- src/config.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/config.c b/src/config.c index 2613aeb..18336e2 100644 --- a/src/config.c +++ b/src/config.c @@ -43,17 +43,17 @@ parse_config(char *path) return NULL; } - conf = malloc(sizeof(config_t)); + conf = calloc(1, sizeof(config_t)); if (conf == NULL) { perror("couldn't allocate config"); return NULL; } - memset(conf, 0, sizeof(config_t)); state = ST_FILENAME; line = col = 1; ws = comment = err = 0; i = 0; + cur = conf; while ((ch = getc(fd)) != EOF) { switch (ch) { case '\t': @@ -65,11 +65,16 @@ parse_config(char *path) /* only run on the first whitespace */ if (!ws) { DPRINTF(("i = %d\n", i)); - i = 0; ws = 1; state++; DPRINTF(("%s:%d:%d: switching to state %d\n", path, line, col, state)); - switch (state) { + switch (state-1) { + case ST_FILENAME: + cur->filename[i] = 0; + break; + case ST_EXT: + cur->ext[i] = 0; + break; /* convert offsets to uint32_t (or close enough) */ case ST_START: cur->start = strtoul(ibuf, NULL, 10); @@ -83,13 +88,8 @@ parse_config(char *path) case ST_SKIP_B: cur->skip_b = strtoul(ibuf, NULL, 10); break; - /* parse options */ - case ST_OPTS: - if (parse_conf_opts(&cur->opts, ibuf) != 0) { - PMSG("error", "unable to parse opts (see above)"); - err = 1; - } } + i = 0; memset(ibuf, 0, 32); } break; @@ -102,7 +102,7 @@ parse_config(char *path) * beginning of a line. */ if (state != ST_FILENAME || !comment) { - cur->next = malloc(sizeof(config_t)); + cur->next = calloc(1, sizeof(config_t)); if (cur == NULL) { perror("couldn't allocate config"); err = 1; @@ -113,9 +113,14 @@ parse_config(char *path) PMSG("error", "line ends early"); err = 1; break; + } else { + DPRINTF(("filename: %s.%s\n", cur->filename, cur->ext)); + if (parse_conf_opts(&cur->opts, ibuf) != 0) { + PMSG("error", "unable to parse opts (see above)"); + err = 1; + } } - memset(cur->next, 0, sizeof(config_t)); cur = cur->next; } @@ -143,7 +148,6 @@ parse_config(char *path) err = 1; } else { cur->filename[i] = toupper(ch); - DPRINTF(("%c", cur->filename[i])); i++; } break; @@ -153,7 +157,6 @@ parse_config(char *path) err = 1; } else { cur->ext[i] = toupper(ch); - DPRINTF(("%c", cur->ext[i])); i++; } break; @@ -178,10 +181,10 @@ parse_config(char *path) } } - col++; - if (err) break; + + col++; }