diff options
author | Rory Bradford <roryrjb@gmail.com> | 2020-08-02 15:31:16 +0100 |
---|---|---|
committer | Rory Bradford <roryrjb@gmail.com> | 2020-08-02 15:31:16 +0100 |
commit | 11a95ee7fa9886ee2f62df940c7a53cfa1a8698a (patch) | |
tree | d5904cf69111a1d6eb2c2dd73257003e69079817 /ignore.c | |
parent | 69631ff66e5afa6993037b60e2db4348a2f7b718 (diff) | |
download | rf-11a95ee7fa9886ee2f62df940c7a53cfa1a8698a.tar.gz |
Additional safety
Signed-off-by: Rory Bradford <roryrjb@gmail.com>
Diffstat (limited to 'ignore.c')
-rw-r--r-- | ignore.c | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/ignore.c b/ignore.c index d7b6428..8abca6e 100644 --- a/ignore.c +++ b/ignore.c @@ -12,25 +12,42 @@ static int total_size; struct ignores *init_ignores(char *path) { + int i = 0; + FILE *ignore = fopen(path, "r"); + + if (ignore == NULL) { + return NULL; + } + + char *line = NULL; + size_t llen = 0; total_size = IGNORE_SIZE; struct ignores *ignores = calloc(sizeof(struct ignores), 1); ignores->list = calloc(sizeof(char *), IGNORE_SIZE); ignores->size = 0; - int i = 0; - FILE *ignore = fopen(path, "r"); - char *line = NULL; - size_t llen = 0; - ssize_t r; + while (getline(&line, &llen, ignore) != -1) { + /** isspace doesn't necessarily hold true for `\n` for anything + * other than the 'C' locale on some platforms, so we have to do + * an additional check for this + */ + + if (strlen(line) == 1 && (isspace(line[0]) || line[0] == '\n')) { + continue; + } - if (ignore != NULL) { - while ((r = getline(&line, &llen, ignore)) != -1) { - char *l = calloc(sizeof(char *), strlen(line) - 1); + if (strlen(line) > 0) { + char *l = calloc(sizeof(char), strlen(line)); + + if (l == NULL) { + fprintf(stderr, "calloc\n"); + exit(EXIT_FAILURE); + } for (size_t j = 0, k = 0; j < strlen(line); j++) { char c = line[j]; - if (isspace(c)) { + if (isspace(c) || c == '\n') { break; } @@ -45,23 +62,25 @@ struct ignores *init_ignores(char *path) { ignores->list[i++] = l; } - - free(line); - fclose(ignore); } + free(line); + fclose(ignore); + ignores->size = i; return ignores; } void free_ignores(struct ignores *ignores) { - if (ignores->size > 0) { - for (int i = 0; i < ignores->size; i++) { - free(ignores->list[i]); + if (ignores != NULL) { + if (ignores->size > 0) { + for (int i = 0; i < ignores->size; i++) { + free(ignores->list[i]); + } } - } - free(ignores->list); - free(ignores); + free(ignores->list); + free(ignores); + } } |