diff options
author | Joseph Hunkeler <jhunkeler@users.noreply.github.com> | 2020-04-08 10:23:35 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-08 10:23:35 -0400 |
commit | 3ff0e986bee05be4abb3c49d2fddec0fa8ccfc59 (patch) | |
tree | a7838359ebc6f603f7e4a144aa6322c7fc94c9dd /lib | |
parent | 06da826131ba1c5e28731fc9bae975fc21b13da1 (diff) | |
download | spmc-3ff0e986bee05be4abb3c49d2fddec0fa8ccfc59.tar.gz |
Add test_relocation_file_replace_text (#14)
* Add test_relocation_file_replace_text
* Fix bad BUFSIZ allocation and interaction
* Use the line limit not the number of lines
* Zero line buffer before next read
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mirrors.c | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/lib/mirrors.c b/lib/mirrors.c index cad3f6b..6bc7aed 100644 --- a/lib/mirrors.c +++ b/lib/mirrors.c @@ -14,7 +14,7 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn } // Allocate buffer - if ((buffer = calloc(BUFSIZ + 1, sizeof(char))) == NULL) { + if ((buffer = calloc(BUFSIZ, sizeof(char))) == NULL) { perror("line buffer"); fprintf(SYSERROR); fclose(fp); @@ -22,7 +22,7 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn } // count number the of lines in the file - while ((fgets(buffer, BUFSIZ, fp)) != NULL) { + while ((fgets(buffer, BUFSIZ - 1, fp)) != NULL) { lines++; } @@ -51,13 +51,13 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn } // Populate results array - result = calloc(lines + 1, sizeof(char *)); + result = calloc(limit + 1, sizeof(char *)); for (size_t i = start; i < limit; i++) { if (i < start) { continue; } - if (fgets(buffer, BUFSIZ, fp) == NULL) { + if (fgets(buffer, BUFSIZ - 1, fp) == NULL) { break; } @@ -73,7 +73,8 @@ char **file_readlines(const char *filename, size_t start, size_t limit, ReaderFn break; } } - result[i - start] = strdup(buffer); + result[i] = strdup(buffer); + memset(buffer, '\0', BUFSIZ); } free(buffer); |