From 56ef8fcedc48bed353e3ccc694a8db3f322fae6f Mon Sep 17 00:00:00 2001 From: Robin Hsu Date: Thu, 11 Mar 2021 18:50:38 +0800 Subject: Fix grep bug testing errno before check status It's legal for a system call to set non-zero errno and return a good status. The caller (grep) should check return status first. Test: 2000 loops of greping 1000+ lines each loop Signed-off-by: Robin Hsu Change-Id: I55f7cd5d8a6289c5e8a21ed3057e995a75b9b4fa --- toys/posix/grep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toys/posix/grep.c b/toys/posix/grep.c index cd8928a1..52d10139 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -154,7 +154,7 @@ static void do_grep(int fd, char *name) lcount++; errno = 0; ulen = len = getdelim(&line, &ulen, TT.indelim, file); - if (errno) perror_msg("%s", name); + if (len == -1 && errno) perror_msg("%s", name); if (len<1) break; if (line[ulen-1] == TT.indelim) line[--ulen] = 0; -- cgit v1.2.3