Skip to content

Commit

Permalink
Merge pull request #99 from troglobit/fix-segfault-on-comment-only-lines
Browse files Browse the repository at this point in the history
Fix #98: segfault on comment only lines
  • Loading branch information
troglobit committed May 31, 2017
2 parents fa2ff5c + 7d70dd2 commit 0f129d9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/lexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ static void qbeg(int state)

static char *trim_whitespace(char *str, unsigned int len)
{
if (!str || !str[0])
return str;

while (len > 1) {
if ((str[len] == 0 || isspace(str[len])) && isspace(str[len - 1]))
len--;
Expand Down
2 changes: 1 addition & 1 deletion tests/annotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
int main(void)
{
char *comment;
char *expect = "A comment we think will go with this option";
char *expect = "Now, is it this comment that goes with the option?";
cfg_t *cfg;
cfg_opt_t *opt;
cfg_opt_t section_opts[] = {
Expand Down
6 changes: 4 additions & 2 deletions tests/annotate.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ section {
/* Another comment, why we set to true */
bool = true

### A comment we think will go with this option
option = "One in ten Vogons die each year from the aftershocks" //// But its really this one, it's closer
### Now, is it this comment that goes with the option?
option = "One in ten Vogons die each year from the aftershocks" //// Or this?

# libConfuse currently favors the former, easier to parse
}
8 changes: 8 additions & 0 deletions tests/suite_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,14 @@ static void parse_buf_test(void)
buf = "######## this is also a shell comment\nbool = {true} ## Use the force, Luke";
fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);

/* Issue #98: Line of only hashes cause segfault */
buf = "##############################################\n# some text #\n##############################################\nbool = {false}";
fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
buf = "////////////////////////////////////////////////////////////////////////////////////////////\n// some text //\n////////////////////////////////////////////////////////////////////////////////////////////\nbool = {false}";
fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
buf = "/******************************************************************************************/\n// some text //\n/******************************************************************************************/\nbool = {false}";
fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);

buf = "string={/usr/local/}";
fail_unless(cfg_parse_buf(cfg, buf) == CFG_SUCCESS);
fail_unless(strcmp(cfg_getnstr(cfg, "string", 0), "/usr/local/") == 0);
Expand Down

0 comments on commit 0f129d9

Please sign in to comment.