Skip to content

Commit

Permalink
Fix #114: Handle very long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
jelu committed Jan 5, 2017
1 parent c02a170 commit 8bd6a67
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/parse_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,8 @@ int parse_conf_tokens(const conf_token_t* tokens, size_t token_size, size_t line

int parse_conf(const char* file) {
FILE* fp;
char buffer[4096];
char* buffer = 0;
size_t bufsize = 0;
char* buf;
size_t s, i, line = 0;
conf_token_t tokens[PARSE_MAX_ARGS];
Expand All @@ -702,13 +703,13 @@ int parse_conf(const char* file) {
if (!(fp = fopen(file, "r"))) {
return 1;
}
while (fgets(buffer, sizeof(buffer), fp)) {
while (getline(&buffer, &bufsize, fp) > 0) {
memset(tokens, 0, sizeof(conf_token_t) * PARSE_MAX_ARGS);
line++;
/*
* Go to the first non white-space character
*/
for (ret = PARSE_CONF_OK, buf = buffer, s = sizeof(buffer); *buf && s; buf++, s--) {
for (ret = PARSE_CONF_OK, buf = buffer, s = bufsize; *buf && s; buf++, s--) {
if (*buf != ' ' && *buf != '\t') {
if (*buf == '\n' || *buf == '\t') {
ret = PARSE_CONF_EMPTY;
Expand Down Expand Up @@ -740,11 +741,13 @@ int parse_conf(const char* file) {
}
else if (ret == PARSE_CONF_OK) {
fprintf(stderr, "CONFIG ERROR [%lu]: Too many arguments", line);
free(buffer);
fclose(fp);
return 1;
}
else if (ret != PARSE_CONF_LAST) {
fprintf(stderr, "CONFIG ERROR [%lu]: Invalid syntax", line);
free(buffer);
fclose(fp);
return 1;
}
Expand All @@ -753,10 +756,12 @@ int parse_conf(const char* file) {
* Configure using the tokens
*/
if (parse_conf_tokens(tokens, i, line)) {
free(buffer);
fclose(fp);
return 1;
}
}
free(buffer);
fclose(fp);

return 0;
Expand Down

0 comments on commit 8bd6a67

Please sign in to comment.