Skip to content

Commit

Permalink
Merge pull request #70 from sisoputnfrba/56-config-segfault-double-ne…
Browse files Browse the repository at this point in the history
…wline

Halt string_split tokenization if there are no more tokens
  • Loading branch information
gastonprieto authored Mar 2, 2017
2 parents e3bd7a8 + 5f8aae6 commit 7b34318
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/commons/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ char** _string_split(char* text, char* separator, bool(*condition)(char*, int))

while(condition(next, size)) {
char* token = strtok_r(str, separator, &next);
if(token == NULL) {
break;
}
str = NULL;
size++;
substrings = realloc(substrings, sizeof(char*) * size);
Expand Down
17 changes: 17 additions & 0 deletions tests/unit-tests/resources/config-double-newline.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This is the IP
IP=127.0.0.1

PORT=8080

PROCESS_NAME=TEST

# This is the load
LOAD=0.5


# This is an array
NUMBERS=[1, 2, 3, 4, 5]

EMPTY_ARRAY=[]


8 changes: 8 additions & 0 deletions tests/unit-tests/test_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,12 @@ context (test_config) {

} end

describe("Double newline") {

it ("should not fail when the config file ends with two newlines") {
t_config *config = config_create("resources/config-double-newline.cfg");
config_destroy(config);
} end
} end

}

0 comments on commit 7b34318

Please sign in to comment.