conf: misc valgrind fixes

This commit is contained in:
Daniel Eklöf 2019-07-21 11:46:46 +02:00
parent 07b3e76062
commit a3b4a53102
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F

View file

@ -208,17 +208,17 @@ parse_config_file(FILE *f, struct config *conf, const char *path)
#endif #endif
unsigned lineno = 0; unsigned lineno = 0;
char *_line = NULL;
while (true) { while (true) {
errno = 0; errno = 0;
lineno++; lineno++;
char *line = NULL;
size_t count = 0; size_t count = 0;
ssize_t ret = getline(&line, &count, f); ssize_t ret = getline(&_line, &count, f);
if (ret < 0) { if (ret < 0) {
free(line); free(_line);
if (errno != 0) { if (errno != 0) {
LOG_ERRNO("failed to read from configuration"); LOG_ERRNO("failed to read from configuration");
return false; return false;
@ -226,12 +226,31 @@ parse_config_file(FILE *f, struct config *conf, const char *path)
break; break;
} }
/* Strip whitespace */
char *line = _line;
{
while (isspace(*line))
line++;
if (line[0] != '\0') {
char *end = line + strlen(line) - 1;
while (isspace(*end))
end--;
*(end + 1) = '\0';
}
}
/* Empty line, or comment */
if (line[0] == '\0' || line[0] == '#') {
free(_line);
continue;
}
/* Check for new section */
if (line[0] == '[') { if (line[0] == '[') {
char *end = strchr(line, ']'); char *end = strchr(line, ']');
if (end == NULL) { if (end == NULL) {
LOG_ERR("%s:%d: syntax error: %s", path, lineno, line); LOG_ERR("%s:%d: syntax error: %s", path, lineno, line);
free(line); goto err;
return false;
} }
*end = '\0'; *end = '\0';
@ -240,19 +259,22 @@ parse_config_file(FILE *f, struct config *conf, const char *path)
section = SECTION_COLORS; section = SECTION_COLORS;
else { else {
LOG_ERR("%s:%d: invalid section name: %s", path, lineno, &line[1]); LOG_ERR("%s:%d: invalid section name: %s", path, lineno, &line[1]);
free(line); goto err;
return false;
} }
free(_line);
continue; continue;
} }
char *key = strtok(line, "="); char *key = strtok(line, "=");
char *value = strtok(NULL, "\n"); char *value = strtok(NULL, "\n");
LOG_INFO("key = %s, value = %s", key, value);
/* Strip trailing whitespace from key (leading stripped earlier) */
{ {
while (isspace(*key)) assert(!isspace(*key));
key++;
char *end = key + strlen(key) - 1; char *end = key + strlen(key) - 1;
while (isspace(*end)) while (isspace(*end))
end--; end--;
@ -262,25 +284,22 @@ parse_config_file(FILE *f, struct config *conf, const char *path)
if (value == NULL) { if (value == NULL) {
if (key != NULL && strlen(key) > 0 && key[0] != '#') { if (key != NULL && strlen(key) > 0 && key[0] != '#') {
LOG_ERR("%s:%d: syntax error: %s", path, lineno, line); LOG_ERR("%s:%d: syntax error: %s", path, lineno, line);
free(line); goto err;
return false;
} }
free(line); free(_line);
continue; continue;
} }
/* Strip leading whitespace from value (trailing stripped earlier) */
{ {
while (isspace(*value)) while (isspace(*value))
value++; value++;
char *end = value + strlen(value) - 1; assert(!isspace(*(value + strlen(value) - 1)));
while (isspace(*end))
end--;
*(end + 1) = '\0';
} }
if (key[0] == '#') { if (key[0] == '#') {
free(line); free(_line);
continue; continue;
} }
@ -290,15 +309,17 @@ parse_config_file(FILE *f, struct config *conf, const char *path)
parser_fun_t section_parser = section_parser_map[section]; parser_fun_t section_parser = section_parser_map[section];
assert(section_parser != NULL); assert(section_parser != NULL);
if (!section_parser(key, value, conf, path, lineno)) { if (!section_parser(key, value, conf, path, lineno))
free(line); goto err;
return false;
}
free(line); free(_line);
} }
return true; return true;
err:
free(_line);
return false;
} }
bool bool
@ -363,6 +384,7 @@ out:
void void
config_free(struct config conf) config_free(struct config conf)
{ {
free(conf.term);
free(conf.shell); free(conf.shell);
free(conf.font); free(conf.font);
} }