Check getline()-return-values properly
It's not useful when 0 is returned anyway, so be sure that we have a string with length > 0, this also solves some indexing-gotchas like "len - 1" and so on. Also, add checked getline()'s whenever it has been forgotten and clean up the error-messages.
This commit is contained in:
4
grep.c
4
grep.c
@@ -87,7 +87,7 @@ addpatternfile(FILE *fp)
|
||||
static size_t size = 0;
|
||||
ssize_t len = 0;
|
||||
|
||||
while ((len = getline(&buf, &size, fp)) != -1) {
|
||||
while ((len = getline(&buf, &size, fp)) > 0) {
|
||||
if (len > 0 && buf[len - 1] == '\n')
|
||||
buf[len - 1] = '\0';
|
||||
addpattern(buf);
|
||||
@@ -106,7 +106,7 @@ grep(FILE *fp, const char *str)
|
||||
struct pattern *pnode;
|
||||
int match = NoMatch;
|
||||
|
||||
for (n = 1; (len = getline(&buf, &size, fp)) != -1; n++) {
|
||||
for (n = 1; (len = getline(&buf, &size, fp)) > 0; n++) {
|
||||
/* Remove the trailing newline if one is present. */
|
||||
if (len && buf[len - 1] == '\n')
|
||||
buf[len - 1] = '\0';
|
||||
|
Reference in New Issue
Block a user