config: fix some glaring errors...

... like not properly initializing the config struct. Sigh.
This commit is contained in:
snow flurry 2020-11-16 20:56:25 -08:00
parent 6e9a68711d
commit be27cccf45

View file

@ -43,17 +43,17 @@ parse_config(char *path)
return NULL; return NULL;
} }
conf = malloc(sizeof(config_t)); conf = calloc(1, sizeof(config_t));
if (conf == NULL) { if (conf == NULL) {
perror("couldn't allocate config"); perror("couldn't allocate config");
return NULL; return NULL;
} }
memset(conf, 0, sizeof(config_t));
state = ST_FILENAME; state = ST_FILENAME;
line = col = 1; line = col = 1;
ws = comment = err = 0; ws = comment = err = 0;
i = 0; i = 0;
cur = conf;
while ((ch = getc(fd)) != EOF) { while ((ch = getc(fd)) != EOF) {
switch (ch) { switch (ch) {
case '\t': case '\t':
@ -65,11 +65,16 @@ parse_config(char *path)
/* only run on the first whitespace */ /* only run on the first whitespace */
if (!ws) { if (!ws) {
DPRINTF(("i = %d\n", i)); DPRINTF(("i = %d\n", i));
i = 0;
ws = 1; ws = 1;
state++; state++;
DPRINTF(("%s:%d:%d: switching to state %d\n", path, line, col, 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) */ /* convert offsets to uint32_t (or close enough) */
case ST_START: case ST_START:
cur->start = strtoul(ibuf, NULL, 10); cur->start = strtoul(ibuf, NULL, 10);
@ -83,13 +88,8 @@ parse_config(char *path)
case ST_SKIP_B: case ST_SKIP_B:
cur->skip_b = strtoul(ibuf, NULL, 10); cur->skip_b = strtoul(ibuf, NULL, 10);
break; 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); memset(ibuf, 0, 32);
} }
break; break;
@ -102,7 +102,7 @@ parse_config(char *path)
* beginning of a line. * beginning of a line.
*/ */
if (state != ST_FILENAME || !comment) { if (state != ST_FILENAME || !comment) {
cur->next = malloc(sizeof(config_t)); cur->next = calloc(1, sizeof(config_t));
if (cur == NULL) { if (cur == NULL) {
perror("couldn't allocate config"); perror("couldn't allocate config");
err = 1; err = 1;
@ -113,9 +113,14 @@ parse_config(char *path)
PMSG("error", "line ends early"); PMSG("error", "line ends early");
err = 1; err = 1;
break; 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; cur = cur->next;
} }
@ -143,7 +148,6 @@ parse_config(char *path)
err = 1; err = 1;
} else { } else {
cur->filename[i] = toupper(ch); cur->filename[i] = toupper(ch);
DPRINTF(("%c", cur->filename[i]));
i++; i++;
} }
break; break;
@ -153,7 +157,6 @@ parse_config(char *path)
err = 1; err = 1;
} else { } else {
cur->ext[i] = toupper(ch); cur->ext[i] = toupper(ch);
DPRINTF(("%c", cur->ext[i]));
i++; i++;
} }
break; break;
@ -178,10 +181,10 @@ parse_config(char *path)
} }
} }
col++;
if (err) if (err)
break; break;
col++;
} }